Codeforces Round #516 (Div. 2, by Moscow Team Olympiad)
阿新 • • 發佈:2018-11-25
A. Make a triangle!
題意:讓某段最少增加多少使得構成三角形
思路:讓較小兩段往最長段去湊
#include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl; using namespace std; int a[5]; int main(){ scanf("%d%d%d",&a[1],&a[2],&a[3]); sort(a+1,a+1+3); if(a[1]+a[2] > a[3])puts("0"); else printf("%d\n",a[3]-a[2]-a[1]+1); return 0; }
B. Equations of Mathematical Magic
題意:存在多少種x使得x與a滿足題式
思路:打表發現與a的二進位制表示中1的數量有關
#include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl; using namespace std; typedef long long LL; int t,n; LL pw[35]; int main(){ scanf("%d",&t); pw[0]=1; for(int i=1;i<35;i++)pw[i]=2LL*pw[i-1]; while(t--){ scanf("%d",&n); int cnt=0; for(int i=0;i<=31;i++)if((1<<i)&n)cnt++; printf("%lld\n",pw[cnt]); } return 0; }
C. Oh Those Palindromes
題意:用原串字元構造出子串中迴文串最多的串
思路:讓相同字元相鄰必定為最優情況
#include <bits/stdc++.h> #define cerr << #x << " = " << x << endl; const int maxn = 1e5+5; using namespace std; int n,cnt[30]; char s[maxn]; int main(){ scanf("%d%s",&n,s+1); for(int i=1;i<=n;i++)cnt[s[i]-'a']++; for(int i=0;i<26;i++){ for(int j=0;j<cnt[i];j++)printf("%c",i+'a'); }puts(""); }
D. Labyrinth
題意:從某點出發,左右移動次數有限,最多能到達幾個合法點
思路:優先進行上下移動,用雙端佇列模擬bfs
#include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl; const int maxn = 2e3+5; using namespace std; int n,m,sr,sc,lm,rm; int tot,vis[maxn][maxn]; int tr[4]={1,-1,0,0},tc[4]={0,0,1,-1}; char mp[maxn][maxn]; struct node{ int r,c,t1,t2; }; void bfs(){ deque<node>Q; vis[sr][sc]=1; Q.push_back({sr,sc,lm,rm}); while(!Q.empty()){ node tmp=Q.front(); Q.pop_front(); for(int i=0;i<4;i++){ node now=tmp; now.r+=tr[i],now.c+=tc[i]; if(now.r >= 1 && now.r <= n && now.c >= 1 && now.c <= m && !vis[now.r][now.c] && mp[now.r][now.c] == '.'){ if(i < 2){tot+=vis[now.r][now.c]=1;Q.push_front({now.r,now.c,now.t1,now.t2});} else{ if(tc[i] == -1 && now.t1){ tot+=vis[now.r][now.c]=1; Q.push_back({now.r,now.c,now.t1-1,now.t2}); } if(tc[i] == 1 && now.t2){ tot+=vis[now.r][now.c]=1; Q.push_back({now.r,now.c,now.t1,now.t2-1}); } } } } } } int main(){ scanf("%d%d%d%d%d%d",&n,&m,&sr,&sc,&lm,&rm); for(int i=1;i<=n;i++)scanf("%s",mp[i]+1); bfs(); printf("%d\n",tot+1); return 0; }
E. Dwarves, Hats and Extrasensory Abilities
題意:互動題,每次詢問一個點的顏色,最後用一條直線將兩種顏色的點分開,不存在這種直線也會WA
思路:先詢問座標上的點,二分列舉剩餘點,顏色與第一點相同就l=mid,否則r=mid,等於把與第一點相同色的點分在一邊,剩下的分在另一邊
#include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl; const int maxn = 35; using namespace std; int n; string str[maxn]; int main(){ cin >> n; cout << "0 2\n"; fflush(stdout); cin >> str[1]; int l=1,r=(int)1e9,mid; for(int i=2;i<=n;i++){ mid=(l+r)/2; cout << mid << ' ' << 2 << endl; fflush(stdout); cin >> str[i]; (str[i][0] == str[1][0]) ? l=mid : r=mid; } cout << l << ' ' << 3 << ' ' << r << ' ' << 1 << endl; fflush(stdout); return 0; }
F. Candies for Children
留坑,寒假補