Codeforces Round #705 (Div. 2)——A~C

A. Anti-knapsack

在这里插入图片描述
Example
input
3
3 2
5 3
1 1
output
2
3 1
3
4 5 2
0

code:

#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--){
        int n, k, tot=0;
        cin>>n>>k;
        int ans[n+10];
        for(int i=k+1; i<=n; i++) ans[++tot]=i;
        for(int i=k-1; i>=1; i--) {
            if( i>=k-i ) ans[++tot]=i;
            else break;
        }
        cout<<tot<<endl;
        for(int i=1; i<=tot; i++) cout<<ans[i]<<" ";
        cout<<endl;
    }
    return 0;
}

B. Planet Lapituletti

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
Example
input
5
24 60
12:21
24 60
23:59
90 80
52:26
1 100
00:01
10 10
04:04
output
12:21
00:00
52:28
00:00
00:00

题解:
镜像串,很容易想到用一个数组来判断。比赛的时候我不知道有printf("%02d",…),绕了个大弯

code:

#include <string.h>
#include <math.h>
#include<algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
typedef long long ll;
const int mor[10]={0, 1, 5, -1, -1, 2, -1, -1, 8, -1 };
bool jg(int x, int time){
    int a=x/10;
    int b=x%10;
    if( mor[a]!=-1 && mor[b]!=-1 && mor[b]*10+mor[a]<time) return 1;
    else return 0;
}
int main()
{
    cin.tie(0);std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    for(;t--;){
        int h, m, x, y;
        char c;
        cin>>h>>m;
        cin>>x>>c>>y;
        bool ok1=jg(x,m), ok2=jg(y,h);
        if( ok1 && ok2 )  { printf("%02d:%02d\n",x,y); continue; }
        while( ok2==0 && ++y<m )  ok2=jg(y,h);
        if(ok2==0) { y=0; x++; }
        if(x>=h) x=0;
        ok1=jg(x,m);
        if(ok1==0) y=0;
        while( ok1==0 && ++x<h )  ok1=jg(x,m);
        if(ok1==0) { x=0; y=0; }
        printf("%02d:%02d\n",x,y);
    }
    return 0;
}

C. K-beautiful Strings

在这里插入图片描述
Example
input
4
4 2
abcd
3 1
abc
4 3
aaaa
9 3
abaabaaaa
output
acac
abc
-1
abaabaaab

题解:
做多的存在简单的数学规律的,就有点忘了最基本的枚举、贪心、暴力。题还是做少了…

code:

#include <string>
#include <string.h>
#include <math.h>
#include<algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=3e5+10;
int cnt[30], n, k;
string s;
int get(int x){ return (k-x%k)%k; }
int main()
{
	int t; 
	cin>>t;
	while(t--)
	{
		memset(cnt, 0, sizeof(cnt));
		cin>>n>>k>>s;
		for(int i=0; i<n; i++)  cnt[s[i]-'a']++;
		int sum=0, flag=1;
		for(int i=0; i<=25; i++)  sum+=get(cnt[i]);
		if(sum==0)	cout<<s<<endl;
		else if(n%k)  cout<<-1<<endl;	
		else
		{
			for(int i=n-1; i>=0; i--)
			{
				sum=sum-get( cnt[s[i]-'a'] )+get( --cnt[s[i]-'a'] );
				for(int j=s[i]-'a'+1; j<=25; j++)
				{
					int las=sum;
					sum=sum-get( cnt[j] )+get( ++cnt[j] );
					if( (i+1)+sum<=n )
					{
						for(int pos=0; pos<i; pos++)  cout<<s[pos];
						cout<<char( j+'a' );
						for(int pos=1; pos<=n-sum-(i+1); pos++)	 cout<<'a';
						for(int w=0; w<=25; w++)
						{
							int f=get( cnt[w] );
							while(f--)  cout<<char( w+'a' );
						}
						cout<<endl; 
						flag=0;
						break;
					}
					cnt[j]--; 
					sum=las;	
				}
				if(flag==0)	break; 
			}
		}
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值