Codeforces Round #668 (Div. 2)A~C题解

发布于 2020-09-07  0 次阅读


A. Permutation Forgery

题目大意:

给你一个长度为n的数组a,求与其p数组相同的数组b
数组p定义:a的两个相邻元素的和排序后的数组

思路:

直接倒序输出即可

AC Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=2e5 +9;
int n, a[N];
void solve(){
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i];
    for(int i=n; i>=1; i--) cout<<a[i]<<" ";
    cout<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}

B. Array Cancellation

题目大意:

给你一个长度为n的数组a,满足a1 + a2 + ⋯ + an = 0
操作:选择一个i 一个j ,使得i −−,j ++
若i<j则代价为0,否则代价为1,求将数组a的元素全变成0的最小代价

AC Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=2e5 +9;
int n, a[N];
void solve(){
    cin>>n;
    for(int i=1; i<=n; i++) cin>>a[i];
    int sum, sumz, ans;
    sum=sumz=ans=0;
    for(int i=1; i<=n; i++){
        if(a[i]>0)  sumz+=a[i];
        if(a[i]<0){
            sum-=a[i];
            if(sumz>0){
                ans+=min(sumz, -a[i]);
                sumz=max((long long)0, sumz+a[i]);
            }
        }
    }
    cout<<sum-ans<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}

C. Balanced Bitstring

题目大意:

给你一个长度为n的由0和1和?组成的字符串,以及一个长度k
求是否可以将?变成0或者1使得字符串s的每个k段的0和1字符串相同

思路:

考虑第一段0123和第二段1234,其中123是相同的,即只需要使得0和4相同即可,即为同余位置的字符相同即可
最后检查一下0~k的位置的0和1的数量是否满足我们上面的假设即可

AC Code:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
// #define TDS_ACM_LOCAL
const int N=3e5 +9;
int n, k;
string s;
void solve(){
    cin>>n>>k;
    cin>>s;
    int flag=1;
    for(int i=0; i<k; i++){
        char tmp=s[i];
        for(int j=i+k; j<n; j+=k){
            if(s[j]=='?' || tmp==s[j]) continue;
            else if(tmp=='?' && s[j]!='?')   tmp=s[i]=s[j];
            else if(tmp!='?' && s[i]!=s[j])  {flag=0; break;}
        }
        if(!flag)   break;
    }
    if(!flag) {cout<<"NO"<<endl; return ;}
    int ans0=0, ans1=0;
    for(int i=0; i<k; i++){
        if(s[i]=='1')   ans1++;
        if(s[i]=='0')   ans0++;
    }
    if(ans0>k/2 || ans1>k/2)  cout<<"NO"<<endl;
    else            cout<<"YES"<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    int T;
    cin>>T;
    while(T--)  solve();
    return 0;
}


平平无奇的在校大学生