# 2020牛客暑期多校训练营（第八场）

## I-Interesting Computer Game

#include <bits/stdc++.h>
using namespace std;

int _, n, cas;
map <int, int> mp;
bool huan[200005];
int fa[200005], siz[200005];

int find(int x) {
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}

int main() {
scanf("%d", &_);
while(_--) {
int now = 0;
mp.clear();
memset(huan, 0, sizeof(huan));
for(int i = 1;i <= 200000; i++) fa[i] = i, siz[i] = 1;

scanf("%d", &n);
for(int i = 1;i <= n; i++) {
int u, v;
scanf("%d %d", &u, &v);
if(!mp[u]) mp[u] = ++now;
if(!mp[v]) mp[v] = ++now;
int fu = find(mp[u]);
int fv = find(mp[v]);
if(fu == fv) {
huan[fu] = 1;
}
else {
fa[fu] = fv;
siz[fv] += siz[fu];
if(huan[fu]) huan[fv] = 1;
}
}

long long ans = 0;
for(int i = 1;i <= now; i++) {
int fi = find(i);
if(fi != i)continue;
ans += siz[fi];
if(!huan[fi]) ans--;
}

printf("Case #%d: %lld\n",++cas, ans);
}
}


## K-Kabaleo Lite

3 1 4 2
4 2 1 1


#include <bits/stdc++.h>
using namespace std;

int _, n;
long long a[100005], b[100005];
stack <pair <long long, long long> > sta;

inline void write(__int128 x) {
if(x < 0) {
putchar('-');
x = -x;
}
if(x>9) write(x/10);
putchar(x%10+'0');
}

int main() {
int cas = 0;
scanf("%d", &_);
while(_--) {
scanf("%d", &n);
for(int i = 1;i <= n; i++) {
scanf("%lld", &a[i]);
a[i] = a[i] + a[i-1];
}
b[0] = 1e9;
for(int i = 1;i <= n; i++) {
scanf("%lld", &b[i]);
b[i] = min(b[i], b[i-1]);
}

sta.push(make_pair(a[1], b[1]));
for(int i = 2;i <= n; i++) {
pair <long long, long long> t = sta.top(); sta.pop();
if(a[i] > t.first) {
if(b[i] == t.second) sta.push(make_pair(a[i], b[i]));
else {
t.second -= b[i];
sta.push(t);
sta.push(make_pair(a[i], b[i]));
}
}
else sta.push(t);
}

__int128 ans = 0;
while(!sta.empty()) {
__int128 u = sta.top().first, v = sta.top().second;
sta.pop();
ans += u*v;
}

printf("Case #%d: %lld ", ++cas, b[1]);
write(ans); puts("");
}
}


©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客