CF1740 A-F 题解
比赛链接:https://codeforces.com/contest/1740/
题解:
AB
简单题
// by SkyRainWind
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int inf = 1e9, INF = 0x3f3f3f3f;
signed main(){
int te;scanf("%d",&te);
while(te--){int n;scanf("%d",&n);printf("%d\n",n);}
return 0;
}
// by SkyRainWind
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int inf = 1e9, INF = 0x3f3f3f3f,maxn=3e5+5;
pii a[maxn];
int n;
int cmp(pii a,pii b){return a.first>b.first;}
void solve(){
scanf("%d",&n);
LL r=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].first,&a[i].second);
if(a[i].first<a[i].second)swap(a[i].first,a[i].second);
r += 2ll*(a[i].first+a[i].second);
}
sort(a+1,a+n+1,cmp);
for(int i=2;i<=n;i++){
r -= a[i].first * 2ll;
}
printf("%I64d\n",r);
}
signed main(){
int te;scanf("%d",&te);
while(te --)solve();
return 0;
}
C
显然最中间的一个只放1个最优,最左边放1个(显然是第一个或者最后一个),最右边都放,求一下答案
注意一下细节
// by SkyRainWind
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
#define int LL
const int inf = 1e9, INF = 0x3f3f3f3f,maxn=2e5+5;
int n,a[maxn];
void solve(){
scanf("%I64d",&n);
for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
sort(a+1,a+n+1);
if(n==3){
printf("%I64d\n",max(abs(a[2]-a[1])+abs(a[3]-a[1]),abs(a[2]-a[3])+abs(a[1]-a[3])));
return ;
}
int ans=0;
for(int i=1;i<=n;i++){
if(i==1){
int t1 = abs(a[1]-a[2])+abs(a[1]-a[n]);
ans=max(ans,t1);
}else if(i==n){
int t1 = abs(a[n]-a[n-1])+abs(a[n]-a[1]);
ans=max(ans,t1);
}else if(i==2){
int t1 = max(abs(a[2]-a[1]),abs(a[2]-a[3])) + abs(a[2]-a[n]);
ans=max(ans,t1);
}else if(i==n-1){
int t1 = max(abs(a[n-1]-a[n]),abs(a[n-1]-a[n-2])) + abs(a[n-1]-a[1]);
ans=max(ans,t1);
}else{
int t1 = max(abs(a[i]-a[i-1]),abs(a[i]-a[i+1])) + max(abs(a[i]-a[n]),abs(a[i]-a[1]));
ans=max(ans,t1);
}
}
printf("%I64d\n",ans);
}
signed main(){
int te;cin>>te;
while(te--)solve();
return 0;
}
D
key observation:如果去掉(1,1)(n,m)之后还有空位,那么就一定可以经过某些移动,一个纸牌从(1,1) -> (n,m)
显然顺序是n n-1 ...的执行,每次加到set里面,如果没有空位就说明不可以
// by SkyRainWind
#include <cstdio>
#include <set>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int inf = 1e9, INF = 0x3f3f3f3f;
void solve(){
int n,m,k;
set<int>S;
scanf("%d%d%d",&n,&m,&k);
int cnt=0, tp = k, gg=0;
for(int i=1;i<=k;i++){
int p;
scanf("%d",&p);
S.insert(p);
if(i-cnt > n*m-3)gg=1;
if(S.find(tp) != S.end()){
S.erase(tp);
-- tp;
int cur = i - cnt;
if(cur > n*m-3){
gg=1;
}
++ cnt;
}
}
if(gg)puts("TIDAK");
else puts("YA");
}
signed main(){
int te;scanf("%d",&te);
while(te--)solve();
return 0;
}
E
显然从深度大的叶子开始赋1.2.3...答案最优,dp一下即可
// by SkyRainWind
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define mpr make_pair
#define debug() cerr<<"Yoshino\n"
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define pii pair<int,int>
using namespace std;
typedef long long LL;
const int inf = 1e9, INF = 0x3f3f3f3f, maxn=2e5+5;
int n;
int dp[maxn], dep[maxn],p[maxn];
signed main(){
scanf("%d",&n);
for(int i=2;i<=n;i++)scanf("%d",&p[i]);
for(int i=1;i<=n;i++)dep[i]=1,dp[i]=0;
p[1] = -1;
for(int i=n;i>=1;i--){
dp[i] = max(dp[i], dep[i]);
if(i!=1){
dep[p[i]] = max(dep[p[i]],dep[i]+1);
dp[p[i]]+=dp[i];
}
}
printf("%d\n",dp[1]);
return 0;
}