Codeforces Round #600 (Div. 2)

A
记录除0的相同权值联通块个数

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
#define endl '\n'
int a[maxn], b[maxn];
 
int main(int argc, char const *argv[])
{
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false); cin.tie(0);
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		bool flg = true; int pre = 0;
		for (int i = 1; i <= n; ++i) cin >> a[i];
		for (int i = 1; i <= n; ++i) {
			cin >> b[i]; b[i] -= a[i];
			if (b[i] < 0) flg = false;
		}
		if (!flg) cout << "No" << endl;
		else {
			int cnt = 0;
			for (int i = 1; i <= n; ++i) {
				if (b[i] > 0 && b[i - 1] == 0) cnt++;
				else if (b[i] > 0 && b[i - 1] > 0 && b[i] != b[i - 1]) cnt++;
			}
			if (cnt > 1) cout << "No" << endl;
			else cout << "Yes" << endl;
		}
	}
	return 0;
}

b
办公室没有人即分组,用map记录多次进入的情况

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
#define endl '\n'
#define f(x) x + maxn
int in[maxn], a[maxn], b[maxn];
int vis[maxn];
int main(int argc, char const *argv[])
{
	//freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false); cin.tie(0);
	int n; 
	while (cin >> n) {
		vector<int> ans;
		for (int i = 1; i <= n; ++i) 
			cin >> in[i];
		for (int i = n; i >= 1; --i) {
			if (in[i] > 0) a[i] = b[in[i]];
			else b[-in[i]] = i;
		}
		bool flg = true;
		map<int, int> est;
		for (int i = 1, l = -1, r = 0; i <= n && flg; ++i) {
			if (i == r) {
				ans.push_back(r - l + 1);
				l = -1; r = 0;
				est.clear();
			}
			if (in[i] > 0) {
				if (l == -1) {
					l = i, r = a[i];
					est[in[i]] = 1;
				}
				else {
					r = max(r, a[i]);
					if (est.count(in[i])) flg = false;
					else est[in[i]] = 1;
				}
				if (a[i] == 0) flg = false;
				if (vis[a[i]]) flg = false;
				vis[a[i]] = 1;
			}
			else if (!vis[i]) flg = false;
		}
		if (!flg) cout << -1 << endl;
		else {
			cout << ans.size() << endl;
			for (int i = 0; i < ans.size(); ++i) {
				if (i == 0) cout << ans[i];
				else cout << " " << ans[i];
			}
			cout << endl;
		}
	}
}

c
先排序
令ans[i]为人数为i的答案,则ans[i] = sum(1, i)+ans[i-m](还需要多计算一次除前m个数的贡献)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
int a[maxn];
ll ans[maxn], pre[maxn];
 
int main()
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_stdio(false); cin.tie(0);
	int n, m; scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i) 
		scanf("%d", &a[i]);
	sort(a + 1, a + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans[i] = pre[i] = pre[i - 1] + a[i];
		if (i > m)
			ans[i] += ans[i - m];
		printf("%lld ", ans[i]);
	}
	printf("\n");
}

d
1-2-5-7 3-4-6-8 11-12
记录每个联通块的大小和左端点和右端点,按左端点-右端点顺序排序后,如果两个联通块有交叉,合并代价为1,没有交叉代价为联通块范围-大小(没有访问的点)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 2e5 + 5;
vector<int> e[maxn];
struct node {
	int minv, maxv, cnt;
	node() {}
	node(int _minv, int _maxv, int _cnt) {
		minv = _minv;
		maxv = _maxv;
		cnt = _cnt;
	}
	bool operator <(const node& ret) const {
		if (minv != ret.minv) return minv < ret.minv;
		if (maxv != ret.maxv) return maxv < ret.maxv;
		return cnt < ret.cnt;
	}
};
int fa[maxn], l[maxn], r[maxn], c[maxn];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
vector<node> blk;
int vis[maxn];
 
int notice[maxn];
int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	//freopen("in.txt", "r", stdin);
	int n, m; cin >> n >> m;
	for (int i = 1; i <= n; ++i) fa[i] = l[i] = r[i] = i, c[i] = 1;
	for (int i = 0, u, v, fu, fv; i < m; ++i) {
		cin >> u >> v;
		notice[u] = notice[v] = 1;
		fu = find(u), fv = find(v);
		if (fu == fv) continue;
		
		l[fu] = min(l[fu], l[fv]); r[fu] = max(r[fu], r[fv]);
		c[fu] += c[fv];
		fa[fv] = fu;
	}
	for (int i = 1, rt; i <= n; ++i) {
		if (!notice[i]) continue;
		if (vis[rt = find(i)]++) continue;
 
		blk.push_back(node(l[rt], r[rt], c[rt]));
	}
	sort(blk.begin(), blk.end());
	int minv = blk[0].minv, maxv = blk[0].maxv, cntv = blk[0].cnt, ans = 0;
	for (int i = 1, x, y, z; i < blk.size(); ++i) {
		if (blk[i].minv < maxv) {
			maxv = max(maxv, blk[i].maxv);
			cntv += blk[i].cnt;
			ans++;
			continue;
		}
		ans += maxv - minv + 1 - cntv;
		minv = blk[i].minv;
		maxv = blk[i].maxv;
		cntv = blk[i].cnt;
	}
	ans += maxv - minv + 1 - cntv;
	cout << ans << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值