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;
}