12B-Correct Solution
Description
One cold winter evening Alice and her older brother Bob was sitting at home near the fireplace and giving each other interesting problems to solve. When it was Alice’s turn, she told the number n to Bob and said:
—Shuffle the digits in this number in order to obtain the smallest possible number without leading zeroes.
—No problem! — said Bob and immediately gave her an answer.
Alice said a random number, so she doesn’t know whether Bob’s answer is correct. Help her to find this out, because impatient brother is waiting for the verdict.
Input
The first line contains one integer n (0 ≤ n ≤ 109) without leading zeroes. The second lines contains one integer m (0 ≤ m ≤ 109) — Bob’s answer, possibly with leading zeroes.
Output
Print OK if Bob’s answer is correct and WRONG_ANSWER otherwise.
Sample Input
Input
3310
1033
Output
OK
Input
4
5
Output
WRONG_ANSWER
sort一下之後直接找第一個0和第一個非0就OK了
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char a[10010];
char b[10010], c[1010], d[1010];
int main() {
while (scanf("%s%s", a, b) != EOF) {
int len = strlen(a);
sort(a, a + len);
int flag = 0;
for (int i = 0; i < len; i++) {
if (a[i] != '0') {
flag = i;
char t = a[i];
a[i] = a[0];
a[0] = t;
break;
}
}
if (strcmp(a, b) == 0) {
printf("OK\n");
}
else
{
printf("WRONG_ANSWER\n");
}
}
return 0;
}
660C Hard ProcessYou are given an array a with n elements. Each element of a is either 0 or 1.
Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
InputThe first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.
On the second line print n integers aj — the elements of the array a after the changes.
If there are multiple answers, you can print any one of them.
7 1 1 0 0 1 1 0 1
4 1 0 0 1 1 1 1
10 2 1 0 0 1 0 1 0 1 0 1
5 1 0 0 1 1 1 1 1 0 1
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=300005;
int n,k;
int a[maxn],sum[maxn]={0};
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){//10010 01223 5 1
// 01101 01223 mid=3
cin>>a[i];
if(a[i]==1){
sum[i]=sum[i-1];
// ans=max(ans,sum[i]);
}
else{
sum[i]=sum[i-1]+1;
}
a[i]=1-a[i];
}
// for(int i=1;i<=n;i++){
// sum[i]=sum[i-1]+a[i];
// }
int ans=0,res=0;
for(int i=1;i<=n;i++){
int L=i,R=n;
int temp=0;
while(L<=R){
int mid=(L+R)/2;
if(sum[mid]-sum[i-1]-1>k-1){
R=mid-1;
}
else{
L=mid+1;
temp=mid-i+1;
}
}
if(temp>ans){
ans=temp;
res=i;
// cout<<"ans="<<ans<<" "<<"res="<<res<<endl;
}
}
cout<<ans<<endl;
for(int i=res;i<=n;i++){
if(!ans) break;
if(a[i]==1) a[i]=0;
if(!a[i]) ans--;
//cout<<"ans--="<<ans<<endl;//10010 01101 00101
}
for(int i=1;i<=n;i++){
cout<<1-a[i]<<" ";
}
cout<<endl;
return 0;
}