CF776补题A-D
一、题目分析

解题思路:分析可知,每次operation都会把相邻的两个字符消掉,又因为字符串的长度是奇数,所以一定会把偶的消掉。故如果目标字符出现在奇数位置,即可通过接下来的操作消掉其他字符,使这个字符串成为目标字符。
代码实现:
1 #include "bits/stdc++.h" 2 #define PII pair<int,int> 3 #define rep(i,z,n) for(int i = z;i <= n; i++) 4 #define per(i,n,z) for(int i = n;i >= z; i--) 5 #define ll long long 6 #define db double 7 #define vi vector<int> 8 #define debug(x) cerr << "!!!" << x << endl; 9 using namespace std; 10 inline ll read() 11 { 12 ll s,r; 13 r = 1; 14 s = 0; 15 char ch = getchar(); 16 while(ch < '0' || ch > '9'){ 17 if(ch == '-') 18 r = -1; 19 ch = getchar(); 20 } 21 while(ch >= '0' && ch <= '9'){ 22 s = (s << 1) + (s << 3) + (ch ^ 48); 23 ch = getchar(); 24 } 25 return s * r; 26 } 27 inline void write(ll x) 28 { 29 if(x < 0) putchar('-'),x = -x; 30 if(x > 9) write(x / 10); 31 putchar(x % 10 + '0'); 32 } 33 char s[10010]; 34 int main() 35 { 36 int t; 37 cin >> t; 38 while(t--){ 39 cin >> s; 40 char code; 41 cin >> code; 42 int len = strlen(s); 43 if(len == 1){ 44 if(code == s[0]) 45 cout << "YES" << endl; 46 else 47 cout << "NO" << endl; 48 continue; 49 } 50 bool odd = false; 51 for(int i = 0;i < len;i++){ 52 if(i % 2 == 0 && s[i] == code) 53 odd = true; 54 } 55 if(odd) 56 cout << "YES" << endl; 57 else 58 cout << "NO" << endl; 59 } 60 return 0; 61 }
(2)B题

解题思路:分情况讨论
1.如果a==1的时候可以直接输出r就可以了
2.如果r<a,直接输出r % a + r /a
3.如果r能够整除a,那么这个公式最大的那个值一定是r - 1的时候
4.如果r不能整除a,那么这个公式的最大值一定d = r / a ,(d * a - 1) % a+(d * a - 1) / a;
5.否则默认答案为r /a + r % a
如果情况都成立,那么取个最大值就可以了
代码实现:
#include "bits/stdc++.h" #define PII pair<int,int> #define rep(i,z,n) for(int i = z;i <= n; i++) #define per(i,n,z) for(int i = n;i >= z; i--) #define ll long long #define db double #define vi vector<int> #define debug(x) cerr << "!!!" << x << endl; using namespace std; inline ll read() { ll s,r; r = 1; s = 0; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') r = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); } return s * r; } inline void write(ll x) { if(x < 0) putchar('-'),x = -x; if(x > 9) write(x / 10); putchar(x % 10 + '0'); } int main() { int n; n = read(); while(n--){ int l,r,a; cin >> l >> r >> a; if(a == 1 || r < a) cout << r << endl; else if(l == r)//1 12 8 7 11 5 cout << (r / a) + (r % a) << endl; else{ int d = r / a; int ans = -INT_MAX; ans = max(ans,r % a + r / a); if(r % a == 0) ans = max(ans,d - 1 + a - 1); else{ if(d * a - 1 >= l) ans = max(ans,(d * a - 1) % a + (d * a - 1) / a); } cout << ans << endl; } } return 0; }
C题

