2.21自我训练情况
题目:洛谷P1052,P1233,P1201。
比赛时间:2.21下午1:30~2:30。
T1
本来想DP的,结果空间不够($O(L)$)。
写个记忆化深搜 $30$ 分走人。
1 #include<stdio.h>
2 #include<string.h>
3 #include<map>
4 #define reg register
5 #define ri reg int
6 #define rep(i, x, y) for(ri i = x; i <= y; ++i)
7 #define nrep(i, x, y) for(ri i = x; i >= y; --i)
8 #define DEBUG 1
9 #define ll long long
10 #define il inline
11 #define max(i, j) (i) > (j) ? (i) : (j)
12 #define min(i, j) (i) < (j) ? (i) : (j)
13 #define read(i) io.READ(i)
14 #define print(i) io.WRITE(i)
15 #define push(i) io.PUSH(i)
16 struct IO {
17 #define MAXSIZE (1 << 20)
18 #define isdigit(x) (x >= '0' && x <= '9')
19 char buf[MAXSIZE], *p1, *p2;
20 char pbuf[MAXSIZE], *pp;
21 #if DEBUG
22 #else
23 IO() : p1(buf), p2(buf), pp(pbuf) {}
24 ~IO() {
25 fwrite(pbuf, 1, pp - pbuf, stdout);
26 }
27 #endif
28 inline char gc() {
29 #if DEBUG
30 return getchar();
31 #endif
32 if(p1 == p2)
33 p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
34 return p1 == p2 ? ' ' : *p1++;
35 }
36 inline bool blank(char ch) {
37 return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
38 }
39 template <class T>
40 inline void READ(T &x) {
41 register double tmp = 1;
42 register bool sign = 0;
43 x = 0;
44 register char ch = gc();
45 for(; !isdigit(ch); ch = gc())
46 if(ch == '-') sign = 1;
47 for(; isdigit(ch); ch = gc())
48 x = x * 10 + (ch - '0');
49 if(ch == '.')
50 for(ch = gc(); isdigit(ch); ch = gc())
51 tmp /= 10.0, x += tmp * (ch - '0');
52 if(sign) x = -x;
53 }
54 inline void READ(char *s) {
55 register char ch = gc();
56 for(; blank(ch); ch = gc());
57 for(; !blank(ch); ch = gc())
58 *s++ = ch;
59 *s = 0;
60 }
61 inline void READ(char &c) {
62 for(c = gc(); blank(c); c = gc());
63 }
64 inline void PUSH(const char &c) {
65 #if DEBUG
66 putchar(c);
67 #else
68 if(pp - pbuf == MAXSIZE) {
69 fwrite(pbuf, 1, MAXSIZE, stdout);
70 pp = pbuf;
71 }
72 *pp++ = c;
73 #endif
74 }
75 template <class T>
76 inline void WRITE(T x) {
77 if(x < 0) {
78 x = -x;
79 PUSH('-');
80 }
81 static T sta[35];
82 T top = 0;
83 do {
84 sta[top++] = x % 10;
85 x /= 10;
86 }while(x);
87 while(top)
88 PUSH(sta[--top] + '0');
89 }
90 template <class T>
91 inline void WRITE(T x, char lastChar) {
92 WRITE(x);
93 PUSH(lastChar);
94 }
95 } io;
96 std::map<int, bool> a;
97 int l, s, t, m;
98 int f[2000010];
99 int dfs(int x) {
100 ri ans = 1 << 30;
101 if(x <= 2000000 && f[x] != -1) return f[x];
102
103 rep(i, s, t) {
104 if(x - i >= s) ans = min(ans, dfs(x - i));
105 }
106 if(a.count(x)) ++ans;
107 if(x <= 2000000) f[x] = ans;
108 return ans;
109 }
110 int main() {
111 memset(f, -1, sizeof(f));
112 read(l), read(s), read(t), read(m);
113 rep(i, 1, m) {
114 ri x;
115 read(x);
116 a[x] = true;
117 }
118 rep(i, s, t) if(a.count(i)) f[i] = 1;
119 else f[i] = 0;
120
121 print(dfs(l));
122 }
T2
把 L 排序后求个 W 的最长上升子序列。
好像没问题的样子。
1 #include<stdio.h>
2 #include<algorithm>
3 #define reg register
4 #define ri reg int
5 #define rep(i, x, y) for(ri i = x; i <= y; ++i)
6 #define nrep(i, x, y) for(ri i = x; i >= y; --i)
7 #define DEBUG 1
8 #define ll long long
9 #define il inline
10 #define max(i, j) (i) > (j) ? (i) : (j)
11 #define min(i, j) (i) < (j) ? (i) : (j)
12 #define read(i) io.READ(i)
13 #define print(i) io.WRITE(i)
14 #define push(i) io.PUSH(i)
15 struct IO {
16 #define MAXSIZE (1 << 20)
17 #define isdigit(x) (x >= '0' && x <= '9')
18 char buf[MAXSIZE], *p1, *p2;
19 char pbuf[MAXSIZE], *pp;
20 #if DEBUG
21 #else
22 IO() : p1(buf), p2(buf), pp(pbuf) {}
23 ~IO() {
24 fwrite(pbuf, 1, pp - pbuf, stdout);
25 }
26 #endif
27 inline char gc() {
28 #if DEBUG
29 return getchar();
30 #endif
31 if(p1 == p2)
32 p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
33 return p1 == p2 ? ' ' : *p1++;
34 }
35 inline bool blank(char ch) {
36 return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
37 }
38 template <class T>
39 inline void READ(T &x) {
40 register double tmp = 1;
41 register bool sign = 0;
42 x = 0;
43 register char ch = gc();
44 for(; !isdigit(ch); ch = gc())
45 if(ch == '-') sign = 1;
46 for(; isdigit(ch); ch = gc())
47 x = x * 10 + (ch - '0');
48 if(ch == '.')
49 for(ch = gc(); isdigit(ch); ch = gc())
50 tmp /= 10.0, x += tmp * (ch - '0');
51 if(sign) x = -x;
52 }
53 inline void READ(char *s) {
54 register char ch = gc();
55 for(; blank(ch); ch = gc());
56 for(; !blank(ch); ch = gc())
57 *s++ = ch;
58 *s = 0;
59 }
60 inline void READ(char &c) {
61 for(c = gc(); blank(c); c = gc());
62 }
63 inline void PUSH(const char &c) {
64 #if DEBUG
65 putchar(c);
66 #else
67 if(pp - pbuf == MAXSIZE) {
68 fwrite(pbuf, 1, MAXSIZE, stdout);
69 pp = pbuf;
70 }
71 *pp++ = c;
72 #endif
73 }
74 template <class T>
75 inline void WRITE(T x) {
76 if(x < 0) {
77 x = -x;
78 PUSH('-');
79 }
80 static T sta[35];
81 T top = 0;
82 do {
83 sta[top++] = x % 10;
84 x /= 10;
85 }while(x);
86 while(top)
87 PUSH(sta[--top] + '0');
88 }
89 template <class T>
90 inline void WRITE(T x, char lastChar) {
91 WRITE(x);
92 PUSH(lastChar);
93 }
94 } io;
95 int n, f[5010];
96 struct _{
97 int l, w;
98 } a[5010];
99 bool cmp(_ a, _ b) {
100 return a.l > b.l;
101 }
102 int main() {
103 read(n);
104 rep(i, 1, n) read(a[i].l), read(a[i].w);
105 std::sort(a + 1, a + n + 1, cmp);
106 f[1] = 1;
107 rep(i, 2, n) {
108 rep(j, 1, i)
109 if(a[i].w > a[j].w)
110 f[i] = max(f[i], f[j] + 1);
111 }
112 print(f[n]);
113 return 0;
114 }
T3
大模拟。
用 map 写最开始挂了。
然后写了个随便卡卡的哈希。
1 #include<stdio.h>
2 #include<map>
3 #include<string.h>
4 #define reg register
5 #define ri reg int
6 #define rep(i, x, y) for(ri i = x; i <= y; ++i)
7 #define nrep(i, x, y) for(ri i = x; i >= y; --i)
8 #define DEBUG 1
9 #define ll long long
10 #define il inline
11 #define max(i, j) (i) > (j) ? (i) : (j)
12 #define min(i, j) (i) < (j) ? (i) : (j)
13 #define read(i) io.READ(i)
14 #define print(i) io.WRITE(i)
15 #define push(i) io.PUSH(i)
16 struct IO {
17 #define MAXSIZE (1 << 20)
18 #define isdigit(x) (x >= '0' && x <= '9')
19 char buf[MAXSIZE], *p1, *p2;
20 char pbuf[MAXSIZE], *pp;
21 #if DEBUG
22 #else
23 IO() : p1(buf), p2(buf), pp(pbuf) {}
24 ~IO() {
25 fwrite(pbuf, 1, pp - pbuf, stdout);
26 }
27 #endif
28 inline char gc() {
29 #if DEBUG
30 return getchar();
31 #endif
32 if(p1 == p2)
33 p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
34 return p1 == p2 ? ' ' : *p1++;
35 }
36 inline bool blank(char ch) {
37 return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
38 }
39 template <class T>
40 inline void READ(T &x) {
41 register double tmp = 1;
42 register bool sign = 0;
43 x = 0;
44 register char ch = gc();
45 for(; !isdigit(ch); ch = gc())
46 if(ch == '-') sign = 1;
47 for(; isdigit(ch); ch = gc())
48 x = x * 10 + (ch - '0');
49 if(ch == '.')
50 for(ch = gc(); isdigit(ch); ch = gc())
51 tmp /= 10.0, x += tmp * (ch - '0');
52 if(sign) x = -x;
53 }
54 inline void READ(char *s) {
55 register char ch = gc();
56 for(; blank(ch); ch = gc());
57 for(; !blank(ch); ch = gc())
58 *s++ = ch;
59 *s = 0;
60 }
61 inline void READ(char &c) {
62 for(c = gc(); blank(c); c = gc());
63 }
64 inline void PUSH(const char &c) {
65 #if DEBUG
66 putchar(c);
67 #else
68 if(pp - pbuf == MAXSIZE) {
69 fwrite(pbuf, 1, MAXSIZE, stdout);
70 pp = pbuf;
71 }
72 *pp++ = c;
73 #endif
74 }
75 template <class T>
76 inline void WRITE(T x) {
77 if(x < 0) {
78 x = -x;
79 PUSH('-');
80 }
81 static T sta[35];
82 T top = 0;
83 do {
84 sta[top++] = x % 10;
85 x /= 10;
86 }while(x);
87 while(top)
88 PUSH(sta[--top] + '0');
89 }
90 template <class T>
91 inline void WRITE(T x, char lastChar) {
92 WRITE(x);
93 PUSH(lastChar);
94 }
95 } io;
96 std::map<long long, int> mp;
97 int n;
98 char s[20], a[20], h[20][20];
99 ll hash(char a[]) {
100 ri len = strlen(a);
101 ll ans = 0;
102 rep(i, 1, len) {
103 ans = ans * 200 + (int)a[i];
104 ans %= 1000000007;
105 }
106 return ans;
107 }
108 int main() {
109 read(n);
110 rep(i, 1, n) {
111 scanf("%s", h[i]);
112 mp[hash(h[i])] = 0;
113 }
114
115 rep(i, 1, n) {
116 ri p, m;
117 scanf("%s%d%d", s, &m, &p);
118 if(p == 0) break;
119 mp[hash(s)] -= m / p * p;
120 rep(j, 1, p) {
121 scanf("%s", a);
122 mp[hash(a)] += m / p;
123 }
124 }
125 rep(i, 1, n)
126 printf("%s %d\n", h[i], mp[hash(h[i])]);
127 return 0;
128 }
RESULT
$ 27+20+67=114 $。
完全不发挥。