2020 年百度之星·程序设计大赛 - 初赛三题解1,2,3,4

A

直接按样例写即可
代码:

#include
#define For(i, s, e) for(int i = s; i < e; i++)
#define For_(i, s, e) for(int i = s; i > e; i++)
#define sd1(i) scanf("%d", &i)
#define sf1(i) scanf("%lf", &i)
#define sl1(i) scanf("%lld", &i)
#define sd2(i, j) scanf("%d%d", &i, &j)
#define sl2(i, j) scanf("%lld%lld", &i, &j)
#define sd3(i, j, k) scanf("%d%d%d", &i, &j, &k)
#define ULL unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
using namespace std;
#define LL long long

int T;
int main(){
	sd1(T);
	while(T--){
		int n;
		sd1(n);
		int b[n];
		double c[n], v[n];
		double maxx = 0.0;
		For(i, 0, n){
			sd1(b[i]);
			sf1(c[i]);
			v[i] = (1.0-c[i]) / (double)(b[i] + 1.0-c[i]);
			if(v[i] > maxx)
				maxx = v[i];
		}
		printf("%.5lf\n", maxx);
		
	}
}

B

由题意
看看x是不是大于一
按概率50 40算概率即可看出
大于1交换 小于1不换
代码:

#include
#define For(i, s, e) for(int i = s; i < e; i++)
#define For_(i, s, e) for(int i = s; i > e; i++)
#define sd1(i) scanf("%d", &i)
#define sf1(i) scanf("%lf", &i)
#define sl1(i) scanf("%lld", &i)
#define sd2(i, j) scanf("%d%d", &i, &j)
#define sl2(i, j) scanf("%lld%lld", &i, &j)
#define sd3(i, j, k) scanf("%d%d%d", &i, &j, &k)
#define ULL unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
using namespace std;
#define LL long long

int T;
int main(){
	sd1(T);
	while(T--){
		double p;
		sf1(p);
		if(p > 1.0)
			printf("No\n");
		else{
			double ex = (p*2 + p/2) / 2;
			if(ex > p)
				printf("Yes\n");
			else
				printf("No\n");
		}
	}
}

C

有两头到中间交换 m为最大交换次数,不是必须的
可以看出首尾交换一次 增加 (m-1 + m-2) , 第二次 增加 (m-3 + m-4),由此可以得到式子 :
ans = 2mn - 2m(1+2*m)/2;
最多操作n/2次
代码:

#include
#define For(i, s, e) for(int i = s; i < e; i++)
#define For_(i, s, e) for(int i = s; i > e; i++)
#define sd1(i) scanf("%d", &i)
#define sf1(i) scanf("%lf", &i)
#define sl1(i) scanf("%lld", &i)
#define sd2(i, j) scanyouf("%d%d", &i, &j)
#define sl2(i, j) scanf("%lld%lld", &i, &j)
#define sd3(i, j, k) scanf("%d%d%d", &i, &j, &k)
#define ULL unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
using namespace std;
#define LL long long

int T;

int main(){
	LL n, m, ans;
	sl1(T);
	while(T--){
		sl2(n, m);
		LL flag = 0;
		if(n == 1){
			ans = 0;
			printf("%lld\n", ans-flag);
			continue;
		}
		
		if(m > n/2  && n > 1){		
			/*if((m - n/2)%2 == 1)
				flag = 1;*/
			m = n/2;
		}
		ans = 2*m*n - 2*m*(1+2*m)/2;
		printf("%lld\n", ans-flag);
	}
}
//

D

每输入一次计算他出路口最快时间 并更新 若有重复则加一
代码:

#include
#define For(i, s, e) for(int i = s; i < e; i++)
#define For_(i, s, e) for(int i = s; i > e; i++)
#define sd1(i) scanf("%d", &i)
#define sf1(i) scanf("%lf", &i)
#define sl1(i) scanf("%lld", &i)
#define sd2(i, j) scanf("%d%d", &i, &j)
#define sl2(i, j) scanf("%lld%lld", &i, &j)
#define sd3(i, j, k) scanf("%d%d%d", &i, &j, &k)
#define ULL unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
using namespace std;
#define LL long long

int T;
int x[100001];
int y[100001];
int d[100001];
vector<int> f;
vector<int> s;
int main(){
	sd1(T);
	while(T--){
		int n;
		sd1(n);
		memset(d, 0, sizeof(d));
		int ans = 0;
		For(i, 0, n){
			sd2(x[i], y[i]);
			int a = 0;
			//ans = 0;
			if(x[i] == 1){
				a = y[i] + 1;
				d[a]++;
			}
			else if(x[i] == 2){
				a = y[i] + 2;
				d[a]++;
			}
			//cout << "a=" << a << endl;
			if(ans == a){
				ans+=d[a]-1;
				//ans++;
				//cout << "ans=" << ans << endl;
			}
			else if(a > ans){
				ans = a;
				//cout << "ans=" << ans << endl;
			}
		
			
			//cout << "ans=" << ans << endl;
		}
		cout << ans << endl;
	}
}

你可能感兴趣的:(比赛题解)