1.
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int main()
{
cin >> n;
int m = n;
int cnt[3] = {0}, cnt2[3] = {0};
while(n -- ){
int t, a, b;
cin >> t >> a >> b;
cnt[t] += a, cnt2[t] += b;
}
for(int i = 1;i <= 2;i ++ ){
if(cnt[i] >= cnt2[i]) puts("LIVE");
else puts("DEAD");
}
return 0;
}
2.简单贪心
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string s;
int k;
int w[26];
int main()
{
cin >> s;
cin >> k;
int maxv = 0;
for(int i = 0;i < 26;i ++ ){
cin >> w[i];
maxv = max(maxv, w[i]);
}
int n = s.size();
int i = 1;
long long res = 0;
for(i = 1;i <= n;i ++ ){
res += i * (w[s[i - 1] - 'a']);
}
while(k -- ){
res += i * maxv;
i ++ ;
}
cout << res << endl;
return 0;
}
3.并查集
debug: 每倒入一种化学物质,如果该物质能够与之前倒入试管中的一种或多种物质发生反应,则试管的危险值将乘以 2。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n, m;
int p[51];
int cnt[51];
int find(int x) // 并查集
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n >> m;
for(int i = 1;i <= n;i ++ ) p[i] = i, cnt[i] = 1;
int maxv = 1;
while(m -- ){
int x, y;
cin >> x >> y;
x = find(x), y = find(y);
if(x != y){
cnt[y] += cnt[x];
p[x] = y;
}
}
long long res = 1;
for(int i = 1;i <= n;i ++ ){
if(p[i] == i){
while(-- cnt[i]){
res *= 2;
}
}
}
cout << res << endl;
return 0;
}