这一场对我来说要比上一场稍难一点,
P1 Snow Boots
还是一如既往地简单,有些人用什么线段树,整体二分,单调队列,你们脑子坏了么?
为什么不用并查集呢!!!
先都按S排序
每次把不能走的染黑,如果任意一段连续的黑色个数小于< d的话我就可以通过,否则不行。
用并查集来维护最大子段和。
时间复杂度$O(nlogn)$
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <bitset> 9 #include <cstdio> 10 #include <cctype> 11 #include <string> 12 #include <cstring> 13 #include <cassert> 14 #include <climits> 15 #include <cstdlib> 16 #include <iostream> 17 #include <algorithm> 18 #include <functional> 19 using namespace std ; 20 21 #define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) 22 #define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++) 23 #define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) 24 #define clr(a) memset(a, 0, sizeof(a)) 25 #define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp) 26 #define ass(a, sum) memset(a, sum, sizeof(a)) 27 28 #define ls ((rt) << 1) 29 #define rs ((rt) << 1 | 1) 30 #define lowbit(x) (x & -x) 31 #define mp make_pair 32 #define pb push_back 33 #define fi first 34 #define se second 35 #define endl '\n' 36 #define ENDL cout << endl 37 #define SZ(x) ((int)x.size()) 38 39 typedef long long ll ; 40 typedef unsigned long long ull ; 41 typedef vector <int> vi ; 42 typedef pair <int, int> pii ; 43 typedef pair <ll, ll> pll ; 44 typedef map <int, int> mii ; 45 typedef map <string, int> msi ; 46 typedef map <ll, ll> mll ; 47 48 const int N = 100010 ; 49 const double eps = 1e-8 ; 50 const int iinf = INT_MAX ; 51 const ll linf = 2e18 ; 52 const double dinf = 1e30 ; 53 const int MOD = 1000000007 ; 54 55 inline int read(){ 56 int X = 0, w = 0 ; 57 char ch = 0 ; 58 while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; } 59 while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ; 60 return w ? - X : X ; 61 } 62 63 void write(int x){ 64 if (x < 0) putchar('-'), x = - x ; 65 if (x > 9) write(x / 10) ; 66 putchar(x % 10 + '0') ; 67 } 68 69 void print(int x) { 70 cout << x << endl ; 71 exit(0) ; 72 } 73 74 void PRINT(string x) { 75 cout << x << endl ; 76 exit(0) ; 77 } 78 79 void douout(double x){ 80 printf("%lf\n", x + 0.0000000001) ; 81 } 82 83 int n, m ; 84 int fa[N], sz[N], vis[N], ans[N] ; 85 86 struct roads {int d, id ;} a[N] ; 87 struct boots {int d, s, id ;} b[N] ; 88 bool cmp1(roads a, roads b) {return a.d > b.d ;} 89 bool cmp2(boots a, boots b) {return a.d > b.d ;} 90 91 int find(int x) { 92 return fa[x] == x ? x : fa[x] = find(fa[x]) ; 93 } 94 95 void Merge(int x, int y) { 96 sz[y] += sz[x] ; fa[x] = y ; 97 } 98 99 signed main(){ 100 freopen("snowboots.in", "r", stdin) ; 101 freopen("snowboots.out", "w", stdout) ; 102 scanf("%d%d", &n, &m) ; 103 for (int i = 1; i <= n; i++) scanf("%d", &a[i].d), a[i].id = i ; 104 for (int i = 1; i <= m; i++) scanf("%d %d", &b[i].d, &b[i].s), b[i].id = i ; 105 sort(a + 1, a + n + 1, cmp1) ; 106 sort(b + 1, b + m + 1, cmp2) ; 107 for (int i = 1; i <= n; i++) fa[i] = i, sz[i] = 1 ; 108 int cnt = 1, dis = 0 ; 109 for (int i = 1; i <= m; i++) { 110 while (cnt <= n && a[cnt].d > b[i].d) { 111 int x = a[cnt].id ; vis[x] = 1 ; 112 if (vis[x - 1]) Merge(x - 1, x) ; 113 if (vis[x + 1]) Merge(x, find(x + 1)) ; 114 dis = max(dis, sz[find(x)]) ; 115 cnt++ ; 116 } 117 if (dis < b[i].s) ans[b[i].id] = 1 ; 118 } 119 for (int i = 1; i <= m; i++) printf("%d\n", ans[i]) ; 120 } 121 122 /* 123 写代码时请注意: 124 1.是否要开Long Long?数组边界处理好了么? 125 2.实数精度有没有处理? 126 3.特殊情况处理好了么? 127 4.做一些总比不做好。 128 思考提醒: 129 1.最大值和最小值问题可不可以用二分答案? 130 2.有没有贪心策略?否则能不能dp? 131 */
P3 Taming the Herd
我看了一眼T2,感觉不会做,就看T3.这个T3一眼看感觉就是dp把,然后数据范围提示是$O(n^3)$
思考了10分钟,大概想出来了一个状态:
$f[i][j]$表示在前ii个里面经历$k$次出逃可以取到最少的修改数
转移什么的就太简单了不说了自己看代码吧
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <bitset> 9 #include <cstdio> 10 #include <cctype> 11 #include <string> 12 #include <cstring> 13 #include <cassert> 14 #include <climits> 15 #include <cstdlib> 16 #include <iostream> 17 #include <algorithm> 18 #include <functional> 19 using namespace std ; 20 21 #define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) 22 #define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++) 23 #define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) 24 #define clr(a) memset(a, 0, sizeof(a)) 25 #define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp) 26 #define ass(a, sum) memset(a, sum, sizeof(a)) 27 28 #define ls ((rt) << 1) 29 #define rs ((rt) << 1 | 1) 30 #define lowbit(x) (x & -x) 31 #define mp make_pair 32 #define pb push_back 33 #define fi first 34 #define se second 35 #define endl '\n' 36 #define ENDL cout << endl 37 #define SZ(x) ((int)x.size()) 38 39 typedef long long ll ; 40 typedef unsigned long long ull ; 41 typedef vector <int> vi ; 42 typedef pair <int, int> pii ; 43 typedef pair <ll, ll> pll ; 44 typedef map <int, int> mii ; 45 typedef map <string, int> msi ; 46 typedef map <ll, ll> mll ; 47 48 const int N = 110 ; 49 const double eps = 1e-8 ; 50 const int iinf = INT_MAX ; 51 const ll linf = 2e18 ; 52 const double dinf = 1e30 ; 53 const int MOD = 1000000007 ; 54 55 inline int read(){ 56 int X = 0, w = 0 ; 57 char ch = 0 ; 58 while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; } 59 while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ; 60 return w ? - X : X ; 61 } 62 63 void write(int x){ 64 if (x < 0) putchar('-'), x = - x ; 65 if (x > 9) write(x / 10) ; 66 putchar(x % 10 + '0') ; 67 } 68 69 void print(int x) { 70 cout << x << endl ; 71 exit(0) ; 72 } 73 74 void PRINT(string x) { 75 cout << x << endl ; 76 exit(0) ; 77 } 78 79 void douout(double x){ 80 printf("%lf\n", x + 0.0000000001) ; 81 } 82 83 int n ; 84 int a[N], b[N][N], dp[N][N] ; 85 86 signed main(){ 87 freopen("taming.in", "r", stdin) ; 88 freopen("taming.out", "w", stdout) ; 89 scanf("%d", &n) ; 90 for (int i = 1; i <= n; i++) scanf("%d", &a[i]) ; 91 for (int i = 0; i <= n; i++) { 92 int cnt = 0 ; 93 for (int j = i; j <= n; j++) cnt += (a[j] != j - i), b[i][j] = cnt ; 94 } 95 ass(dp, 0x3f) ; 96 dp[0][0] = 0 ; 97 for (int i = 0; i <= n; i++) 98 for (int j = 1; j <= n; j++) 99 for (int k = i + 1; k <= n; k++) 100 dp[k][j] = min(dp[k][j], dp[i][j - 1] + b[i + 1][k]) ; 101 for (int i = 1; i <= n; i++) printf("%d\n", dp[n][i]) ; 102 } 103 104 /* 105 写代码时请注意: 106 1.是否要开Long Long?数组边界处理好了么? 107 2.实数精度有没有处理? 108 3.特殊情况处理好了么? 109 4.做一些总比不做好。 110 思考提醒: 111 1.最大值和最小值问题可不可以用二分答案? 112 2.有没有贪心策略?否则能不能dp? 113 */
P2 Directory Traversal
this problem is a little difficult
即使我第一眼就看出来他是棵树(路径文件夹什么的不就是树状存储的么)
然后开始考虑贪心,发现不行,下意识的想树形dp
明显状态为 f[i]表示以i为出发点的答案
于是开始推式子,emmmm还想不太好玩
大概想了20分钟,中间也想过到底能不能dp
最终我还是推出来啦!
f[x]=f[fat[x]]-(len[x]+1)*size[x]+3*(leave-size[x])
其中几个数组解释一下
fat[x] x的father
size[x] x的子树大小
leave 直观意思,叶子
len[x] : 文件夹长度
然后就是两遍dfs扫出答案
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #include <queue> 6 #include <stack> 7 #include <vector> 8 #include <bitset> 9 #include <cstdio> 10 #include <cctype> 11 #include <string> 12 #include <cstring> 13 #include <cassert> 14 #include <climits> 15 #include <cstdlib> 16 #include <iostream> 17 #include <algorithm> 18 #include <functional> 19 using namespace std ; 20 21 #define rep(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) 22 #define Rep(i, a, b) for (int (i) = (a) - 1; (i) < (b); (i)++) 23 #define REP(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) 24 #define clr(a) memset(a, 0, sizeof(a)) 25 #define Sort(a, len, cmp) sort(a + 1, a + len + 1, cmp) 26 #define ass(a, sum) memset(a, sum, sizeof(a)) 27 28 #define ls ((rt) << 1) 29 #define rs ((rt) << 1 | 1) 30 #define lowbit(x) (x & -x) 31 #define mp make_pair 32 #define pb push_back 33 #define fi first 34 #define se second 35 #define endl '\n' 36 #define ENDL cout << endl 37 #define SZ(x) ((int)x.size()) 38 39 typedef long long ll ; 40 typedef unsigned long long ull ; 41 typedef vector <int> vi ; 42 typedef pair <int, int> pii ; 43 typedef pair <ll, ll> pll ; 44 typedef map <int, int> mii ; 45 typedef map <string, int> msi ; 46 typedef map <ll, ll> mll ; 47 48 const int N = 100010 ; 49 const double eps = 1e-8 ; 50 const int iinf = INT_MAX ; 51 const ll linf = 2e18 ; 52 const double dinf = 1e30 ; 53 const int MOD = 1000000007 ; 54 55 inline int read(){ 56 int X = 0, w = 0 ; 57 char ch = 0 ; 58 while (!isdigit(ch)) { w |= ch == '-' ; ch = getchar() ; } 59 while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar() ; 60 return w ? - X : X ; 61 } 62 63 void write(int x){ 64 if (x < 0) putchar('-'), x = - x ; 65 if (x > 9) write(x / 10) ; 66 putchar(x % 10 + '0') ; 67 } 68 69 void print(int x) { 70 cout << x << endl ; 71 exit(0) ; 72 } 73 74 void PRINT(string x) { 75 cout << x << endl ; 76 exit(0) ; 77 } 78 79 void douout(double x){ 80 printf("%lf\n", x + 0.0000000001) ; 81 } 82 83 int n, leave ; 84 ll ans, f[N], dis[N] ; 85 int len[N], sz[N] ; 86 vector <int> son[N] ; 87 88 void dfs1(int rt) { 89 for (int i = 0; i < SZ(son[rt]); i++) { 90 int to = son[rt][i] ; 91 dis[to] = dis[rt] + len[to] + 1 ; 92 dfs1(to) ; 93 sz[rt] += sz[to] ; 94 } 95 if (!son[rt].size()) { 96 sz[rt] = 1 ; 97 dis[rt]-- ; 98 f[1] += dis[rt] ; 99 } 100 } 101 102 void dfs2(int rt) { 103 for (int i = 0; i < SZ(son[rt]); i++) { 104 int to = son[rt][i] ; 105 if (!SZ(son[to])) continue ; 106 f[to] = f[rt] - (len[to] + 1) * sz[to] + 3 * (leave - sz[to]) ; 107 ans = min(ans, f[to]) ; 108 dfs2(to) ; 109 } 110 } 111 112 int main() { 113 scanf("%d", &n) ; 114 for (int i = 1; i <= n; i++) { 115 int m ; 116 char s[20] ; 117 scanf("%s", s + 1) ; 118 len[i] = strlen(s + 1) ; 119 scanf("%d", &m) ; 120 if (!m) leave++ ; 121 for (int j = 1; j <= m; j++) { 122 int x ; 123 scanf("%d", &x) ; 124 son[i].pb(x) ; 125 } 126 } 127 dfs1(1) ; 128 ans = f[1] ; 129 dfs2(1) ; 130 printf("%lld\n", ans) ; 131 } 132 133 /* 134 写代码时请注意: 135 1.是否要开Long Long?数组边界处理好了么? 136 2.实数精度有没有处理? 137 3.特殊情况处理好了么? 138 4.做一些总比不做好。 139 思考提醒: 140 1.最大值和最小值问题可不可以用二分答案? 141 2.有没有贪心策略?否则能不能dp? 142 */
USACO的题目再练练,今天居然被第二题给打了,不爽(# ̄~ ̄#),
加油