2022SCUT Winter Training Day 12

A. Min Cost String

解法:根据样例1猜测出构造方式,以k=4为例,构造成[aabacad][bbcbd][ccd][d]这种形式,这样能保证cost恰好为0。如果长度不够,就继续循环,这样就是最小cost。

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
int n, k; string s;

signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=0; i<k; i++){
        s+='a'+i;
        for(int j=i+1; j<k; j++){
            s+='a'+i;
            s+='a'+j;
        }
    }
    while(s.size() < n) s+=s;
    s.resize(n);
    cout<<s;
}

D. GCD Length

解法:考虑构造,用p-n(质数-次数)图表示的话,gcd是x和y的交集。我们只考虑质数2和3,让gcd是6的倍数,凑出c位长度。然后x和y分别在gcd基础上分别不断x2,x3,直到凑出a,b位长度。

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
#define int long long
int t,a,b,c,low[10];

inline void init(){
    low[1]=1;
    for(int i=2; i<=9; i++) low[i]=low[i-1]*10;
}
inline void solve(){
    int gcd = 1;
    while(gcd < low[c]) gcd*=6;
    int x=gcd, y=gcd;
    while(x < low[a]) x*=2;
    while(y < low[b]) y*=3;
    cout<<x<<' '<<y<<'\n';
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    init();
    cin>>t; while(t--) {cin>>a>>b>>c; solve();}
}

E. 3-Coloring 

解法:首先考虑只有1,2的情况下如何放,只要(i+j)为奇数的部分放1,为偶数的放2即可,这样即可交错放置。当1,2中其中一个放满了,另一个被禁了的时候,才考虑放3。注意,只要其中一个放满了,另外两个数字随便放都不会产生冲突(自己画图想想,很容易理解的)。

补充:注意输出格式,题目有说明。代码里用的是endl,也可以清空缓冲区。

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
const int N = 102;
int n,x,vis[N][N];

inline bool put(int x){
    for(int i=1; i<=n; i++)
    for(int j=1; j<=n; j++)
    if((i+j)%2+1==x || x==3){
        if(!vis[i][j]){
            vis[i][j] = 1;
            cout<<x<<' '<<i<<' '<<j<<endl;
            return 1;
        }
    }
    return 0;
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1; i<=n*n; i++){
        cin>>x;
        if(x==1){if(!put(2)) put(3);} //尝试放1,如果放不了就放2
        else if(x==2) {if(!put(1)) put(3);}
        else {if(!put(1)) put(2);}
    }
}

J. Yet Another Card Deck 

解法:签到题。容易发现,同一数字,只有第一个数字对结果有影响,所以用数组p记录每个数字第一个位置,然后按题意模拟即可。

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
const int N = 3e5+5;
int n,q,p[52]; //p[x]为数字x的最高位置

signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    memset(p,0x3f3f3f3f,sizeof(p));
    cin>>n>>q; int x;
    for(int i=1; i<=n; i++) cin>>x, p[x]=min(p[x],i);
    for(int i=1; i<=q; i++){
        cin>>x; cout<<p[x]<<' ';
        for(int y=1; y<=50; y++)
            if(p[y]<p[x]) p[y]++;
        p[x]=1;
    }
}

 K. Déjà Vu

解法:首先判断是否能变成非回文,手算模拟几个数据,可猜出当且仅当所有字母都是a的时候才不能变成非回文。重点是构造非回文,我的方法是:找到第一个不是'a'的字母,在它的对称位置放'a',那就一定不是回文。(注意遍历位置和插入位置需要分类讨论)

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
int t; string s;

inline void solve(){
    int len = s.size();
    for(int i=0; i<len; i++)
        if(s[i]!='a'){
            if(len-i-1>=i) s.insert(len-i,1,'a');
            else s.insert(len-i-1,1,'a');
            cout<<"YES"<<'\n'<<s<<'\n'; return;
        }
    cout<<"NO"<<'\n';
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--){
        cin>>s;
        solve();
    }
}

L. Balance the Bits 

解法:见注释

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"!!!"<<'\n';
const int N = 2e5+5;
int t,n; char s[N];

inline void solve(){
    int n0=0,n1=0,c0=0,c1=0;
    if(s[1]!='1' || s[n]!='1') {cout<<"NO"<<'\n'; return;}
    for(int i=1; i<=n; i++){
        if(s[i]=='0') n0++;
        else n1++;
    }
    if(n0%2) {cout<<"NO"<<'\n'; return;}
    //到这里说明已经成功
    cout<<"YES"<<'\n';
    string a,b;
    for(int i=1; i<=n; i++){
        if(s[i]=='1'){ //是1,左半边左括号,右半边右括号
            if(c1<n1/2) a+='(', b+='(';
            else a+=')', b+=')';
            c1++;
        } else { //是0,交替加左右括号
            if(c0%2) a+='(', b+=')';
            else a+=')', b+='(';
            c0++;
        }
    }
    cout<<a<<'\n'<<b<<'\n';
}
signed main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>t; while(t--){
        cin>>n>>(s+1);
        solve();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值