Codeforces #668(div2) A-C题解

本文解析了CodeForces竞赛中的三道题目,包括PermutationForgery、ArrayCancellation和BalancedBitstring,提供了详细的题解思路和AC代码实现。

https://codeforces.com/contest/1405

A. Permutation Forgery

题目大意:

给定一个序列,其中每两项和另做一个序列。要求对原序列重新排序,使得其和序列中的元素大小不变(顺序任意)

题解:

Trick题,只需要把原序列倒过来输出一遍就可以。

AC代码:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
using namespace std;
typedef long long LL;
int T;
const LL inf = 0x3f3f3f3f;
int a[105];
int b[105];
int c[105];
int main() {
    cin >> T;
    int n;
    while(T--) {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        cin >> n;
        for(int i = 0; i < n; i++) {
            cin >> a[i];
        }
        for(int i = n - 1; i >= 0; i-- )
            cout << a[i] << " ";
        cout << endl;
    }
    return 0;
 
}

 

B. Array Cancellation

 

题目大意:

给定一队序列,如果序列中,正数在前,负数在后,可以同时对正数-1,为负数+1,但如果正负数顺序相反,执行这样的操作将花费一个coin,问最终如果想要所有元素为0,需要花费多少。

 

题解:

只需要使用两个标志量模拟即可。一个anspos负责记录当前序列中仍然存留的正数大小,ansneg负责记录未被正数消耗掉,或是无法被正数抵消而残留的负数值,最终输出ansneg即可。

 

AC代码:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
using namespace std;
typedef long long LL;
int T;
const LL inf = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
int a[maxn];
int main() {
    cin >> T;
    int n;
    while(T--) {
        cin >> n;
        for(int i = 0; i < n ; i++) {
            cin >> a[i];
        }
        LL anspos = 0, ansneg = 0;
        for(int i = 0; i < n; i++) {
            if(a[i] >= 0) {
                anspos += a[i];
            }
            else {
                if(anspos) {
                    if(anspos >= abs(a[i])){
                        anspos += a[i];
                    }
                    else {
                        a[i] += anspos;
                        anspos = 0;
                        ansneg += a[i];
                    }
                }else {
                    ansneg += a[i];
                }
            }
        }
        cout << anspos << endl;
 
    }
    return 0;
 
}

 

C. Balanced Bitstring

 

题目大意:

给定一个N长的字符串,试问其能不能满足每个K字串中0和1的数量相等。

 

题解:

首先要发现一个重要规律,若可以满足,则序列必定是一个以K为循环节的循环序列。

因为对于两个相邻K-子序列而言,如N = 10, K = 4;考察S1[0~3]和S2[1~4]

其中S1[1~3]与 S2[1~3]相同,若满足条件,则S1[0]与S2[4]必须相同,因此递推可得,N必以K为循环节。

接下来仅需检查这一点即可。

 

AC代码:

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
using namespace std;
typedef long long LL;
int T;
int n, k, t;
string s;
int main() {
    cin >> T;
    int n;
    while(T--) {
       cin >> n >> k >> s;
       int zero = 0, one = 0;
       bool chk = true;
       for(int i = 0; i < k; i++) {
           int tmp = -1;
           for(int j = i; j < n; j += k) {
               if(s[j] != '?') {
                   if(tmp != -1 && s[j] - '0' != tmp) {
                       chk = false;
                       break;
                   }
                   tmp = s[j] - '0';
               }
           }
           if(tmp != -1) {
               (tmp == 0 ? zero++:one++);
           }
       }
       if(max(zero,one) > k / 2) {
           chk = false;
       }
       cout << (chk? "YES\n" : "NO\n");
    }
    return 0;

}

 

### 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、付费专栏及课程。

余额充值