Gym 102896
A - Almost Balanced Tree
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;
int pos=1;
int dp[N];
int lr[N];
int rr[N];
bool flag;
int DFS(int a,int b){
int res=pos++;
if(b) dp[res]=2,b--;
else dp[res]=1,a--;
int xb=b/2;
int yb=b-b/2;
int k=(yb>xb)*min(a,2);
int xa=k;
int ya=0;
a-=k;
xa+=a/2;
ya+=a-a/2;
if(abs((xa+2*xb)-(ya+2*yb))>1) {
puts("-1");
flag=1;
return -1;
}
if(xa+xb) lr[res]=DFS(xa,xb);
if(ya+yb) rr[res]=DFS(ya,yb);
return res;
}
void solve()
{
int a,b;
cin>>a>>b;
DFS(a,b);
if(!flag){
for(int i=1;i<pos;i++){
cout<<dp[i]<<" "<<lr[i]<<" "<<rr[i]<<endl;
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
C - Color the Tree
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define pb push_back
const int N = 1e5 + 10;
using namespace std;
int dp[25];
vector<int>vp[25];
void solve(){
int n;
cin>>n;
for(int i=2;i<=n;i++) cin>>dp[i];
for(int i=n;i>=2;i--){
vp[i].pb(i);
vector<int>st=vp[dp[i]];
vp[dp[i]].clear();
bool flag=0;
if(vp[i].size()%2==0) flag=1;
vector<int>::iterator it;
for(it=vp[i].begin();it!=vp[i].end();it++){
if(flag){
for(int j=0;j<(int)st.size();j++){
vp[dp[i]].pb(st[j]);
}
}
else{
for(int j=st.size()-1;j>=0;j--){
vp[dp[i]].pb(st[j]);
}
}
vp[dp[i]].pb(*it);
flag^=1;
}
for(int j=0;j<(int)st.size();j++){
vp[dp[i]].pb(st[j]);
}
}
cout<<(int)vp[1].size()<<endl;
for(int i=(int)vp[1].size()-1;i>=0;i--) cout<<vp[1][i]<<" ";
cout<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
E - Easy Measurements
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;
void solve(){
int t;
cin>>t;
while(t--){
ll b,d;
cin>>b>>d;
ll gcd=__gcd(b,d);
ll bb=b*b/gcd;
// b/=gcd;
d/=gcd;
ll tot=0;
cout<<(b-1)/(d)<<endl;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
K - Kate’s 2021 Celebration
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
const int N = 1e5 + 10;
using namespace std;
int dp[N];
void solve(){
int n;
cin>>n;
int minn=INF;
int pos=0;
for(int i=1;i<=n;i++){
string s;
int w;
cin>>w>>s;
memset(dp,0,sizeof(dp));
int len=s.size();
for(int j=0;j<len;j++){
dp[s[j]-'0']++;
}
if(dp[1]>=1&&dp[2]>=2&&dp[0]>=1){
if(minn>w){
minn=w;
pos=i;
}
}
}
cout<<pos<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
M - Miser
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define INF 0x3f3f3f3f
#define pb push_back
const int N = 1e6 + 10;
using namespace std;
vector<int> dp[N];
int tot=0;
int vis[N];
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
int k;
cin>>k;
while(k--){
int x;
cin>>x;
dp[i].pb(x);
}
}
for(int i=n;i>=1;i--){
int maxn=-1;
vector<int>::iterator it;
for(it=dp[i].begin();it!=dp[i].end();it++){
maxn=max(maxn,vis[*it]);
}
tot=max(tot,maxn++);
for(it=dp[i].begin();it!=dp[i].end();it++){
vis[*it]=maxn;
}
}
cout<<tot+1<<endl;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
return 0;
}