程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces Round #264 (Div. 2)

Codeforces Round #264 (Div. 2)

編輯:C++入門知識

Codeforces Round #264 (Div. 2)


Codeforces Round #264 (Div. 2)

題目鏈接

A:注意特判正好的情況,其他就一個個去判斷記錄最大值即可

B:掃一遍,不夠的用錢去填即可,把多余能量記錄下來

C:把主副對角線處理出來,然後黑格白格只能各選一個最大的放即可

D:轉化為DAG最長路問題,每個數字記錄下在每個序列的位置,如果一個數字能放上去,那麼肯定是每個序列上的數字都是在之前最末尾數字的後面

E:大力出奇跡,預處理出樹,然後每次查詢從當前位置一直往上找到一個符合的即可,如果沒有符合的就是-1

代碼:

A:

#include 
#include 
#include 
using namespace std;

int n, s;

int main() {
	scanf("%d%d", &n, &s);
	int x, y;
	int flag = 1;
	int ans = 0;
	for (int i = 0; i < n; i++) {
		scanf("%d%d", &x, &y);
		if (x == s) {
			if (y == 0) {
				ans = max(ans, y);
				flag = 0;
			}
		}
		else if (x < s) {
			ans = max(ans, (100 - y) % 100);
			flag = 0;
		}
	}
	if (flag) printf("-1\n");
	else printf("%d\n", ans);
	return 0;
}

B:

#include 
#include 

const int N = 100005;
typedef long long ll;

int n;
ll h[N];

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%lld", &h[i]);
	ll now = 0;
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		if (h[i] > h[i - 1]) {
			ll need = h[i] - h[i - 1];
			if (now >= need) {
				now -= need;
			} else {
				ans += need - now;
				now = 0;
			}
		} else {
			now += h[i - 1] - h[i];
		}
	}
	printf("%lld\n", ans);
	return 0;
}

C:

#include 
#include 

const int N = 2005;

typedef long long ll;

int n;
ll g[N][N], zhu[N + N], fu[N + N];

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			scanf("%lld", &g[i][j]);
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			zhu[i - j + n] += g[i][j];
			fu[i + j] += g[i][j];
		}
	}
	ll b = -1, w = -1;
	int bx, by, wx, wy;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			ll sum = zhu[i - j + n] + fu[i + j] - g[i][j];
			if ((i + j) % 2 == 0) {
				if (b < sum) {
					b = sum;
					bx = i + 1;
					by = j + 1;
				}
			}
			else {
				if (w < sum) {
					w = sum;
					wx = i + 1;
					wy = j + 1;
				}
			}
		}
	}
	printf("%lld\n", b + w);
	printf("%d %d %d %d\n", bx, by, wx, wy);
	return 0;
}

D:

#include 
#include 
#include 
using namespace std;

const int N = 1005;

struct Num {
	int v[10], la;
} num[N];

bool cmp(Num a, Num b) {
	return a.la < b.la;
}

int n, k, dp[N][N];

bool judge(int i, int j) {
	for (int x = 0; x < k; x++) {
		if (num[i].v[x] < num[j].v[x]) return false;
	}
	return true;
}

int main() {
	scanf("%d%d", &n, &k);
	for (int i = 0; i < k; i++) {
		int tmp;
		for (int j = 1; j <= n; j++) {
			scanf("%d", &tmp);
			num[tmp].v[i] = j;
		}
	}
	for (int i = 0; i <= n; i++) {
		int maxv = 0;
		for (int j = 0; j < k; j++) {
			maxv = max(maxv, num[i].v[j]);
		}
		num[i].la = maxv;
	}
	sort(num, num + n + 1, cmp);
	/*
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j < k; j++) {
			printf("%d ", num[i].v[j]);
		}
		printf("\n");
	}*/
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j < i; j++) {
			dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			if (judge(i, j)) {
				dp[i][i] = max(dp[i][i], dp[i][j] + 1);
			}
		}
	}
	int ans = 0;
	for (int i = 0; i <= n; i++)
		ans = max(ans, dp[n][i]);
	printf("%d\n", ans);
	return 0;
}

E:

#include 
#include 
#include 
using namespace std;

const int N = 100005;

int n, q, val[N], f[N];
vector g[N];

void dfs(int u, int fa) {
	f[u] = fa;
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (v == fa) continue;
		dfs(v, u);
	}
}

int gcd(int a, int b) {
	while (b) {
		int tmp = a % b;
		a = b;
		b = tmp;
	}
	return a;
}

int query(int u) {
	int v = val[u];
	u = f[u];
	while (u) {
		if (gcd(val[u], v) > 1) return u;
		u = f[u];
	}
	return -1;
}

int main() {
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; i++)
		scanf("%d", &val[i]);
	int u, v;
	for (int i = 1; i < n; i++) {
		scanf("%d%d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	dfs(1, 0);
	int tp, a, b;
	while (q--) {
		scanf("%d%d", &tp, &a);
		if (tp == 1) {
			printf("%d\n", query(a));
		} else {
			scanf("%d", &b);
			val[a] = b;
		}
	}
	return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved