Codeforces Round #266 (Div. 2)

Codeforces Round #266 (Div. 2)

题目链接

A:就简单的判断一下那种更大即可

B:枚举x到sqrt(n),然后可以直接算出y,然后判断一下即可

C:先判断和是否是3的倍数,然后预处理出前缀和出现位置和后缀和对应sum / 3个数,然后从头往后扫一遍把当前一个和后面进行组合即可

D:先预处理出差分,使得数组表示线段的添加方式,然后每次有一个-1,就能和前面多少个1进行匹配,方案数就乘上多少,如果是0,就能和前面+1个匹配

E:利用并查集,把每次询问拆分成2个部分,起点到x,x到根,然后每次从根往下dfs一遍,对应询问符合的就把对应询问++,dfs完如果一个询问符合两次,就是符合的输出YES,否则就是NO

代码:

#include <cstdio>
#include <cstring>

int n, m, a, b;

int solve() {
	if (b >= m * a) return a * n;
	int yu = n % m;
	int ans = n / m * b;
	if (yu * a < b) return ans + yu * a;
	return ans + b;
}

int main() {
	scanf("%d%d%d%d", &n, &m, &a, &b);
	printf("%d\n", solve());
	return 0;
}

B:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;

ll n, a, b;

int main() {
	scanf("%lld%lld%lld", &n, &a, &b);
	n = n * 6;
	ll ans = 1e18, x, y;
	if (a * b >= n) {
		x = a;
		y = b;
		ans = a * b;
	}
	else {
		int flag = 0;
		if (a > b) {
			flag = 1;
			swap(a, b);
		}
		for (int i = 1; i < 1000000 && i < n; i++) {
			ll r = n / i + (n % i != 0);
			ll l = i;
			if (l > r) swap(l, r);
			if (l < a || r < b) continue;
			if (i * r < ans) {
				ans = i * r;
				x = i;
				y = r;
			}
		}
		if (flag) swap(x, y);
	}
	printf("%lld\n%lld %lld\n", ans, x, y);
	return 0;
}

C:

#include <cstdio>
#include <cstring>

const int N = 500005;

typedef long long ll;

int n;
ll a[N], pres[N], prec[N], sufs[N], sufc[N];

int main() {
	scanf("%d", &n);
	ll sum = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
		sum += a[i];
	}
	if (sum % 3) printf("0\n");
	else {
		sum /= 3;
		for (int i = 1; i <= n; i++) {
			pres[i] = pres[i - 1] + a[i];
			if (pres[i] == sum)
				prec[i] = 1;
		}
		for (int i = n; i >= 1; i--) {
			sufs[i] = sufs[i + 1] + a[i];
			sufc[i] = sufc[i + 1];
			if (sufs[i] == sum) sufc[i]++;
		}
		ll ans = 0;
		for (int i = 1; i <= n; i++)
			ans += prec[i] * sufc[i + 2];
		printf("%lld\n", ans);
	}
	return 0;
}

D:

#include <cstdio>
#include <cstring>

typedef long long ll;
const int MOD = 1000000007;

const int N = 2005;
int n, h, a[N], b[N];

int main() {
	scanf("%d%d", &n, &h);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] = h - a[i];
	for (int i = 1; i <= n + 1; i++) b[i] = a[i] - a[i -1];
	int ans = 1, cnt = 0;
	for (int i = 1; i <= n + 1; i++) {
		if (b[i] == 0) ans = (ll)ans * (cnt + 1) % MOD;
		else if (b[i] == 1) cnt++;
		else if (b[i] == -1) ans = (ll)ans * cnt % MOD, cnt--;
		else ans = 0;
	}
	printf("%d\n", ans);
	return 0;
}

E:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

#define MP(a,b) make_pair(a,b)
typedef pair<int, int> pii;
const int N = 100005;

int n, m, parent[N];

int find(int x) {
	return x == parent[x] ? x : parent[x]  = find(parent[x]);
}

vector<pii> p, q[N];
vector<int> g[N];
int tot, vis[N], cnt[N];

void dfs(int u) {
	vis[u] = 1;
	for (int i = 0; i < g[u].size(); i++)
		dfs(g[u][i]);
	for (int i = 0; i < q[u].size(); i++) {
		if (vis[q[u][i].first])
			cnt[q[u][i].second]++;
	}
	vis[u] = 0;
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++)
		parent[i] = i;
	int c, x, y;
	while (m--) {
		scanf("%d%d", &c, &x);
		if (c == 2)
			p.push_back(MP(find(x), x));
		else {
			scanf("%d", &y);
			if (c == 1) {
				g[y].push_back(x);
				int px = find(x);
				int py = find(y);
				if (px != py)
					parent[px] = py;
			} else {
				q[x].push_back(MP(p[y - 1].first, tot));
				q[p[y - 1].second].push_back(MP(x, tot));
				tot++;
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (parent[i] == i) dfs(i);
	for (int i = 0; i < tot; i++)
		if (cnt[i] == 2) printf("YES\n");
		else printf("NO\n");
	return 0;
}
时间: 2024-10-03 06:07:41

Codeforces Round #266 (Div. 2)的相关文章

Codeforces Round #266 (Div. 2)B(暴力枚举)

很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找出a和b中最小的那个,然后开始枚举,一直枚举到sqrt(6*n)的向上取整.这样所有可能是答案的情况都有啦.再干别的都是重复的或者肯定不是最小面积的. #include<iostream> #include<cstdio> #include<cstdlib> #includ

Codeforces Round #266 (Div.2) B Wonder Room --枚举

题意:给出一个两边长为a,b的矩形,要求增加a和增加b使a*b>=6*n且a*b最小. 解法:设新的a,b为a1,b1,且设a<b,那么a<=a1<=ceil(sqrt(6*n)),那么我们可以枚举a1,然后算出b1,如果b1<b,那么b1 = b,然后算出面积,取所有面积的最小值不就可以了,然后再枚举一次b1,处理与之相同即可. 复杂度O(sqrt(n)) 代码: #include <iostream> #include <cstdio> #incl

Codeforces Round #266 (Div. 2) A

题目: A. Cheap Travel time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found ou

Codeforces Round #266 (Div. 2)C. Number of Ways

传送门 Description You've got array a[1], a[2], ..., a[n], consisting of n integers. Count the number of ways to split all the elements of the array into three contiguous parts so that the sum of elements in each part is the same. More formally, you nee

Codeforces Round #266 (Div. 2)-C,D

C - Number of Ways 直接暴力从前往后寻找.假设找到1/3sum的位置,那么标记++.找到2/3的位置,总数加上标记数. #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> #include<vector> #include<math.h> #include<que

Codeforces Round #266 (Div. 2) A. Cheap Travel

Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will nee

Codeforces Round #266 (Div. 2) B. Wonder Room

The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has a a?×?b square meter wonder room. The caretaker wants to accommodate exactly n students there. But the law says that

Codeforces Round #266 (Div. 2) D. Increase Sequence

Peter has a sequence of integers a1,?a2,?...,?an. Peter wants all numbers in the sequence to equalh. He can perform the operation of "adding one on the segment[l,?r]": add one to all elements of the sequence with indices froml to r (inclusive).

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/