20161110练习总结

T1

贪心,排序后一次一次跳和尽可能多地跳就好…

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
#include <bits/stdc++.h>
using namespace std;
bool iosig;
char ch;
template<class T>
inline void read(T &x) {
x = 0, iosig = 0;
do {
ch = getchar();
if (ch == '-') iosig = 1;
} while (!isdigit(ch));
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
}
int maxh, maxjump, minjump;
int n, deltah;
int h[1000010];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(deltah);
for (int i = 1; i <= n; i++)
read(h[i]);
sort(h + 1, h + n + 1);
for (int i = 1; i <= n; i++) {
if (h[i] - h[i - 1] <= deltah)
maxh = h[i], maxjump++;
else break;
}
register int cur = 0, pre = 0;
while (h[pre] < maxh) {
while (h[cur] - h[pre] <= deltah && cur <= n)
cur++;
pre = --cur;
minjump++;
}
cout << maxh << " " << maxjump << " " << minjump;
return 0;
}

T2

考虑从 $l$ 往 $r$ 连权值为 $c$ 的边,然后相邻边连权值为 $0$ 的边,跑 dijkstra 就好了。
出题人很良心,终于卡SPFA了233333….

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
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
typedef __gnu_pbds::cc_hash_table<int, int> HashMap;
char ch;
bool iosig;
template<class T>
inline void read(T &x) {
x = 0, iosig = 0;
do {
ch = getchar();
if (ch == '-') iosig = 1;
} while (!isdigit(ch));
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = getchar();
}
const int MAX = 6000000;
int n, m;
struct Data {
int l, r, w;
inline bool operator < (const Data &n) const {
return l > n.l;
}
} data[MAX];
struct Node {
int v, w;
Node(int v, int w) : v(v), w(w) {}
inline bool operator < (const Node &n) const {
return w > n.w;
}
};
vector<Node> edge[MAX];
inline void addEdge(int u, int v, int w) {
edge[u].push_back(Node(v, w));
}
long long dis[MAX];
bool vis[MAX];
HashMap h;
struct Query {
int v;
long long w;
Query(int v, long long w) : v(v), w(w) {}
inline bool operator < (const Query &n) const {
return w > n.w;
}
};
typedef __gnu_pbds::priority_queue<Query> Heap;
Heap::point_iterator id[MAX];
int len;
int ranges[MAX];
inline void dijkstra(int s) {
Heap q;
fill(dis + 1, dis + len + 1, LONG_LONG_MAX);
dis[s] = 0;
id[s] = q.push(Query(s, 0));
while (!q.empty()) {
register int now = q.top().v;
q.pop();
if (vis[now]) continue;
vis[now] = true;
for (register int i = 0; i < edge[now].size(); i++) {
Node *it = &edge[now][i];
if (it->w + dis[now] < dis[it->v]) {
dis[it->v] = it->w + dis[now];
if (id[it->v] != NULL) q.modify(id[it->v], Query(it->v, dis[it->v]));
else id[it->v] = q.push(Query(it->v, dis[it->v]));
}
}
}
}
int main() {
/*#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif*/
read(n), read(m);
for (register int i = 1; i <= m; i++)
read(data[i].l), read(data[i].r), read(data[i].w), ranges[(i << 1) - 1] = data[i].l, ranges[i << 1] = data[i].r;
sort(data + 1, data + m + 1);
sort(ranges + 1, ranges + (m << 1 | 1));
len = unique(ranges + 1, ranges + m * 2 + 1) - (ranges + 1);
if (ranges[1] != 1 || ranges[len] != n)
cout << "-1", exit(0);
for (register int i = len; i > 1; i--)
addEdge(i, i - 1, 0);
for (register int i = 1; i <= len; i++)
h[ranges[i]] = i;
for (register int i = 1; i <= m; i++) {
data[i].l = h[data[i].l], data[i].r = h[data[i].r];
if (data[i].l > data[i].r) swap(data[i].l, data[i].r);
addEdge(data[i].l, data[i].r, data[i].w);
}
dijkstra(1);
cout << (dis[len] == LONG_LONG_MAX ? -1 : dis[len]);
return 0;
}

T3

听说hyj同学此题写了300行+,然而此题不是100行-就能解决的吗?
考虑打部分高精度,贪心,从高位往低位暴力 $DFS$。

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
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
const int MAXLEN = 50;
int n, len1, len2, T[10][10];
bool flag;
char w[MAXN][100], f[MAXN][100], ans[MAXN][100];
inline void dfs(int pos, int cur, int len, bool status) {
if (cur == len + 1) {
flag = 1;
return;
}
if (!status) {
for (register int i = 9; i >= 0; i--) {
if (T[f[pos][cur] - '0'][i] && i <= w[pos][cur] - '0') {
ans[pos][cur] = i + '0';
if (i == w[pos][cur] - '0') dfs(pos, cur + 1, len, 0);
else dfs(pos, cur + 1, len, 1);
if (flag) return;
}
}
} else {
for (register int i = 9; i >= 0; i--) {
if (T[f[pos][cur] - '0'][i]) {
ans[pos][cur] = i + '0';
dfs(pos, cur + 1, len, 1);
if (flag) return;
}
}
}
}
inline void init() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (register int i = 1; i <= n; i++)
cin >> w[i] + 1 >> f[i] + 1;
for (register int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
cin >> T[i][j];
for (register int i = 0; i < 10; i++) T[i][i] = 1;
for (register int i = 0; i < 10; i++)
for (register int j = 0; j < 10; j++)
for (register int k = 0; k < 10; k++)
T[j][k] = T[j][k] || (T[j][i] && T[i][k]);
}
inline void solve() {
for (register int i = n; i >= 1; i--) {
len1 = strlen(w[i] + 1), len2 = strlen(f[i] + 1);
if (len1 < len2) cout << "NO", exit(0);
if (len1 > len2) {
for (register int j = 1; j <= len2; j++) {
for (register int k = 9; k >= 0; k--) {
if (T[f[i][j] - '0'][k] == 1) {
ans[i][j] = k + '0';
break;
}
}
}
} else {
flag = false;
dfs(i, 1, len1, 0);
if (!flag) cout << "NO", exit(0);
}
if (i == 1) break;
register int ret = 1;
len1 = strlen(w[i - 1] + 1), len2 = strlen(ans[i] + 1);
if (len1 != len2) ret = (len1 > len2) + 1;
else {
for (register int j = 1; j <= len1; j++) {
if (ans[i][j] > w[i - 1][j]) {
ret = 1;
break;
}
if (ans[i][j] < w[i - 1][j]) {
ret = 2;
break;
}
}
}
if (!ans[i][1]) cout << "NO", exit(0);
if (ret == 2)
for (register int j = 1; j <= MAXLEN; j++)
w[i - 1][j] = ans[i][j];
}
cout << "YES\n";
for (register int i = 1; i <= n; i++)
cout << ans[i] + 1 << " ";
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
init();
solve();
return 0;
}

Comments

Your browser is out-of-date!

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

×