Educational Codeforces Round 86 (Rated for Div. 2)

Educational Codeforces Round 86 (Rated for Div. 2)

CF 1342A Road To Zero
每次可以选择花费a对x加一或减一或者对y加一或减一
花费b对x和y同时加一或减一
求同时让x和y变为0花费最少

#include 
#include 
#include 
#include 
using namespace std;
int main(){	
    int t;	
    scanf("%d",&t);	
    while(t--){		
    	long long x,y,a,b;		             
    	scanf("%lld%lld%lld%lld",&x,&y,&a,&b);		
    	long long num1=x*a+y*a;		
    	long long num2=b*x+a*abs(y-x);		
		long long num3=b*y+a*abs(y-x);		
		printf("%lld\n",min(num1,min(num2,num3)));	
	}	
	return 0;
}

CF 1342B Binary Period
字符串长度小于等于2或者字符串全为1或者0可以直接输出原字符串,其他情况下可以构造出01循环串

#include 
#include 
#include 
using namespace std;
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		char s[105];
		scanf("%s",s);
		int len=strlen(s);
		if (len<=2)  printf("%s\n",s);
		else{
			int i=0;
			int num=0;
			while(s[i]){
				if (s[i]=='1')  num++;
				i++;
			}
			if (num==len||num==0)  printf("%s\n",s);
			else{
				int j=0;
				while(s[j]){
					if(j==0)  printf("%c",s[j]);
					else{
						if (s[j]!=s[j-1])  printf("%c",s[j]);
						else{
							if(s[j-1]=='0')  printf("1");
							else printf("0");
							printf("%c",s[j]);
						}
					}
					j++;
				}
				printf("\n");
			}
		}
	}
	return 0;
}

CF 1342C Yet Another Counting Problem
打印然后自己推公式题

#include 
#include 
using namespace std;
long long gcd(long long a,long long b){
	return b==0? a:gcd(b,a%b);
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		long long x,y;
		int n;
		scanf("%lld%lld%d",&x,&y,&n);
		long long num=gcd(max(x,y),min(x,y));
		num=x*y/num;
		int flag=0;
		while(n--){
			long long l,r;
			scanf("%lld%lld",&l,&r);
			if (flag)  printf(" ");
			flag=1;
			if (x%y==0||y%x==0)  printf("0");
			else{
				long long num1=l/num;
				long long num2=r/num;
				long long num3=l%num;
				long long num4=r%num;
				long long ans=(num2-num1-1)*max(x,y);
				if (num3<max(x,y))  ans+=(max(x,y)-num3);
				if (num4<max(x,y))  ans+=(num4+1);
				else  ans+=max(x,y);
				printf("%lld",r-l+1-ans);
			}
		}
		printf("\n");
	}
	return 0;
}

剩下的题日后再补题

你可能感兴趣的:(CF刷题)