(Incomplete) 中国大学生程序设计竞赛(杭州)
Posted 大四开始ACM
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(Incomplete) 中国大学生程序设计竞赛(杭州)相关的知识,希望对你有一定的参考价值。
反思:好好读题,一开始卡了好几道水题。多和队友交流思路。不要轻言放弃,珍惜上机机会。
A. Arcsoft\'s Office Rearrangement
方法: 贪心
注意到房子是连成一条线的,所以操作都都是对相邻的block进行或者产生相邻的block。贪心地从第一个block 开始处理。
code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include <cstdlib> 10 #include <cmath> 11 #include <set> 12 #include <list> 13 #include <deque> 14 #include <map> 15 #include <queue> 16 #include <fstream> 17 #include <cassert> 18 #include <unordered_map> 19 #include <unordered_set> 20 #include <cmath> 21 #include <sstream> 22 #include <time.h> 23 #include <complex> 24 #include <iomanip> 25 #define Max(a,b) ((a)>(b)?(a):(b)) 26 #define Min(a,b) ((a)<(b)?(a):(b)) 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) 32 #define FOREACH(a,b) for (auto &(a) : (b)) 33 #define rep(i,n) FOR(i,0,n) 34 #define repn(i,n) FORN(i,1,n) 35 #define drep(i,n) DFOR(i,n-1,0) 36 #define drepn(i,n) DFOR(i,n,1) 37 #define MAX(a,b) a = Max(a,b) 38 #define MIN(a,b) a = Min(a,b) 39 #define SQR(x) ((LL)(x) * (x)) 40 #define Reset(a,b) memset(a,b,sizeof(a)) 41 #define fi first 42 #define se second 43 #define mp make_pair 44 #define pb push_back 45 #define all(v) v.begin(),v.end() 46 #define ALLA(arr,sz) arr,arr+sz 47 #define SIZE(v) (int)v.size() 48 #define SORT(v) sort(all(v)) 49 #define REVERSE(v) reverse(ALL(v)) 50 #define SORTA(arr,sz) sort(ALLA(arr,sz)) 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) 52 #define PERMUTE next_permutation 53 #define TC(t) while(t--) 54 #define forever for(;;) 55 #define PINF 1000000000000 56 #define newline \'\\n\' 57 58 #define test if(1)if(0)cerr 59 using namespace std; 60 using namespace std; 61 typedef vector<int> vi; 62 typedef vector<vi> vvi; 63 typedef pair<int,int> ii; 64 typedef pair<double,double> dd; 65 typedef pair<char,char> cc; 66 typedef vector<ii> vii; 67 typedef long long ll; 68 typedef unsigned long long ull; 69 typedef pair<ll, ll> l4; 70 const double pi = acos(-1.0); 71 72 const int maxn = 1e5+1; 73 ll a[maxn]; 74 ll n, k; 75 76 int main() 77 { 78 ios::sync_with_stdio(false); 79 cin.tie(0); 80 int T; cin >> T; 81 for (int kase = 1; kase <= T; ++kase) 82 { 83 cin >> n >> k; 84 for (int i = 1; i <= n; ++i) 85 cin >> a[i]; 86 a[0] = 0; 87 for (int i = 1; i <= n; ++i) 88 a[i] += a[i-1]; 89 ll ans = 0; 90 if (a[n] % k != 0) 91 { 92 ans = -1; 93 } 94 else 95 { 96 ll block = a[n] / k; 97 int last = 0; 98 for (int i = 1; i <= n; ++i) 99 { 100 if (a[i] % block == 0) 101 { 102 ans += (a[i]-a[last])/block - 1; 103 last = i; 104 } 105 else 106 { 107 ans += 1; 108 } 109 } 110 } 111 cout << "Case #" << kase << ": " << ans << newline; 112 } 113 }
B. Bomb
方法:强联通分量
求出图的强联通分量,对于入度为0的强联通分量,选择最小的cost。
code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include <cstdlib> 10 #include <cmath> 11 #include <set> 12 #include <list> 13 #include <deque> 14 #include <map> 15 #include <queue> 16 #include <fstream> 17 #include <cassert> 18 #include <unordered_map> 19 #include <unordered_set> 20 #include <cmath> 21 #include <sstream> 22 #include <time.h> 23 #include <complex> 24 #include <iomanip> 25 #define Max(a,b) ((a)>(b)?(a):(b)) 26 #define Min(a,b) ((a)<(b)?(a):(b)) 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) 32 #define FOREACH(a,b) for (auto &(a) : (b)) 33 #define rep(i,n) FOR(i,0,n) 34 #define repn(i,n) FORN(i,1,n) 35 #define drep(i,n) DFOR(i,n-1,0) 36 #define drepn(i,n) DFOR(i,n,1) 37 #define MAX(a,b) a = Max(a,b) 38 #define MIN(a,b) a = Min(a,b) 39 #define SQR(x) ((LL)(x) * (x)) 40 #define Reset(a,b) memset(a,b,sizeof(a)) 41 #define fi first 42 #define se second 43 #define mp make_pair 44 #define pb push_back 45 #define all(v) v.begin(),v.end() 46 #define ALLA(arr,sz) arr,arr+sz 47 #define SIZE(v) (int)v.size() 48 #define SORT(v) sort(all(v)) 49 #define REVERSE(v) reverse(ALL(v)) 50 #define SORTA(arr,sz) sort(ALLA(arr,sz)) 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) 52 #define PERMUTE next_permutation 53 #define TC(t) while(t--) 54 #define forever for(;;) 55 #define PINF 1000000000000 56 #define newline \'\\n\' 57 58 #define test if(1)if(0)cerr 59 using namespace std; 60 using namespace std; 61 typedef vector<int> vi; 62 typedef vector<vi> vvi; 63 typedef pair<int,int> ii; 64 typedef pair<double,double> dd; 65 typedef pair<char,char> cc; 66 typedef vector<ii> vii; 67 typedef long long ll; 68 typedef unsigned long long ull; 69 typedef pair<ll, ll> l4; 70 const double pi = acos(-1.0); 71 72 const int maxn = 1e3+1; 73 int n; 74 ll r[maxn], x[maxn], y[maxn], c[maxn]; 75 ll mincost[maxn], indegree[maxn]; 76 bool adj[maxn][maxn]; 77 vector<int> g[maxn]; 78 bool connected(int u, int v) 79 { 80 ll dx = x[u]-x[v]; 81 ll dy = y[u]-y[v]; 82 return dx*dx+dy*dy <= r[u]*r[u]; 83 } 84 int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt; 85 stack<int> Stack; 86 void dfs(int u) 87 { 88 pre[u] = lowlink[u] = ++dfs_clock; 89 Stack.push(u); 90 for (int i = 0; i < g[u].size(); ++i) 91 { 92 int v = g[u][i]; 93 if (!pre[v]) 94 { 95 dfs(v); 96 lowlink[u] = min(lowlink[u], lowlink[v]); 97 } else if (!sccno[v]) 98 { 99 lowlink[u] = min(lowlink[u], pre[v]); 100 } 101 } 102 if (lowlink[u] == pre[u]) 103 { 104 scc_cnt++; 105 for(;;) 106 { 107 int x = Stack.top(); Stack.pop(); 108 sccno[x] = scc_cnt; 109 if (x == u) break; 110 } 111 } 112 } 113 void find_scc(int n) 114 { 115 dfs_clock = scc_cnt = 0; 116 memset(sccno, 0, sizeof(sccno)); 117 memset(pre, 0, sizeof(pre)); 118 for (int i = 0; i < n; ++i) 119 if (!pre[i]) dfs(i); 120 } 121 int main() 122 { 123 ios::sync_with_stdio(false); 124 cin.tie(0); 125 int T; cin >> T; 126 for (int kase = 1; kase <= T; ++kase) 127 { 128 cin >> n; 129 for (int i = 0; i < n; ++i) g[i].clear(); 130 for (int i = 0; i < n; ++i) cin >> x[i] >> y[i] >> r[i] >> c[i]; 131 for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (i != j && connected(i, j)) g[i].push_back(j); 132 find_scc(n); 133 memset(mincost, -1, sizeof(mincost)); 134 for (int i = 0; i < n; ++i) if (mincost[sccno[i]] == -1 || mincost[sccno[i]] > c[i]) mincost[sccno[i]] = c[i]; 135 memset(indegree, 0, sizeof(indegree)); 136 memset(adj, false, sizeof(adj)); 137 for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (sccno[i] != sccno[j] && !adj[sccno[i]][sccno[j]] && connected(i, j)) adj[sccno[i]][sccno[j]] = true, indegree[sccno[j]] += 1; 138 ll ans = 0; 139 for (int i = 1; i <= scc_cnt; ++i) 140 if (!indegree[i]) ans += mincost[i]; 141 cout << "Case #" << kase << ": " << ans << newline; 142 } 143 144 }
C. Car
方法:贪心
最后一段的通过时间一定是1s,根据速度的关系,可以得出 di-1/ti-1 <= di/ti, 然后可以根据ti 求出 ti-1。
code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include <cstdlib> 10 #include <cmath> 11 #include <set> 12 #include <list> 13 #include <deque> 14 #include <map> 15 #include <queue> 16 #include <fstream> 17 #include <cassert> 18 #include <unordered_map> 19 #include <unordered_set> 20 #include <cmath> 21 #include <sstream> 22 #include <time.h> 23 #include <complex> 24 #include <iomanip> 25 #define Max(a,b) ((a)>(b)?(a):(b)) 26 #define Min(a,b) ((a)<(b)?(a):(b)) 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) 32 #define FOREACH(a,b) for (auto &(a) : (b)) 33 #define rep(i,n) FOR(i,0,n) 34 #define repn(i,n) FORN(i,1,n) 35 #define drep(i,n) DFOR(i,n-1,0) 36 #define drepn(i,n) DFOR(i,n,1) 37 #define MAX(a,b) a = Max(a,b) 38 #define MIN(a,b) a = Min(a,b) 39 #define SQR(x) ((LL)(x) * (x)) 40 #define Reset(a,b) memset(a,b,sizeof(a)) 41 #define fi first 42 #define se second 43 #define mp make_pair 44 #define pb push_back 45 #define all(v) v.begin(),v.end() 46 #define ALLA(arr,sz) arr,arr+sz 47 #define SIZE(v) (int)v.size() 48 #define SORT(v) sort(all(v)) 49 #define REVERSE(v) reverse(ALL(v)) 50 #define SORTA(arr,sz) sort(ALLA(arr,sz)) 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) 52 #define PERMUTE next_permutation 53 #define TC(t) while(t--) 54 #define forever for(;;) 55 #define PINF 1000000000000 56 #define newline \'\\n\' 57 58 #define test if(1)if(0)cerr 59 using namespace std; 60 using namespace std; 61 typedef vector<int> vi; 62 typedef vector<vi> vvi; 63 typedef pair<int,int> ii; 64 typedef pair<double,double> dd; 65 typedef pair<char,char> cc; 66 typedef vector<ii> vii; 67 typedef long long ll; 68 typedef unsigned long long ull; 69 typedef pair<ll, ll> l4; 70 const double pi = acos(-1.0); 71 72 const int maxn = 1e5+1; 73 int a[maxn]; 74 int n; 75 76 int main() 77 { 78 ios::sync_with_stdio(false); 79 cin.tie(0); 80 int T; cin >> T; 81 for (int kase = 1; kase <= T; ++kase) 82 { 83 cin >> n; 84 a[0] = 0; 85 for (int i = 1; i <= n; ++i) cin >> a[i]; 86 int ans = 1; 87 int last = 1; 88 for (int i = n-1; i >= 1; --i) 89 { 90 int t = ceil(1.0*last*(a[i]-a[i-1])/(a[i+1]-a[i])); 91 ans += t; 92 last = t; 93 } 94 cout << "Case #" << kase << ": " << ans << newline; 95 } 96 }
D. Difference
方法:Meet in the Middle, 搜索
推导可知,y < 1e10,可以将y分成前半部分和后半部分。对于每一个k,预处理0-99999 的f[k][i]。对于一组input(x,k),枚举前半部分a*1e5(或者后半部分),将f(a*1e5, k) - a*1e5 储存到一个hashtable里(f(a*1e5, k) = f(a, k) = f[k][a]),再枚举另一半,f(b, k) - b, 观察 x-(f(b,k)-b) 是否在hashtable 中,更新答案。注意,如果x为0,最终答案要减1,因为要排除y = 0*1e5 + 0的情况。
code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <string> 6 #include <vector> 7 #include <stack> 8 #include <bitset> 9 #include <cstdlib> 10 #include <cmath> 11 #include <set> 12 #include <list> 13 #include <deque> 14 #include <map> 15 #include <queue> 16 #include <fstream> 17 #include <cassert> 18 #include <unordered_map> 19 #include <unordered_set> 20 #include <cmath> 21 #include <sstream> 22 #include <time.h> 23 #include <complex> 24 #include <iomanip> 25 #define Max(a,b) ((a)>(b)?(a):(b)) 26 #define Min(a,b) ((a)<(b)?(a):(b)) 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a)) 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a)) 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a)) 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a)) 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a)) 32 #define FOREACH(a,b) for (auto &(a) : (b)) 33 #define rep(i,n) FOR(i,0,n) 34 #define repn(i,n) FORN(i,1,n) 35 #define drep(i,n) DFOR(i,n-1,0) 36 #define drepn(i,n) DFOR(i,n,1) 37 #define MAX(a,b) a = Max(a,b) 38 #define MIN(a,b) a = Min(a,b) 39 #define SQR(x) ((LL)(x) * (x)) 40 #define Reset(a,b) memset(a,b,sizeof(a)) 41 #define fi first 42 #define se second 43 #define mp make_pair 44 #define pb push_back 45 #define all(v) v.begin(),v.end() 46 #define ALLA(arr,sz) arr,arr+sz 47 #define SIZE(v) (int)v.size() 48 #define SORT(v) sort(all(v)) 49 #define REVERSE(v) reverse(ALL(v)) 50 #define SORTA(arr,sz) sort(ALLA(arr,sz)) 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz)) 52 #define PERMUTE next_permutation 53 #define TC(t) while(t--) 54 #define forever for(;;) 55 #define PINF 1000000000000 56 #define newline \'\\n\' 57 58 #define test if(1)if(0)cerr 59 using namespace std; 60 using namespace std; 61 typedef vector<int> vi; 62 typedef vector<vi> vvi; 63 typedef pair<int,int> ii; 64 typedef pair<double,double> dd; 65 typedef pair<char,char> cc; 66 typedef vector<ii> vii; 67 typedef long long ll; 68 typedef unsigned long long ull; 69 typedef pair<ll, ll> l4; 70 const double pi = acos(-1.0); 71 72 const int maxb = 1e5; 73 ll powers[10][10]; 74 ll f[10][maxb+1]; 75 ll x, k; 76 unordered_map<ll, int> hashint; 77 int main() 78 { 79 ios::sync_with_stdio(false); 80 cin.tie(0); 81以上是关于(Incomplete) 中国大学生程序设计竞赛(杭州)的主要内容,如果未能解决你的问题,请参考以下文章