Codeforces Round #668 (Div. 2)题解ABC

A. Permutation Forgery

题目传送门

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

题目传送门

Array Cancellation

题目大意

给你一个长度为n的数组a,满足 a 1 + a 2 + ⋯ + a n = 0 a_1+a_2+⋯+a_n=0 a1+a2++an=0
操作:选择一个 i i i一个 j j j,使得 i − − i-- i j + + j++ 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

题目传送门

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值