Codeforces Round #670 (Div. 2)
A. Subset Mex
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod =1e9+7;
const int maxn =1e5+10;
int a[101];
void solve() {
int n;
cin>>n;
memset(a,0,sizeof(a));
for (int i = 0; i < n; ++i) {
int x;
cin>>x;
a[x]++;
}
int sum=0;
for (int i = 0;a[i]>0; ++i) {
sum++,a[i]--;
}
for (int i=0;a[i];++i) {
sum++;
}
cout<<sum<<'\n';
}
signed main() {
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
B. Maximum Product
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int maxn =1e5+10;
int a[maxn];
void solve() {
int n;
cin>>n;
for (int i = 0; i < n; ++i) {
cin>>a[i];
}
sort(a, a + n);
if(a[n-1]<=0){
int sum=1;
for (int i = n-1; i>n-6; --i) {
sum*=a[i];
}
cout<<sum<<endl;
} else{
int l=0,r=n-2;
int sum=1;
int res=0;
while (res<2){
if(a[l]*a[l+1]>a[r-1]*a[r]){
sum*= a[l]*a[l+1];
l+=2;
} else{
sum*=a[r-1]*a[r];
r-=2;
}
res++;
}
cout<<sum*a[n-1]<<endl;
}
}
signed main() {
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
C. Link Cut Centroids
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod =1e9+7;
const int maxn =1e5+10;
int n,p1,p2,p3,c[maxn];
vector<int > g[maxn];
void dfs(int u,int fa){
c[u]=1;
for(auto v:g[u])
if(v!=fa) dfs(v,u),c[u]+=c[v];
if (!p3){
if (c[u]==1) p1=fa,p2=u;
if (n-c[u]==c[u]) p3=fa;
}
}
void solve() {
cin>>n;
p1=p2=p3=0;
for (int i = 1; i <=n; ++i) g[i].clear(),c[i]=0;
for (int i = n;--i;) {
int u,v;
cin>>u>>v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, -1);
cout<<p1<<' '<<p2<<'\n'<<p2<<' '<<(p3?p3:p1)<<"\n";
}
signed main() {
int _ = 1;
cin >> _;
while (_--) {
solve();
}
return 0;
}
D. Three Sequences
#include <bits/stdc++.h>
#define int ll
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f3f;
const int mod =1e9+7;
const int maxn =1e5+10;
int a[maxn];
int cnt[maxn];
void solve() {
int n,sum=0;
a[0]=0;
cin>>n;
for (int i = 1; i <=n; ++i) cin>>a[i];
for(int i=1;i<=n;i++) {
cnt[i] = a[i] - a[i - 1];
if (cnt[i]>0&&i!=1) sum+=cnt[i];
}
int sx=(cnt[1]-sum)/2;
cout<<max(sum+sx,cnt[1]-sx)<<"\n";
int k;
cin>>k;
while (k--){
int l,r,d;
cin>>l>>r>>d;
if (l==1) cnt[1]+=d;
else {
if (d<0) {
if (cnt[l] > 0) {
sum-=min(cnt[l],-d);
}
}else{
if (cnt[l]+d>=0) sum+=min(cnt[l]+d,d);
}
cnt[l]+=d;
}
if(r+1<=n){
if (d>0){
if (cnt[r+1]>0) sum-=min(cnt[r+1],d);
} else{
if (cnt[r+1]-d>=0) sum+=min(cnt[r+1]-d,-d);
}
cnt[r+1]-=d;
}
sx=(cnt[1]-sum)/2;
cout<<max(sum+sx,cnt[1]-sx)<<"\n";
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int _ = 1;
while (_--) {
solve();
}
return 0;
}