Problem A :
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int main(){
int n,a[6],b[6];
scanf("%d",&n);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<n;i++){
int t;
scanf("%d",&t);
a[t]++;
}
for(int i=0;i<n;i++){
int t;
scanf("%d",&t);
b[t]++;
}
int res=0,f=0,c=0;
for(int i=1;i<=5;i++){
int t=a[i]-b[i];
if(t%2!=0){
f=1;
break;
}else{
res += t/2;
c += abs(t/2);
}
}
if(f || res!=0) printf("-1");
else printf("%d",c/2);
printf("\n");
return 0;
}
Problem B :
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define maxn 2000000005
int main(){
string str;
int k;
cin>>str>>k;
int len = str.length();
if(str=="0"){
printf("0\n");
return 0;
}
int Z=0,N=0;
for(int i=len-1;i>=0;i--){
if(Z==k){
break;
}
if(str[i]=='0') Z++;
else N++;
}
if(Z==k) cout<<N<<endl;
else printf("%d\n",len-1);
return 0;
}
Problem C :
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 200005;
struct Node{
int c,index;
};
bool check(Node a,Node b){
return a.c>b.c;
}
int main(){
int n,k,a[maxn],b[maxn];
Node c[maxn];
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++) scanf("%d",&b[i]);
for(int i=0;i<n;i++){
c[i].c = b[i]-a[i];
c[i].index = i;
}
sort(c, c+n, check);
int res=0;
for(int i=0;i<k;i++)
res += a[c[i].index];
for(int i=k;i<n;i++)
if(c[i].c>0){
res += a[c[i].index];
}else{
res += b[c[i].index];
}
cout<<res<<endl;
return 0;
}
Problem D:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define F(i,a,b) for(int i=a;i<=b;i++)
#define D(i,a,b) for(int i=a;i<b;i++)
const int maxn = 200005;
string a,b;
int len,lenl, num[maxn],vis[maxn];
int check(int mid){
F(i,1,len) vis[i]=0;
F(i,mid+1,len) vis[num[i]]=1;
int cnt = 0;
D(i,0,len){
if(!vis[i+1]) continue;
if(a[i]==b[cnt]) cnt++;
if(cnt>=lenl) return 1;
}
return 0;
}
int main(){
while (cin>>a>>b) {
len = (int) a.length();
lenl = (int) b.length();
F(i,1,len) scanf("%d",&num[i]);
int l=0,r=len-1,mid,ans;
while (l<=r) {
mid = (l+r)>>1;
check(mid)?l=mid+1,ans=mid : r=mid-1;
}
cout<<ans<<endl;
}
}