1. 程式人生 > >USACO 2018 February Contest, Gold 總結

USACO 2018 February Contest, Gold 總結

這一場對我來說要比上一場稍難一點,

P1 Snow Boots

還是一如既往地簡單,有些人用什麼線段樹,整體二分,單調佇列,你們腦子壞了麼?

為什麼不用並查集呢!!!

先都按S排序

每次把不能走的染黑,如果任意一段連續的黑色個數小於< d的話我就可以通過,否則不行。

用並查集來維護最大子段和。

時間複雜度$O(nlogn)$

  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 */
AC CODE

P3 Taming the Herd

我看了一眼T2,感覺不會做,就看T3.這個T3一眼看感覺就是dp把,然後資料範圍提示是$O(n^3)$

思考了10分鐘,大概想出來了一個狀態:   $f[i][j]$表示在前ii個裡面經歷$k$次出逃可以取到最少的修改數   轉移什麼的就太簡單了不說了自己看程式碼吧
  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 */
AC CODE

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掃出答案

  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 */
AC CODE

USACO的題目再練練,今天居然被第二題給打了,不爽(# ̄~ ̄#),

加油