Codeforces Round 920 (Div. 3) A~D题做题分享

分享1月15日Codeforces Round 920 (Div. 3)做题经历(寒假第一场cf

A

A题链接

------------

签到题,分别找出两列数中不相等的值相减,再将所得的数相乘即可。

一开始有点急没有立刻理清思路,可恶

AC代码

#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int a[4][2];
		int i, j;
		for (i = 0; i < 4; i++) {
			for (j = 0; j < 2; j++) {
				cin >> a[i][j];
			}
		}
		int max = -10000, maxx = -10000;
		int min = 100000, minn = 100000;
		for (i = 0; i < 4; i++) {
			if (a[i][0] > max) {
				max = a[i][0];
			}
			if (a[i][0] < min)
				min = a[i][0];
			if (a[i][1] > maxx)
				maxx = a[i][1];
			if (a[i][1] < minn)
				minn = a[i][1];
		}
		cout << abs((maxx - minn) * (max - min)) << endl;
	}
	return 0;
}

------------

B

B题链接

------------

贪心,找规律,卡了好久,一直离答案差一步,最后重新换了一边思路AC的。解题思路是找到a和b两string数组中1->0的个数,再取max便是答案。

AC代码

#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int i;
		string a, b;
		cin >> a >> b;
		int cnt = 0;
		int ans = 0;
		for (i = 0; i < n; i++) {
			if (a[i] != b[i]) {
				if (a[i] == '1') {
					cnt++;
				} else if (b[i] == '1') {
					ans++;
				}
			}
		}
		if (cnt >= ans)
			cout << cnt << endl;
		else
			cout << ans << endl;
	}
	return 0;
}

------------

C

c题链接

------------

简单的时间计算问题,要能够发出所有消息,那么就需要发消息所用点亮最少,如果所需最少电量都比f大那么便是NO,否则是YES.

AC代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int q[N], p[N];
signed main() {
	int t;
	cin >> t;
	while (t--) {
		int n, f, a, b;
		cin >> n >> f >> a >> b;
		int i;
		int sum = 0;
		for (i = 1; i <= n; i++) {
			scanf("%lld", &q[i]);
			if ((q[i] - q[i - 1])*a >= b) {
//				判断两时刻间隔时间中关机更优还是不关机更优
				sum += b;
			} else {
				sum += (q[i] - q[i - 1]) * a;
			}
		}
		if (sum >= f)
			cout << "NO" << endl;
		else
			cout << "YES" << endl;
	}
}

------------

D

D题链接

------------

双指针算法+贪心+排序,思路不好想,但代码比较好理解。

本题作为双指针用法的经典题目,记录备忘。

AC代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 2e5 + 10;
int a[N], b[N];
signed main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		int i;
		for (i = 1; i <= n; i++) {
			scanf("%lld", &a[i]);
		}
		sort(a + 1, a + 1 + n);
		for (i = 1; i <= m; i++) {
			scanf("%lld", &b[i]);
		}
		sort(b + 1, b + m + 1);
		int ans = 0;
		int la = 1, lb = 1, ra = n, rb = m;
		while (la <= ra) {
			int x = abs(a[la] - b[lb]), y = abs(a[la] - b[rb]), z = abs(a[ra] - b[lb]), w = abs(a[ra] - b[rb]);
			if (x >= y && x >= z && x >= w) {
				ans += x;
				la++;
				lb++;
			} else if (y >= x && y >= z && y >= w) {
				ans += y;
				la++;
				rb--;
			} else if (z >= x && z >= y && z >= w) {
				ans += z;
				ra--;
				lb++;
			} else if (w >= x && w >= y && w >= z) {
				ans += w;
				ra--;
				rb--;
			}
		}
		cout << ans << endl;
	}
	return 0;
}
/*
4 6
6 1 2 4
3 5 1 7 2 3

1 2 4 6
1 2 3 3 5 7

*/

------------

posted @ 2024-07-05 17:35  ZhangDT  阅读(0)  评论(0编辑  收藏  举报