「模拟测试」20171030

T2 Game

甲乙两个人轮流那一些物品,甲先手,他可以拿走 $1$ 或 $2$ 个物品。对于后面,若前一个人拿走 $k$ 个物品,当前的人可以拿走 $k$ 或 $k + 1$ 个物品,甲乙的策略都是让自己尽量比别人拿的物品的价值高,求最优策略下,甲最多比乙多拿多少?

题解

$f[i][j]$ 表示第 $i$ 个物品,当前拿 $j$ 个时,甲最多比乙拿多少,先预处理一个前缀和。
注意到其实甲的决策并不会影响到答案,只要乙的决策最劣即是最优答案,而且显然 $j \leq \sqrt{n}$,所以我们直接记忆化搜索就好了。

代码

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「SuperOJ 2018」游戏 30-10-2017
* DP
* @author xehoth
*/
#include <bits/stdc++.h>

namespace IO {

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}

template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}

inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}

inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}

template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void print(const char *s) {
for (; *s; s++) print(*s);
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}

template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}

~InputOutputStream() { flush(); }
} io;
}

namespace {

const int MAXN = 20000;
const int INF = -1061109568;

using IO::io;

int a[MAXN + 1], sum[MAXN + 1], f[MAXN + 1][255], n;

inline int optMax(const register int a, const register int b) {
return a > b ? a : b;
}

inline int dp(register int x, register int k) {
register int &ret = f[x][k];
if (ret != INF) return ret;
return x + k > n
? 0
: (x + k <= n
? ret = optMax(ret, sum[x + k] - sum[x] - dp(x + k, k))
: 0,
x + k + 1 <= n
? ret = optMax(
ret, sum[x + k + 1] - sum[x] - dp(x + k + 1, k + 1))
: 0,
ret);
}

inline void solve() {
register int T;
io >> T;
while (T--) {
io >> n;
for (register int i = 1; i <= n; i++)
io >> a[i], sum[i] = sum[i - 1] + a[i];
memset(f, 0xc0, sizeof(f));
io << dp(0, 1) << '\n';
}
}
}

int main() {
// freopen("sample/1.in", "r", stdin);
solve();
return 0;
}

T3 星星

给出一个无重边自环的无向图,求由四个点五条变组成的这种图的个数
T3 星星

题解

对于每个点我们先把它连的点打上标记,再枚举所有与它相连且度数比它小的点的边集来求每条边的贡献,可以证明这样的复杂度是 $O(m \sqrt{m})$ 的。

至于证明我们分度数 $\sqrt{m}$ 来讨论即可。

代码

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Copyright (c) 2017, xehoth
* All rights reserved.
* 「SuperOJ 2019」星星 30-10-2017
*
* @author xehoth
*/
#include <bits/stdc++.h>

namespace IO {

inline char read() {
static const int IN_LEN = 1000000;
static char buf[IN_LEN], *s, *t;
s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin) : 0;
return s == t ? -1 : *s++;
}

template <typename T>
inline bool read(T &x) {
static char c;
static bool iosig;
for (c = read(), iosig = false; !isdigit(c); c = read()) {
if (c == -1) return false;
c == '-' ? iosig = true : 0;
}
for (x = 0; isdigit(c); c = read()) x = x * 10 + (c ^ '0');
iosig ? x = -x : 0;
return true;
}

inline void read(char &c) {
while (c = read(), isspace(c) && c != -1)
;
}

inline int read(char *buf) {
register int s = 0;
register char c;
while (c = read(), isspace(c) && c != -1)
;
if (c == -1) {
*buf = 0;
return -1;
}
do
buf[s++] = c;
while (c = read(), !isspace(c) && c != -1);
buf[s] = 0;
return s;
}

const int OUT_LEN = 1000000;

char obuf[OUT_LEN], *oh = obuf;

inline void print(char c) {
oh == obuf + OUT_LEN ? (fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf) : 0;
*oh++ = c;
}

template <typename T>
inline void print(T x) {
static int buf[30], cnt;
if (x == 0) {
print('0');
} else {
x < 0 ? (print('-'), x = -x) : 0;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 | 48;
while (cnt) print((char)buf[cnt--]);
}
}

inline void print(const char *s) {
for (; *s; s++) print(*s);
}

inline void flush() { fwrite(obuf, 1, oh - obuf, stdout); }

struct InputOutputStream {
template <typename T>
inline InputOutputStream &operator>>(T &x) {
read(x);
return *this;
}

template <typename T>
inline InputOutputStream &operator<<(const T &x) {
print(x);
return *this;
}

~InputOutputStream() { flush(); }
} io;
}

namespace {

using IO::io;

const int MAXN = 100000;
int n, m;

struct Data {
int deg, id;
inline bool operator<(const Data &d) const { return deg > d.deg; }
} d[MAXN + 1];

std::vector<int> edge[MAXN + 1];

typedef std::vector<int>::iterator Iterator;
bool vis[MAXN + 1];
int tag[MAXN + 1];

inline void solveCase() {
for (register int i = 1, u, v; i <= m; i++) {
io >> u >> v, edge[u].push_back(v), edge[v].push_back(u);
d[u].deg++, d[v].deg++;
}
for (register int i = 1; i <= n; i++) d[i].id = i;
memset(vis, false, sizeof(bool) * (n + 1));
memset(tag, 0, sizeof(int) * (n + 1));
register long long ans = 0;
std::sort(d + 1, d + n + 1);
for (register int i = 1, cur, cnt; i <= n; i++) {
vis[cur = d[i].id] = true;
for (register Iterator p = edge[cur].begin(); p != edge[cur].end(); p++)
tag[*p] = cur;
for (register Iterator p = edge[cur].begin(); p != edge[cur].end();
p++) {
cnt = 0;
if (!vis[*p])
for (register Iterator k = edge[*p].begin();
k != edge[*p].end(); k++)
if (tag[*k] == cur) cnt++;
ans += ((long long)cnt * (long long)(cnt - 1)) / 2;
}
}
io << ans << '\n';
for (register int i = 1; i <= n; i++) edge[i].clear();
for (register int i = 1; i <= n; i++) d[i].deg = 0;
}

inline void solve() {
register int T;
for (io >> T, T--; T--;) io >> n >> m, solveCase();
io >> n >> m, solveCase();
}
}

int main() {
// freopen("sample/1.in", "r", stdin);
solve();
return 0;
}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×