= Vain学习(C/C++ acm算法 python Linux Golang)的个人网站 =

Codeforces-Round-660-Div-2

Posted on

A:
思路:
用 6 10 14 15 去构造答案,因为只用前三个构造可能会出现重复的

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
const ll maxn = 3e5 + 5;
//int a[maxn];
char s[N];
map<int, int> mp;
int pre[maxn], sum[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{

int a = 6, b = 10, c = 14, d = 15;
int n;
cin >> n;
if (n - a - b - c <= 0)
cout << "NO" << endl;
else
{
int e = n - a - b - c;
if (e == a || e == b || e == c)
{
if (n - a - b - d <= 0)
cout << "NO" << endl;
else
{
cout << "YES" << endl;
cout << a << ' ' << b << ' ' << d << ' ' << n - a - b - d << endl;
}
}
else
{
cout << "YES" << endl;
cout << a << ' ' << b << ' ' << c << ' ' << e << endl;
}
}
}
}

B:
思路:
他让用x构造最大的二进制串r,但这个x又要在所有可能里尽可能的小,这个x是个n位数,显然二进制串当然越长越好了,所以只有两种选择,9和8都是3位的,但又要让x尽可能的小,呢么删掉的n位就需要是8即可

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const int N = 1e3 + 5;
const ll maxn = 3e5 + 5;
//int a[maxn];
char s[N];
map<int, int> mp;
int pre[maxn], sum[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
string ans = string(n - (n + 3) / 4, '9') + string((n + 3) / 4, '8');
cout << ans << endl;
}
}

C:
给一颗树,树上的每个节点有一个点权和一开始有多少人,然后给出计算点权的方案,问这棵树的每个点权是否合法。
思路:

1
添加几个约束即可

参考代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <math.h>
using namespace std;
typedef long long ll;
const ll N = 1e3 + 5;
const ll maxn = 3e5 + 5;
//ll a[maxn];
char s[N];
ll cnt;
map<ll, ll> mp;
ll pre[maxn], sum[maxn], head[maxn], w[maxn], nums[maxn];
ll good[maxn];
struct node
{
ll v, nex;
} edge[maxn];
void add(ll u, ll v)
{
edge[cnt].v = v, edge[cnt].nex = head[u];
head[u] = cnt++;
}
bool fla;
void dfs(ll u, ll fa)
{
pre[u] = fa;
for (ll i = head[u]; ~i; i = edge[i].nex)
{
ll v = edge[i].v;
if (v != fa)
dfs(v, u), nums[u] += nums[v];
}
ll x = (nums[u] + w[u]) / 2, y;
y = nums[u] - x;
if (x < 0 || y < 0 || x + y != nums[u] || x - y != w[u])
fla = false;
good[u] = x;
ll sum = 0;
for (int i = head[u]; ~i; i = edge[i].nex)
sum += good[edge[i].v];
if (sum > good[u])
fla = false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll t;
cin >> t;
while (t--)
{
memset(head, -1, sizeof head);
ll n, m;
cnt = 0;
cin >> n >> m;
for (ll i = 1; i <= n; i++)
cin >> nums[i], good[i] = 0;
for (ll i = 1; i <= n; i++)
cin >> w[i];
ll u, v;
for (ll i = 1; i < n; i++)
cin >> u >> v, add(u, v), add(v, u);
fla = true;
dfs(1, 0);
if (fla)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}