CodeforcesRound#553(Div. 2)(A-D题解)

本文详细解析了CodeForces比赛1151题目的解题思路,包括字符串转换成本最低的问题、矩阵中寻找非零异或和、数列求和的模运算以及人员排列以最小化总愤怒值的策略。通过具体代码示例,深入探讨了暴力模拟、异或操作、模运算技巧及贪心算法的应用。

http://codeforces.com/contest/1151/problem/A

题意:对于任意两个字符a,b,将a变成b的价值为他们的最小距离。其中z到a的距离是1。

   求出将给定字符串变成一个含有 "ACTG" 子串的字符串最小价值。

思路:暴力模拟。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int dis(char a,char b){
 5     if(a > b){char c = a;a = b;b = c;}
 6     int d = b - a;
 7     return min(26 - d,d);
 8 }
 9 
10 int main(){
11     int n;
12     cin >> n;
13     char in[100];
14     scanf("%s",in);
15     int ans = 99999999;
16     int maxx = 0;
17     for(int i(0);i+3<n;++i) {
18         maxx = 0;
19         maxx += dis(in[i],'A');
20         maxx += dis(in[i+1],'C');
21         maxx += dis(in[i+2],'T');
22         maxx += dis(in[i+3],'G');
23         ans = min(maxx,ans) ;
24     }
25     cout << ans << endl;
26 
27     return 0;
28 }
A

 

 

http://codeforces.com/contest/1151/problem/B

题意:给定n行每行m个数字,问是否能在每一行都选出一个数字,使得这些共n个数字的异或和不为零。

思路:先求出某一列的异或和,如果不为0,则找到。

   如果为0,那么就对某一行被选中的数字进行更换,如果更换后的数字和更换前的数字不同,则成功找到。

   如果无论如何更换异或和都是0,那么就是找不到。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int a[505][505];
 5 int b[505];
 6 int main()
 7 {
 8     int n,m;
 9     scanf("%d%d",&n,&m);
10     for(int i = 1;i <= n;i ++){
11         for(int j = 1;j <= m;j ++){
12             scanf("%d",&a[i][j]);
13         }
14     }
15     int ans = 0;
16     for(int i = 1;i <= n;i ++){
17         ans ^= a[i][1];
18         b[i] = 1;
19     }
20     if(!ans){
21         int f = 0;
22         for(int i = 1;i <= n;i ++){
23             int now = a[i][b[i]];
24             f = 0;
25             for(int j = 2;j <= m;j ++){
26                 if(now != a[i][j]){b[i] = j;f = 1;break;}
27             }
28             if(f){break;}
29         }
30         if(!f){
31             printf("NIE\n");
32         }
33         else{
34             printf("TAK\n");
35             for(int i = 1;i <= n;i ++){
36                 if(i != 1){printf(" ");}
37                 printf("%d",b[i]);
38             }printf("\n");
39         }
40     }
41     else{
42         printf("TAK\n");
43         for(int i = 1;i <= n;i ++){
44             if(i != 1){printf(" ");}
45             printf("%d",b[i]);
46         }printf("\n");
47     }
48 
49     return 0;
50 }
B

 

 

http://codeforces.com/contest/1151/problem/C

题意:列出两个数列:

     A:1,3,5,7,9,11,13......

     B:2,4,6,8,10,12,14......

   由上面两个数列得到一个新的数列(一个A数组中的数,两个B数组中的数,四个A数组中的数,八个B数组中的数......):

     C:1,2,4,3,5,7,9,6,8,10,12,14,16,18,20......

   求出任意区间的数字和并取模,总区间范围是 1e18 。

思路:写起来比较别扭,只需要模拟即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 long long mod = 1e9 + 7;
 5 long long F(long long x){
 6     long long ans = 0;
 7     long long now = 1; /// %2
 8     long long sum = x;
 9     long long len = 1;
10     long long j = 1;
11     long long o = 2;
12     while(sum){
13         sum -= len;
14         if(now){
15             ans += ((len % mod) * ((len + j - 1) % mod));
16             ans %= mod;
17             j += ((len * 2) % mod);
18             j %= mod;
19         }
20         else{
21             ans += ((len % mod) * ((len + o - 1) % mod));
22             ans %= mod;
23             o += ((len * 2) % mod);
24             o %= mod;
25         }
26         now = 1 - now;
27         len = min(len * 2,sum);
28     }
29     return ans % mod;
30 }
31 
32 int main(){
33     long long l,r;
34     cin >> l >> r;
35     ///cout << F(r) << " " << F(l - 1) << endl;
36     long long ans = F(r) - F(l - 1);
37     ans = (((ans % mod) + mod) % mod);
38     cout << ans << endl;
39 
40     return 0;
41 }
C

 

 

http://codeforces.com/contest/1151/problem/D

题意:给定n个人,每个人有两个权值a和b。要将这些人排队,如果一个人被排在第 i 个位置。那他的愤怒值是 a*(i - 1) + b*(n - j)。

   求出如何排队使得总愤怒值最小。

思路:贪心,一个人的 b - a 越小,这个人越靠前。排序即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Node{
 5     long long a,b;
 6 }p[100005];
 7 
 8 bool cmp(Node x,Node y){
 9     return x.b - x.a < y.b - y.a;
10 }
11 
12 int main(){
13     int n;
14     cin >>n;
15     for(int i = 0;i < n;i ++){
16         cin >> p[i].a >> p[i].b;
17     }
18     sort(p,p + n,cmp);
19     long long ans = 0;
20     for(int i = 0;i < n;i ++){
21         ans += (p[i].a * i) + (p[i].b * (n - 1 - i));
22     }
23     cout << ans << endl;
24 
25     return 0;
26 }
D

 

转载于:https://www.cnblogs.com/love-fromAtoZ/p/10739317.html

### Codeforces Round 1005 Div. 2 A-F Problem Solutions #### A. Money Change 为了处理货币转换问题,可以将所有的金额都转化为分的形式来简化计算。通过遍历输入数据并累加各个部分的金额,最后求得剩余的钱数并对100取模得到最终结果[^2]。 ```cpp #include <iostream> using namespace std; int main() { int s, xi, yi; cin >> s; int total_cents = 0; for (int i = 0; i < s; ++i) { cin >> xi >> yi; total_cents += xi * 100 + yi; } cout << (s * 100 - total_cents) % 100 << endl; } ``` #### B. Odd and Even Pairs 此题目要求找到至少一对满足条件的索引:要么是一个偶数值的位置,或者是两个奇数值位置。程序会读入测试次数`t`以及每次测试中的数组长度`n`及其元素,并尝试找出符合条件的一对索引输出;如果没有这样的组合则返回-1[^3]。 ```cpp #include <cstdio> int main() { int t, n, num; scanf("%d", &t); while (t--) { int evenIndex = 0, oddIndex1 = 0, oddIndex2 = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &num); if (num % 2 == 0 && !evenIndex) evenIndex = i; else if (num % 2 != 0) { if (!oddIndex1) oddIndex1 = i; else if (!oddIndex2) oddIndex2 = i; } if ((evenIndex || (oddIndex1 && oddIndex2))) break; } if (evenIndex) printf("1\n%d\n", evenIndex); else if (oddIndex1 && oddIndex2) printf("2\n%d %d\n", oddIndex1, oddIndex2); else printf("-1\n"); } return 0; } ``` 由于仅提供了前两道题的具体描述和解决方案,在这里无法继续给出完整的C至F题解答。通常情况下,每一道竞赛编程题都有其独特的挑战性和解决方法,建议查阅官方题解或社区讨论获取更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值