解题思路:因为要去最小的n段和,所以直接排个序,取前n段就可以了。
代码实现:
1 #include "bits/stdc++.h" 2 #define PII pair<int,int> 3 #define rep(i,z,n) for(int i = z;i <= n; i++) 4 #define per(i,n,z) for(int i = n;i >= z; i--) 5 #define ll long long 6 #define db double 7 #define vi vector<int> 8 #define debug(x) cerr << "!!!" << x << endl; 9 using namespace std; 10 inline ll read() 11 { 12 ll s,r; 13 r = 1; 14 s = 0; 15 char ch = getchar(); 16 while(ch < '0' || ch > '9'){ 17 if(ch == '-') 18 r = -1; 19 ch = getchar(); 20 } 21 while(ch >= '0' && ch <= '9'){ 22 s = (s << 1) + (s << 3) + (ch ^ 48); 23 ch = getchar(); 24 } 25 return s * r; 26 } 27 inline void write(ll x) 28 { 29 if(x < 0) putchar('-'),x = -x; 30 if(x > 9) write(x / 10); 31 putchar(x % 10 + '0'); 32 } 33 struct node{ 34 int num; 35 int val; 36 int cox; 37 }e[200010],tt[200010]; 38 bool cmp(node a,node b) 39 { 40 return a.val < b.val; 41 } 42 bool cmp1(node a,node b) 43 { 44 return a.cox < b.cox; 45 } 46 int main() 47 { 48 int t; 49 cin >> t; 50 while(t--){ 51 int k,m; 52 cin >> k >> m; 53 rep(i,0,m - 1){ 54 cin >> e[i].cox >> e[i].val; 55 e[i].num = i + 1; 56 } 57 sort(e,e + m,cmp); 58 int ans = 0; 59 int cnt = 0; 60 rep(i,0,k - 1){ 61 ans += e[i * 2].val; 62 ans += e[i * 2 + 1].val; 63 tt[cnt++] = e[i * 2]; 64 tt[cnt++] = e[i * 2 + 1]; 65 } 66 cout << ans << endl; 67 sort(tt,tt + cnt,cmp1); 68 rep(i,0,k - 1){ 69 cout <<tt[i].num << ' ' << tt[k * 2 - 1 - i].num << endl; 70 } 71 } 72 return 0; 73 }
D题

解题思路:既然要我们从1 2 3 4 5 6用循环右移的操作变成所给序列,所以我们可以反过来,让所给序列左移变成1 2 3 4 5 6 即可。
代码实现:
1 #include "bits/stdc++.h" 2 #define PII pair<int,int> 3 #define rep(i,z,n) for(int i = z;i <= n; i++) 4 #define per(i,n,z) for(int i = n;i >= z; i--) 5 #define ll long long 6 #define db double 7 #define vi vector<int> 8 #define debug(x) cerr << "!!!" << x << endl; 9 using namespace std; 10 inline ll read() 11 { 12 ll s,r; 13 r = 1; 14 s = 0; 15 char ch = getchar(); 16 while(ch < '0' || ch > '9'){ 17 if(ch == '-') 18 r = -1; 19 ch = getchar(); 20 } 21 while(ch >= '0' && ch <= '9'){ 22 s = (s << 1) + (s << 3) + (ch ^ 48); 23 ch = getchar(); 24 } 25 return s * r; 26 } 27 inline void write(ll x) 28 { 29 if(x < 0) putchar('-'),x = -x; 30 if(x > 9) write(x / 10); 31 putchar(x % 10 + '0'); 32 } 33 int a[2010]; 34 int dis[2010]; 35 bool ok = false; 36 int move(int k) 37 { 38 int step = 0; 39 while(1){ 40 step++; 41 int temp = a[1]; 42 for(int i = 1;i < k;i++) 43 a[i] = a[i + 1]; 44 a[k] = temp; 45 if(a[k] == k) 46 return step; 47 } 48 } 49 int main() 50 { 51 int t; 52 t = read(); 53 while(t--){ 54 int n; 55 n = read(); 56 rep(i,1,n) 57 a[i] = read(); 58 memset(dis,0,sizeof(dis)); 59 per(i,n,1) { 60 if(a[i] != i) 61 dis[i] = move(i); 62 } 63 rep(i,1,n) cout << dis[i] << ' '; 64 cout << endl; 65 } 66 return 0; 67 }
二、总结
第一点、很久没有写过cf题了,思路有点固化住了。
第二点、发现自己阅读理解能力很差,看个题目都要很久
希望能够早点上个绿。。。。
本文来自博客园,作者:{scanner},转载请注明原文链接:{https://home.cnblogs.com/u/scannerkk/}

浙公网安备 33010602011771号