csust-8..4早训CF之900-2400分

目录

 

A - Memory and Crow

B - Memory and Trident

C - Memory and De-Evolution

D - Memory and Scores

E - Sonya and Queries

F - Sonya and Problem Wihtout a Legend


目前只会ABCEF。。。等我会了就补上QAQ

A - Memory and Crow

 CodeForces - 712A 

题目链接https://codeforces.com/problemset/problem/712/A

#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
#define ll long long
ll a[mac];
int main()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &a[i]);
	int mark = 0;
	ll sum = 0;
	for (int i = n - 1; i >= 1; i--) {
		sum += (mark == 0)?a[i + 1]:-a[i + 1];
		a[i] += (mark == 0) ? sum : -sum;
		mark ^= 1;
	}
	for (int i = 1; i <= n; i++)
		printf("%lld ", a[i]);
	printf("\n");
	return 0;
}

B - Memory and Trident

 CodeForces - 712B 

题目链接https://codeforces.com/problemset/problem/712/B

#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
#define ll long long
int x[mac], y[mac];
char s[mac];
int main()
{
	int n;
	while (scanf("%s", s + 1) != EOF) {
		int len = strlen(s + 1);
		if (len % 2) printf("-1\n");
		else {
			int r = 0, l = 0, u = 0, d = 0;
			for (int i = 1; i <= len; i++) {
				if (s[i] == 'R') r++;
				else if (s[i] == 'L') l++;
				else if (s[i] == 'U') u++;
				else d++;
			}
			int ans = 0;
			if (r != l) {
				if (r < l) swap(r, l);
				if ((r-l) % 2) ans += (r - l) / 2 + 1;
				else ans += (r - l) / 2;
			}
			if (u != d) {
				if (u < d) swap(u, d);
				ans += (u - d) / 2;
			}
			printf("%d\n", ans);
		}
	}
	
	return 0;
}

C - Memory and De-Evolution

 CodeForces - 712C 

题目链接https://codeforces.com/problemset/problem/712/C

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a, b;
	scanf("%d%d", &a, &b);
	if (a < b) swap(a, b);
	int ans = 0, b1, b2, b3;
	b1 = b2 = b3 = b;
	while (1) {
		if (b1 != a) {
			if (b2 + b3 > a) b1 = a, ans++;
			else b1 = b2 + b3 - 1, ans++;
		}
		if (b2 != a) {
			if (b1 + b3 > a) b2 = a, ans++;
			else b2 = b1 + b3 - 1, ans++;
		}
		if (b3 != a) {
			if (b2 + b1 > a) b3 = a, ans++;
			else b3 = b2 + b1 - 1, ans++;
		}
		if (b1 == a && b2 == a && b3 == a) break;
	}
	printf("%d\n", ans);
	return 0;
}

D - Memory and Scores

 CodeForces - 712D 

题目链接https://codeforces.com/problemset/problem/712/D

 

E - Sonya and Queries

 CodeForces - 713A 

题目链接https://codeforces.com/problemset/problem/713/A

#include <bits/stdc++.h>
using namespace std;
const int mac = 1e5 + 10;
char s[50], ss[10];
int vis[1 << 20];
int main()
{
	int n;
	scanf("%d", &n);
	while (n--) {
		scanf("%s%s", ss, s);
		if (ss[0] == '+') {
			int len = strlen(s);
			int sum = 0;
			for (int i = 0; i < len; i++) {
				if (s[i] & 1)  sum = sum << 1 | 1;
				else sum = sum << 1;
			}
			vis[sum]++;
		}
		else if (ss[0] == '-') {
			int len = strlen(s);
			int sum = 0;
			for (int i = 0; i < len; i++) {
				if (s[i] & 1)  sum = sum << 1 | 1;
				else sum = sum << 1;
			}
			vis[sum]--;
		}
		else {
			int len = strlen(s);
			int sum = 0;
			for (int i = 0; i < len; i++) {
				if (s[i] & 1)  sum = sum << 1 | 1;
				else sum = sum << 1;
			}
			printf("%d\n", vis[sum]);
		}
	}
	return 0;
}

F - Sonya and Problem Wihtout a Legend

 CodeForces - 713C 

题目链接https://codeforces.com/problemset/problem/713/C

方法一:O(nlog(n))

#include <bits/stdc++.h>
using namespace std;
const int mac = 2e5 + 10;
#define IOS ios::sync_with_stdio(false)
#define ll long long
const ll inf = 1e18 + 10;
priority_queue<int>q;
ll f[mac];
int a[mac];
int main()
{
	IOS;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		int x = a[i];
		x -= i;
		q.push(x);
		if (q.size() && q.top() > x) {
			ans += q.top() - x;
			q.pop();
			q.push(x);
		}
	}
	cout << ans << endl;
	return 0;
}

方法二:O(n^2)

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false)
#define ll long long
const int mac = 2e5 + 10;
const ll inf = 1e16 + 10;
int a[mac], b[mac];
ll dp[3100][3100];
int main()
{
	IOS;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i] - i, a[i] -= i;
	sort(b + 1, b + 1 + n);
	for (int i = 1; i <= n; i++) {
		ll p = inf;
		for (int j = 1; j <= n; j++) {
			p = min(p, dp[i - 1][j]);
			dp[i][j] = p + abs(a[i] - b[j]);
		}
	}
	ll ans = inf;
	for (int i = 1; i <= n; i++) ans = min(dp[n][i], ans);
	cout << ans << endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值