Codeforces Round 709 (Div. 1)

EternalEpic發表於2024-09-27

原題比賽地址

A. Basic Diplomacy

模擬就好,就先隨便取。然後對於大於 \(\lceil m/2\rceil\) 的那個數直接減到正好滿足為止。

const int N = 1e5 + 5;
int T, n, m, k[N], h[N];
vector <int> f[N], g[N];

signed main(void) {
	for (read(T); T; T--) {
		read(n), read(m);
		for (int i = 1; i <= n; i++) g[i].clear(), h[i] = 0;
		int id = 0;
		for (int i = 1; i <= m; i++) {
			read(k[i]); f[i].clear();
			for (int j = 1, x; j <= k[i]; j++) {
				read(x); f[i].push_back(x);
			}
			h[f[i][0]]++; g[f[i][0]].push_back(i); k[i] = f[i][0];
			if (h[f[i][0]] > (m + 1) / 2) id = f[i][0];
		}
		
		if (!id) {
			puts("YES");
			for (int i = 1; i <= m; i++) writeln(k[i], i == m ? '\n' : ' ');
			continue;
		}
		
		int cnt = h[id];
		for (int i = 1; i <= m; i++) {
			if (f[i].size() == 1) continue;
			else if (f[i][0] == id) {
				k[i] = f[i][1];
				cnt--;
				if (cnt <= (m + 1) / 2) break;
			}
		}
		
		if (cnt > (m + 1) / 2) puts("NO");
		else {
			puts("YES");
			for (int i = 1; i <= m; i++) writeln(k[i], i == m ? '\n' : ' ');
		}
	}
	//fwrite(pf, 1, o1 - pf, stdout);
	return 0;
}

B. Playlist

模擬它刪除互質的數的過程,可以用並查集去維護刪除之後連線的過程。所有數壓入佇列中進行有序刪除,可知道複雜度與刪除次數同階。

inline int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a % b);
}

const int N = 1e5 + 5;
int T, n, a[N], f[N];
vector <int> vec; bool del[N];
inline int getf(int u) {
	return f[u] == u ? u : f[u] = getf(f[u]);
}

signed main(void) {
	for (read(T); T; T--) {
		read(n);
		queue <int> q;
		for (int i = 1; i <= n; i++) {
			read(a[i]); f[i] = i; q.push(i);
		}
		while (!q.empty()) {
			int u = q.front(); q.pop();
			if (del[u])	continue;
			int v = (getf(u) + 1) % n;
			if (v == 0)	v = n;
			if (gcd(a[u], a[v]) == 1) {
				q.push(u);
				int f1 = getf(u), f2 = getf(v);
				f[f1] = f2; del[v] = true;
				vec.push_back(v);
			}
		}
		writeln(vec.size(), ' ');
		for (auto x : vec) writeln(x, ' '); puts("");
		vec.clear();
		for (int i = 1; i <= n; i++) a[i] = 0, del[i] = false;
	}
	//fwrite(pf, 1, o1 - pf, stdout);
	return 0;
}

C. Skyline Photo

單調棧處理分界點,然後dp轉移是可以被分界點(區域性最小值)取代或者自己單獨算一張照片,可以用線段樹維護單點修改區間最值。

const int N = 6e5 + 5;
int n, g[N], h[N], b[N];

struct SegmentTree {
	ll t[N << 2];
	inline void build(int pos, int l, int r) {
		t[pos] = -1e18; if (l == r) return;
		int mid = l + r >> 1;
		build(pos << 1, l, mid);
		build(pos << 1 | 1, mid + 1, r);
	}
	
	inline void pushup(int pos) { t[pos] = max(t[pos << 1], t[pos << 1 | 1]); }
	inline void modify(int pos, int l, int r, int x, ll v) {
		if (l == r) return (void)(t[pos] = v);
		int mid = l + r >> 1;
		if (x <= mid) modify(pos << 1, l, mid, x, v);
		else modify(pos << 1 | 1, mid + 1, r, x, v);
		pushup(pos);
	}
	
	inline ll query(int pos, int l, int r, int L, int R) {
		if (L <= l && R >= r) return t[pos];
		int mid = l + r >> 1; ll ret = -1e18;
		if (L <= mid) ret = query(pos << 1, l, mid, L, R);
		if (R > mid) chkmax(ret, query(pos << 1 | 1, mid + 1, r, L, R));
		return ret;
	}
} T;

stack <pii> q; ll dp[N];

signed main(void) {
	read(n);
	for (int i = 1; i <= n; i++) read(h[i]);
	for (int i = 1; i <= n; i++) read(b[i]);
	for (int i = 1; i <= n; i++) {
		while (!q.empty() && q.top().first > h[i]) q.pop();
		if (!q.empty()) g[i] = q.top().second;
		q.push(Mp(h[i], i));
	}
	
	T.build(1, 1, n); dp[1] = b[1];
	T.modify(1, 1, n, 1, dp[1]);
	for (int i = 2; i <= n; i++) {
		if (g[i]) dp[i] = max(dp[g[i]], T.query(1, 1, n, g[i], i - 1) + b[i]);
		else dp[i] = max((ll) b[i], T.query(1, 1, n, 1, i - 1) + b[i]);
		T.modify(1, 1, n, i, dp[i]);
	}
	writeln(dp[n]);
	//fwrite(pf, 1, o1 - pf, stdout);
	return 0;
}

D. Useful Edges

普及組floyed題目?我不想多說。

const int N = 605 , M = 2e5 + 5;

int n, m, dis[N][N] , d[N][N], u[M], v[M] , w[M];

signed main(void) {
	read(n), read(m); Ms(dis, 0x3f);
	for (int i = 1; i <= m; i++) {
		read(u[i]), read(v[i]), read(w[i]);
		dis[u[i]][v[i]] = dis[v[i]][u[i]] = w[i];
	}
	
	for (int i = 1; i <= n; i++) dis[i][i] = 0;
	for (int k = 1; k <= n; k++)
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++) chkmin(dis[i][j], dis[i][k] + dis[k][j]);
	
	int q; read(q);
	for (int i = 1, u, v, l; i <= q; i++) {
		read(u), read(v), read(l);
		for (int j = 1; j <= n; j++) chkmax(d[u][j], l - dis[v][j]);
 	}
 	
 	int ans = 0;
 	for (int i = 1; i <= m; i++)
 	for (int j = 1; j <= n; j++)
		if (dis[j][u[i]] + w[i] <= d[j][v[i]] || dis[j][v[i]] + w[i] <= d[j][u[i]]) { ++ans; break; }	
	writeln(ans);
	return 0;
}

相關文章