PAT 甲级真题题解(63-120)
1063 Set Similarity
n个序列分别先放进集合里去重。在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分子。
1 // 1063 Set Similarity 2 #include <set> 3 #include <map> 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 set <int> se[55]; 10 11 int main() { 12 int n, k, m; 13 scanf("%d", &n); 14 for(int i = 1; i <= n; i++) { 15 scanf("%d", &m); 16 while(m--) { 17 scanf("%d", &k); 18 se[i].insert(k); 19 } 20 } 21 scanf("%d", &k); 22 while(k--) { 23 int id1, id2; 24 double cnt = 0, total = 0; 25 scanf("%d %d", &id1, &id2); 26 for(int i : se[id1]) { 27 if(se[id2].find(i) != se[id2].end()) cnt++; 28 } 29 total = se[id1].size() + se[id2].size() - cnt; 30 printf("%.1f%\n", cnt * 100 / total); 31 } 32 return 0; 33 }
1064 Complete Binary Search Tree
按照中序遍历建树。对应根节点依次填入排序好的数即可。
1 // 1064 Complete Binary Search Tree 2 #include <queue> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1234; 9 int a[N], level[N], n, cnt = 0; 10 11 void solve(int root) { 12 if(root > n) return ; 13 solve(2 * root); 14 level[root] = a[++cnt]; 15 solve(2 * root + 1); 16 } 17 18 int main() { 19 scanf("%d", &n); 20 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 21 sort(a + 1, a + 1 + n); 22 solve(1); 23 for(int i = 1; i <= n; i++) { 24 if(i != 1) printf(" "); 25 printf("%d", level[i]); 26 } 27 return 0; 28 }
1065 A+B and C (64bit)
对溢出的情况特判一下。
1 // 1065 A+B and C (64bit) 2 #include <iostream> 3 using namespace std; 4 5 typedef long long ll; 6 7 int main() { 8 int t; 9 scanf("%d", &t); 10 for(int i = 1; i <= t; i++) { 11 ll a, b, c, sum = 0; 12 scanf("%lld %lld %lld", &a, &b, &c); 13 sum = a + b; 14 if(a > 0 && b > 0 && sum <= 0) { 15 printf("Case #%d: true\n", i); 16 } else if(a < 0 && b < 0 && sum >= 0) { 17 printf("Case #%d: false\n", i); 18 } else if(sum > c) { 19 printf("Case #%d: true\n", i); 20 } else { 21 printf("Case #%d: false\n", i); 22 } 23 } 24 return 0; 25 }
1066 Root of AVL Tree
AVL模板题
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef struct node; 5 typedef node * tree; 6 7 struct node{ 8 int v, heigh; 9 tree L,R; 10 }; 11 12 int getheigh(tree root){ 13 if(root==NULL) return 0; 14 return root->heigh; 15 } 16 17 void updataheigh(tree root){ 18 root->heigh=max(getheigh(root->L),getheigh(root->R))+1; 19 } 20 21 int getBalance(tree root){ 22 return getheigh(root->L)-getheigh(root->R); 23 } 24 25 void L(tree &root){ 26 tree temp; 27 temp=root->R; 28 root->R=temp->L; 29 temp->L=root; 30 updataheigh(root); 31 updataheigh(temp); 32 root=temp; 33 } 34 35 void R(tree &root){ 36 tree temp; 37 temp=root->L; 38 root->L=temp->R; 39 temp->R=root; 40 updataheigh(root); 41 updataheigh(temp); 42 root=temp; 43 } 44 45 void insertt(tree &root,int v){ 46 if(root==NULL){ 47 root=new node; 48 root->v=v; 49 root->heigh=1; 50 root->L=root->R=NULL; 51 return; 52 } 53 if(v<root->v){ 54 insertt(root->L,v); 55 updataheigh(root); 56 if(getBalance(root)==2){ 57 if(getBalance(root->L)==1){ 58 R(root); 59 } 60 else if(getBalance(root->L)==-1){ 61 L(root->L); 62 R(root); 63 } 64 } 65 } 66 else{ 67 insertt(root->R,v); 68 updataheigh(root); 69 if(getBalance(root)==-2){ 70 if(getBalance(root->R)==-1){ 71 L(root); 72 } 73 else if(getBalance(root->R)==1){ 74 R(root->R); 75 L(root); 76 } 77 } 78 } 79 80 } 81 82 int main(){ 83 int n; 84 scanf("%d",&n); 85 int x; 86 tree root; 87 root=NULL; 88 for(int i=0;i<n;i++){ 89 scanf("%d",&x); 90 insertt(root,x); 91 } 92 printf("%d\n",root->v); 93 return 0; 94 }
1067 Sort with Swap(0, i)
看到表排序的知识,摘录下。
表排序思想:N个数字的排列由若干个独立的环组成
分成以下三种环:
1. 环内只有一个元素,即该数已经在正确的位置上,不需要交换。
2. 环内有 n 个元素,含 0,需要交换次数是:n-1 (每一个非 0 元素都要和 0 换一下)
3. 环内有 n 个元素,不含 0,需要交换的次数是:n+1(把 0 换入换出 + 其余 n-1 个非 0 元素和 0 换一下)
1 // 1067 Sort with Swap(0, i) 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 const int N = 1e5 + 10; 8 bool vis[N]; 9 int pos[N]; 10 11 int main() { 12 int n, u, ans = 0; 13 scanf("%d", &n); 14 for(int i = 0; i < n; i++) { 15 scanf("%d", &u); 16 pos[u] = i; 17 } 18 for(int i = 0; i < n; i++) { 19 if(pos[i] != i && !vis[i]) { 20 int cnt = 0, j = i; 21 do { 22 cnt++; 23 vis[j] = true; 24 if(j == 0) cnt -= 2; 25 j = pos[j]; 26 }while(j != i); 27 cnt++; 28 ans += cnt; 29 } 30 } 31 printf("%d\n", ans); 32 return 0; 33 }
1068 Find More Coins
因题目特殊的输出要求,用01背包的时候需要先用大的数填,不然如果先用小的数填,小的数会影响后面的数,最后从小的往大的拿出去即可。
1 // 1068 Find More Coins 2 #include <vector> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e4 + 10; 9 int n, m; 10 int a[N], dp[N]; 11 vector <int> ans; 12 bool check[N][123]; 13 14 int main() { 15 scanf("%d %d", &n, &m); 16 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 17 sort(a + 1, a + 1 + n); 18 for(int i = n; i >= 1; i--) { 19 for(int j = m; j >= a[i]; j--) { 20 if(dp[j - a[i]] + a[i] >= dp[j]) { 21 dp[j] = dp[j - a[i]] + a[i]; 22 check[i][j] = 1; 23 } 24 } 25 } 26 if(dp[m] != m) { 27 printf("No Solution\n"); 28 return 0; 29 } 30 int id = 1; 31 while(m > 0) { 32 if(check[id][m]) { 33 ans.push_back(a[id]); 34 m -= a[id]; 35 } 36 id++; 37 } 38 for(int i = 0; i < ans.size(); i++) { 39 if(i != 0) printf(" ", ans[i]); 40 printf("%d", ans[i]); 41 } 42 return 0; 43 }
1069 The Black Hole of Numbers
按题目模拟即可。
1 // 1069 The Black Hole of Numbers 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 int a[5]; 9 bool cmp(int a, int b) {return a > b;} 10 11 int cal1(int n) { 12 memset(a, 0, sizeof(a)); 13 int cnt = 0, res = 0; 14 while(n) { 15 a[++cnt] = n % 10; 16 n /= 10; 17 } 18 sort(a + 1, a + 1 + 4); 19 for(int i = 4; i >= 1; i--) { 20 res = res * 10 + a[i]; 21 } 22 return res; 23 } 24 25 int cal2() { 26 int res = 0; 27 sort(a + 1, a + 1 + 4); 28 for(int i = 1; i <= 4; i++) { 29 res = res * 10 + a[i]; 30 } 31 return res; 32 33 } 34 35 int main() { 36 int n; 37 scanf("%d", &n); 38 while(1) { 39 int p1 = cal1(n); 40 int p2 = cal2(); 41 n = p1 - p2; 42 printf("%04d - %04d = %04d\n", p1, p2, n); 43 if(n == 6174 || n == 0) return 0; 44 } 45 return 0; 46 }
1070 Mooncake
按每吨多少钱从大到小排序下,再取即可。
1 // 1070 Mooncake 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 const int N = 1234; 8 9 struct node{ 10 double storage, price; 11 }moon[N]; 12 13 bool cmp(node x, node y) { 14 return x.price * y.storage > y.price * x.storage; 15 } 16 17 int main() { 18 int n; 19 double d, ans = 0; 20 scanf("%d %lf", &n, &d); 21 for(int i = 1; i <= n; i++) scanf("%lf", &moon[i].storage); 22 for(int i = 1; i <= n; i++) scanf("%lf", &moon[i].price); 23 sort(moon + 1, moon + 1 + n, cmp); 24 for(int i = 1; i <= n; i++) { 25 if(d >= moon[i].storage) { 26 ans += moon[i].price; 27 d -= moon[i].storage; 28 } else { 29 ans += moon[i].price * (1.0 * d / moon[i].storage); 30 break; 31 } 32 } 33 printf("%.2f\n", ans); 34 return 0; 35 }
1071 Speech Patterns
用map存下。更新答案即可。
1 // 1071 Speech Patterns 2 #include <map> 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 7 string in, tmp, ans1; 8 map<string, int> mp; 9 map<string, int>::iterator it; 10 11 int main(){ 12 int ans2 = 0; 13 getline(cin, in); 14 for(int i = 0 ; i < in.size(); i++) { 15 if(isalnum(in[i])) { 16 tmp += tolower(in[i]); 17 if(i == in.size() - 1) mp[tmp]++; 18 } else { 19 if(tmp.size()) mp[tmp]++; 20 tmp.clear(); 21 } 22 } 23 for(it = mp.begin(); it!=mp.end(); it++){ 24 if(ans2 < it->second){ 25 ans2 = it -> second; 26 ans1 = it -> first; 27 }else if(ans2 == it -> second && ans1 > it -> first){ 28 ans1 = it -> first; 29 } 30 } 31 cout << ans1 << " " << ans2 << endl; 32 }
1072 Gas Station
以加油站为起点,跑最短路即可。
1 //1072 Gas Station 2 #include <queue> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N=1e3+100; 10 const int INF=0x3f3f3f3f;; 11 int n,m,k,ds,mp[N][N],dis[N],vis[N]; 12 13 int toint(char s[]){ 14 int sum=0; 15 int len=strlen(s); 16 for(int i=0;i<len;i++){ 17 if(s[i]=='G') continue; 18 sum=sum*10+s[i]-'0'; 19 } 20 if(s[0]=='G') return sum+n; 21 return sum; 22 } 23 24 void Dijkstra(int v){ 25 fill(vis,vis+N,false); 26 fill(dis,dis+N,INF); 27 for(int i=1;i<=n+m;i++) dis[i]=mp[v][i]; 28 dis[v]=0; 29 for(int i=0;i<n+m;i++){ 30 int u=-1,minn=INF; 31 for(int j=1;j<=n+m;j++){ 32 if(!vis[j]&&minn>dis[j]){ 33 u=j;minn=dis[j]; 34 } 35 } 36 vis[u]=true; 37 for(int j=1;j<=n+m;j++){ 38 if(!vis[j]&&dis[j]>mp[u][j]+dis[u]){ 39 dis[j]=mp[u][j]+dis[u]; 40 } 41 } 42 } 43 } 44 int main(){ 45 int num,a,b; 46 char s1[5],s2[5]; 47 scanf("%d %d %d %d",&n,&m,&k,&ds); 48 fill(mp[0],mp[0]+N*N,INF); 49 for(int i=0;i<k;i++){ 50 scanf("%s %s %d",s1,s2,&num); 51 a=toint(s1),b=toint(s2); 52 mp[a][b]=mp[b][a]=num; 53 } 54 int id=-1; 55 double maxDis=-1,minAvg=INF; 56 for(int i=n+1;i<=n+m;i++){ 57 Dijkstra(i); 58 double avg=0; 59 double Min=INF; 60 bool flag=false; 61 for(int j=1;j<=n;j++){ 62 if(dis[j]>ds){ 63 flag=true; 64 break; 65 } 66 if(Min>dis[j]){ 67 Min=dis[j]; 68 } 69 avg+=1.0*dis[j]/n; 70 } 71 if(flag) continue; 72 if(maxDis<Min){ 73 maxDis=Min; 74 id=i; 75 minAvg=avg; 76 } 77 else if(maxDis==Min&&minAvg>avg){ 78 id=i; 79 minAvg=avg; 80 } 81 82 } 83 if(id==-1) printf("No Solution\n"); 84 else{ 85 printf("G%d\n",id-n); 86 printf("%.1f %.1f\n",maxDis,minAvg); 87 } 88 return 0; 89 }
1073 Scientific Notation
按题意模拟即可。
1 // 1073 Scientific Notation 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e4 + 10; 9 char s[N]; 10 11 int main() { 12 int n, i = 0; 13 scanf("%s", s); 14 n = strlen(s); 15 while(s[i] != 'E') { 16 i++; 17 } 18 i++; 19 if(s[0] != '+') printf("%c", s[0]); 20 int m = 0; 21 for(int j = i + 1; j < n; j++) m = 10 * m + (s[j] - '0'); 22 if(s[i] == '+') { 23 m+=2; 24 for(int j = 1; j < i - 1; j++) { 25 m--; 26 if(m == -1) printf("."); 27 if(s[j] != '.') printf("%c", s[j]); 28 } 29 while(m > 0) { 30 printf("0"); 31 m--; 32 } 33 } else { 34 m--; 35 printf("0."); 36 while(m > 0) { 37 printf("0"); 38 m--; 39 } 40 for(int j = 1; j < i - 1; j++) { 41 if(s[j] != '.') printf("%c", s[j]); 42 } 43 } 44 return 0; 45 }
1074 Reversing Linked List
每k个长度翻转一次。$j$位置对应交换的位置为$(2 * i + k - j - 1)$,这个只使用前半和后半交换,注意特判跳出。
1 //1074 Reversing Linked List 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e5 + 10; 9 int nxt[N], val[N], res[N]; 10 11 int main() { 12 int root, n, k, cnt = 0; 13 scanf("%d%d%d", &root, &n, &k); 14 for(int i = 1; i <= n; i++) { 15 int u, v, w; 16 scanf("%d%d%d", &u, &v, &w); 17 val[u] = v; nxt[u] = w; 18 } 19 while(root != -1) { 20 res[cnt++] = root; 21 root = nxt[root]; 22 } 23 for(int i = 0; i + k <= cnt; i += k) { 24 for(int j = i; j < (i + k); j++) { 25 if(j >= (2 * i + k - j - 1)) break; 26 swap(res[(2 * i + k - j - 1)], res[j]); 27 } 28 } 29 for(int i = 0; i < cnt - 1; i++) { 30 printf("%05d %d %05d\n", res[i], val[res[i]], res[i + 1]); 31 } 32 printf("%05d %d -1\n", res[cnt - 1], val[res[cnt - 1]]); 33 return 0; 34 }
1075 PAT Judge
结构体排序,注意一下区别提交得0分和提交没有通过编译。
1 // 1075 PAT Judge 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e5 + 10; 9 int v[6]; 10 int n, k, m; 11 struct node{ 12 int q[6], id, sum, ok, perfect; 13 }p[N]; 14 15 void init() { 16 for(int i = 1; i <= n; i++) 17 for(int j = 1; j <= k; j++) 18 p[i].q[j] = -2, p[i].id = i; 19 } 20 21 bool cmp(node x, node y) { 22 if(x.sum == y.sum) { 23 if(x.perfect == y.perfect) return x.id < y.id; 24 return x.perfect > y.perfect; 25 } 26 return x.sum > y.sum; 27 } 28 29 int main() { 30 int r = 1; 31 scanf("%d %d %d", &n, &k, &m); 32 for(int i = 1; i <= k; i++) scanf("%d", &v[i]); 33 init(); 34 for(int i = 1; i <= m; i++) { 35 int uid, pid, val; 36 scanf("%d %d", &uid, &pid); 37 scanf("%d", &val); 38 p[uid].q[pid] = max(p[uid].q[pid], val); 39 } 40 for(int i = 1; i <= n; i++) { 41 int cnt = 0, perfect = 0; 42 bool ok = 0; 43 for(int j = 1; j <= k; j++) { 44 if(p[i].q[j] != -1 && p[i].q[j] != -2) cnt += p[i].q[j], ok = 1; 45 if(p[i].q[j] == v[j]) perfect++; 46 } 47 if(ok) p[i].ok = 1; 48 else p[i].ok = 0; 49 p[i].sum = cnt; 50 p[i].perfect = perfect; 51 } 52 sort(p + 1, p + 1 + n, cmp); 53 for(int i = 1; i <= n; i++) { 54 if(p[i].ok == 0) continue; 55 if(p[i].sum != p[i - 1].sum) r = i; 56 printf("%d %05d %d", r, p[i].id, p[i].sum); 57 for(int j = 1; j <= k; j++) { 58 if(p[i].q[j] == -2) printf(" -"); 59 else if(p[i].q[j] == -1) printf(" 0"); 60 else printf(" %d",p[i].q[j]); 61 } 62 printf("\n"); 63 } 64 return 0; 65 }
1076 Forwards on Weibo
DFS或BFS,DFS的时候注意剪枝。
DFS版本
1 //1076 Forwards on Weibo 2 #include <vector> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1234; 9 vector <int> E[N]; 10 bool vis[N]; 11 int layer[N]; 12 int n, m, k, u, sz, ans; 13 14 void dfs(int u, int l) { 15 vis[u] = 1; 16 layer[u] = l; 17 if(l <= k) { 18 for(int i = 0; i < E[u].size(); i++) { 19 int v = E[u][i]; 20 if(!vis[v] || layer[v] > l + 1) { 21 dfs(v, l + 1); 22 } 23 } 24 } 25 } 26 27 int main() { 28 scanf("%d %d", &n, &k); 29 for(int i = 1; i <= n; i++) { 30 scanf("%d", &sz); 31 while(sz) { 32 sz--; 33 scanf("%d", &u); 34 E[u].push_back(i); 35 } 36 } 37 scanf("%d", &m); 38 while(m--) { 39 for(int i = 1; i <= n; i++) vis[i] = 0, layer[i] = 0; 40 scanf("%d", &u); 41 ans = 0; 42 dfs(u, 1); 43 for(int i = 1; i <= n; i++) if(layer[i] > 0) ans++; 44 printf("%d\n", ans - 1); 45 } 46 return 0; 47 }
BFS版本
1 //1076 Forwards on Weibo 2 #include <queue> 3 #include <vector> 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N = 1234; 10 vector <int> E[N]; 11 bool vis[N]; 12 int n, m, k, u, sz; 13 14 void bfs() { 15 int ans = 0; 16 for(int i = 1; i <= n; i++) vis[i] = 0; 17 queue < pair<int,int> > Q; 18 Q.push(make_pair(u, 0)); 19 vis[u] = 1; 20 while(!Q.empty()) { 21 int cur = Q.front().first; 22 int layer = Q.front().second; 23 Q.pop(); 24 if(layer >= k) continue; 25 for(int i = 0; i < E[cur].size(); i++) { 26 int v = E[cur][i]; 27 if(!vis[v]) { 28 ans++; 29 vis[v] = 1; 30 Q.push(make_pair(v, layer + 1)); 31 } 32 } 33 } 34 printf("%d\n", ans); 35 } 36 37 int main() { 38 scanf("%d %d", &n, &k); 39 for(int i = 1; i <= n; i++) { 40 scanf("%d", &sz); 41 while(sz) { 42 sz--; 43 scanf("%d", &u); 44 E[u].push_back(i); 45 } 46 } 47 scanf("%d", &m); 48 while(m--) { 49 scanf("%d", &u); 50 bfs(); 51 } 52 return 0; 53 }
1077 Kuchiguse
模拟
1 //1077 Kuchiguse 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 int id[110]; 8 char s[110][260]; 9 10 int main() { 11 int n, cnt = 1; 12 scanf("%d", &n); 13 getchar(); 14 for(int i = 1; i <= n; i++) { 15 char c; 16 while(scanf("%c", &c) != EOF && c != '\n') { 17 s[i][id[i]++] = c; 18 } 19 } 20 while(1) { 21 for(int i = 2; i <= n; i++) { 22 if( ((id[i] - cnt) < 0) || (s[i][id[i] - cnt] != s[1][id[1] - cnt]) ) { 23 cnt--; 24 if(cnt == 0) printf("nai"); 25 else { 26 for(int i = id[1] - cnt; i < id[1] ; i++) { 27 printf("%c", s[1][i]); 28 } 29 } 30 return 0; 31 } 32 } 33 cnt++; 34 } 35 return 0; 36 }
1078 Hashing
Hash二次探测。0^2,1^2,2^2...。
1 //1078 Hashing 2 #include <iostream> 3 using namespace std; 4 5 const int N = 1e4 + 10; 6 bool vis[N]; 7 8 bool check(int x) { 9 if(x < 2) return false; 10 for(int i = 2; i * i <= x; i++) { 11 if(x % i == 0) return false; 12 } 13 return true; 14 } 15 16 int main() { 17 int n, m, p; 18 cin >> m >> n; 19 while(!check(m)) {m++;} 20 for(int i = 1; i <= n; i++) { 21 bool f = 0; 22 cin >> p; 23 if(i != 1) printf(" "); 24 for(int i = 0; i < m; i++) { 25 int tmp = (p + i * i) % m; 26 if(!vis[tmp]) { 27 f = 1; 28 printf("%d", tmp); 29 vis[tmp] = 1; 30 break; 31 } 32 } 33 if(!f) printf("-"); 34 } 35 return 0; 36 }
1079 Total Sales of Supply Chain
从根跑一遍DFS到叶子节点。统计每个叶子节点的能够产生的销售额。
1 //1079 Total Sales of Supply Chain 2 #include <vector> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 1e5 + 10; 7 int cnt[N]; 8 bool vis[N]; 9 double p, r; 10 double price[N]; 11 vector <int> E[N]; 12 13 void dfs(int u, double val) { 14 vis[u] = 1; price[u] = val; 15 for(int i = 0; i < E[u].size(); i++) { 16 int v = E[u][i]; 17 if(!vis[v]) dfs(v, val * (1 + r / 100)); 18 } 19 } 20 21 int main() { 22 int n, k, u; 23 scanf("%d %lf %lf", &n, &p, &r); 24 for(int i = 0; i < n; i++) { 25 scanf("%d", &k); 26 if(k == 0){ 27 scanf("%d", &cnt[i]); 28 } else { 29 while(k--) { 30 scanf("%d", &u); 31 E[i].push_back(u); 32 } 33 } 34 } 35 double ans = 0; 36 dfs(0, p); 37 for(int i = 0; i < n; i++) { 38 ans += price[i] * cnt[i]; 39 } 40 printf("%.1f\n", ans); 41 return 0; 42 }
1080 Graduate Admission
按照题意模拟,用set从小到大排序特性会方便很多。
1 // 1080 Graduate Admission 2 #include <set> 3 #include <cstdio> 4 #include <vector> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N = 4e4 + 10; 10 struct Stu{ 11 int Id, Ge, Gi, Gh, Rank; 12 int choices[10]; 13 }; 14 15 struct Sch{ 16 int maxn, lastRank; 17 set<int> admit; 18 }; 19 20 bool cmp(Stu x, Stu y){ 21 if(x.Gh == y.Gh) return x.Ge > y.Ge; 22 return x.Gh > y.Gh; 23 } 24 25 set<int>::iterator it; 26 27 int main(){ 28 int n, m, k; 29 scanf("%d%d%d", &n, &m, &k); 30 vector<Sch> sch(m); 31 vector<Stu> stu(n); 32 for(int i = 0; i < m; i++) scanf("%d", &sch[i].maxn); 33 for(int i = 0; i < n; i++){ 34 scanf("%d%d", &stu[i].Ge, &stu[i].Gi); 35 stu[i].Id=i; stu[i].Gh = stu[i].Ge + stu[i].Gi; 36 for(int j = 0; j < k; j++) scanf("%d", &stu[i].choices[j]); 37 } 38 sort(stu.begin(), stu.end(), cmp); 39 int Rank = 1; 40 for(int i = 0; i < n; i++){ 41 if(i==0 || (stu[i].Gh == stu[i-1].Gh && stu[i].Gi == stu[i-1].Gi)) stu[i].Rank = Rank; 42 else{ 43 Rank = i + 1; 44 stu[i].Rank = Rank; 45 } 46 } 47 for(int i = 0; i < n; i++){ 48 bool f=0; 49 for(int j = 0; j < k; j++){ 50 int schid = stu[i].choices[j]; 51 if(sch[schid].maxn > 0){ 52 sch[schid].admit.insert(stu[i].Id); 53 sch[schid].maxn--; 54 sch[schid].lastRank = stu[i].Rank; 55 f = true; 56 }else if(sch[schid].lastRank == stu[i].Rank) { 57 sch[schid].admit.insert(stu[i].Id); 58 f = true; 59 } 60 if(f) break; 61 } 62 } 63 for(int i = 0; i < m; i++){ 64 for(it = sch[i].admit.begin(); it!=sch[i].admit.end(); it++){ 65 if(it == sch[i].admit.begin()) printf("%d", *it); 66 else printf(" %d", *it); 67 } 68 printf("\n"); 69 } 70 return 0; 71 }
1081 Rational Sum
通分,注意最后若答案为0的时的格式输出。
1 // 1081 Rational Sum 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 typedef long long ll; 7 ll a[123], b[123]; 8 9 int main() { 10 ll n, nr = 0, dm = 1; 11 scanf("%lld", &n); 12 for(ll i = 1; i <= n; i++) { 13 scanf("%lld/%lld", &a[i], &b[i]); 14 dm = b[i] / __gcd(b[i], dm) * dm; 15 } 16 for(ll i = 1; i<= n; i++) { 17 nr = nr + (dm / b[i]) * a[i]; 18 } 19 ll g = __gcd(nr, dm); 20 if(g != 0) nr /= g, dm /= g; 21 if(nr / dm) { 22 printf("%lld", nr / dm); 23 nr = nr - (nr / dm) * dm; 24 if(nr != 0) printf(" %lld/%lld", nr, dm); 25 return 0; 26 } 27 nr = nr - (nr / dm) * dm; 28 if(nr != 0) printf("%lld/%lld", nr, dm); 29 else printf("0"); 30 return 0; 31 }
1082 Read Number in Chinese
模拟,注意细节即可。
1 // 1082 Read Number in Chinese 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 8 string a[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; 9 string b[10] = {"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"}; 10 11 int main() { 12 int n, f = 0; 13 string s; 14 cin >> s; 15 if(s[0] == '-') { 16 cout << "Fu" << " "; 17 s = s.substr(1); 18 } 19 n = s.size(); 20 reverse(s.begin(), s.end()); 21 for(int i = n - 1; i >= 1; i--) { 22 if(i == n - 1) { 23 cout << a[s[i] - '0'] << " " << b[i]; 24 } else { 25 if(s[i] != '0') { 26 if(f) { 27 cout << " " << a[0]; 28 f = 0; 29 } 30 cout << " " << a[s[i] - '0'] << " " << b[i]; 31 } else { 32 f = 1; 33 if(i == 4) cout << " " << b[4]; 34 } 35 } 36 } 37 if(f && s[0] != '0') { 38 cout << " " << a[0]; 39 } 40 if(s.size() == 1) { 41 cout << a[s[0] - '0']; 42 } 43 else if(s.size() > 1 && s[0] != '0'){ 44 cout << " " << a[s[0] - '0']; 45 } 46 return 0; 47 }
1083 List Grades
结构体排序
1 // 1083 List Grades 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 2e5 + 10; 7 struct node { 8 char name[15], id[15]; 9 int grade; 10 }recoder[N]; 11 12 bool cmp(node x, node y) { 13 return x.grade > y.grade; 14 } 15 16 int main() { 17 bool f = 0; 18 int n; 19 scanf("%d", &n); 20 for(int i = 1; i <= n; i++) { 21 scanf("%s %s %d", recoder[i].name, recoder[i].id, &recoder[i].grade); 22 } 23 int g1, g2; 24 scanf("%d%d", &g1, &g2); 25 sort(recoder + 1, recoder + 1 + n, cmp); 26 for(int i = 1; i <= n; i++) { 27 if(recoder[i].grade <= g2 && recoder[i].grade >= g1) { 28 printf("%s %s\n", recoder[i].name, recoder[i].id); 29 f = 1; 30 } 31 } 32 if(!f) printf("NONE\n"); 33 return 0; 34 }
map标记一下。
1 // 1084 Broken Keyboard 2 #include <map> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 map <char, int> m; 9 10 int main() { 11 char c; 12 string s1, s2; 13 cin >> s1 >> s2; 14 for(int i = 0; i < s2.size(); i++) { 15 m[s2[i]] = 1; 16 if(s2[i] >= 'a' && s2[i] <= 'z') { 17 c = s2[i] - 32; 18 m[c] = 1; 19 } 20 else if(s2[i] >= 'A' && s2[i] <= 'Z') { 21 c = s2[i] + 32; 22 m[c] = 1; 23 } 24 } 25 for(int i = 0; i < s1.size(); i++) { 26 if(!m[s1[i]]) { 27 m[s1[i]] = 1; 28 if(s1[i] >= 'a' && s1[i] <= 'z') { 29 c = s1[i] - 32; 30 m[c] = 1; 31 cout << c; 32 } 33 else if(s1[i] >= 'A' && s1[i] <= 'Z') { 34 c = s1[i] + 32; 35 m[c] = 1; 36 cout << s1[i]; 37 } else { 38 cout << s1[i]; 39 } 40 } 41 } 42 return 0; 43 }
1085 Perfect Sequence
二分搜索
1 // 1085 Perfect Sequence 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 typedef long long ll; 8 const int N = 1e5 + 10; 9 ll a[N]; 10 11 int main() { 12 int ans = 0; 13 ll n, p; 14 scanf("%lld %lld", &n, &p); 15 for(int i = 0; i < n; i++) scanf("%lld", &a[i]); 16 sort(a, a + n); 17 for(int i = 0; i < n; i++) { 18 int pos1 = lower_bound(a, a + n, a[i]) - a; 19 int pos2 = lower_bound(a, a + n, a[i] * p + 1) - a; 20 pos2--; 21 ans = max(ans, pos2 - pos1 + 1); 22 } 23 printf("%d\n", ans); 24 return 0; 25 }
1086 Tree Traversals Again
由题目可以得知push的时候为往下找,分情况讨论存左孩子还是右孩子,pop的时候表示往右走,下一次push存右孩子,最后后序遍历一下即可。
1 // 1086 Tree Traversals Again 2 #include <stack> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 char s[5]; 9 struct node{ 10 int lch, rch; 11 }tree[35]; 12 stack <int> sta; 13 int n, u, fa, root = -1, child = 0; 14 15 void postorder(int u) { 16 if(tree[u].lch != 0) postorder(tree[u].lch); 17 if(tree[u].rch != 0) postorder(tree[u].rch); 18 if(u == root) printf("%d", u); 19 else printf("%d ", u); 20 } 21 22 int main() { 23 scanf("%d", &n); 24 n *= 2; 25 while(n--) { 26 scanf("%s", s); 27 if(s[1] == 'u') { 28 scanf("%d", &u); 29 sta.push(u); 30 if(root == -1) { 31 root = u; 32 } else { 33 if(child == 0) tree[fa].lch = u; 34 else tree[fa].rch = u; 35 } 36 fa = u; 37 child = 0; 38 } else { 39 if(sta.size()) 40 fa = sta.top(); 41 sta.pop(); 42 child = 1; 43 } 44 } 45 postorder(root); 46 return 0; 47 }
1087 All Roads Lead to Rome
先Floyd得到最短路,再DFS搜,搜的过程中更新答案。
1 // 1087 All Roads Lead to Rome 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 const int N = 205; 6 int n, m, cost, cnt = 0, maxhappy = 0; 7 int happy[N], d[N][N]; 8 string s[N], s1, s2; 9 map<string, int> mp; 10 vector<int> E[N], res, ans; 11 bool vis[N]; 12 13 void dfs(int u, int en, int co) { 14 if(u == en && co == cost) { 15 int sum = 0; 16 for(int i = 0; i < res.size(); i++) { 17 sum += happy[res[i]]; 18 } 19 if(sum > maxhappy) { 20 maxhappy = sum; 21 ans.clear(); 22 for(int i = 0; i < res.size(); i++) ans.push_back(res[i]); 23 } 24 else if(sum == maxhappy && res.size() < ans.size()) { 25 ans.clear(); 26 for(int i = 0; i < res.size(); i++) ans.push_back(res[i]); 27 } 28 cnt++; 29 return ; 30 } 31 for(int k = 0; k < E[u].size(); k++) { 32 int v = E[u][k]; 33 if(vis[v]) continue; 34 res.push_back(v); 35 vis[v] = 1; 36 dfs(v, en, co + d[u][v]); 37 vis[v] = 0; 38 res.pop_back(); 39 } 40 } 41 42 int main() { 43 cin >> n >> m >> s[1]; 44 mp[s[1]] = 1; 45 memset(d, 0x3f, sizeof(d)); 46 for(int i = 0; i <= n; i++) d[i][i] = 0; 47 for(int i = 2; i <= n; i++) { 48 cin >> s[i] >> happy[i]; 49 mp[s[i]] = i; 50 } 51 for(int i = 1; i <= m; i++) { 52 cin >> s1 >> s2 >> cost; 53 d[mp[s1]][mp[s2]] = cost; 54 d[mp[s2]][mp[s1]] = cost; 55 E[mp[s2]].push_back(mp[s1]); 56 E[mp[s1]].push_back(mp[s2]); 57 } 58 59 for(int k = 1; k <= n; k++) 60 for(int i = 1; i <= n; i++) 61 for(int j = 1; j <= n; j++) 62 d[i][j] = min(d[i][j] ,d[i][k] + d[k][j]); 63 64 cost = d[mp[s[1]]][mp["ROM"]]; 65 vis[mp[s[1]]] = 1; 66 dfs(mp[s[1]], mp["ROM"], 0); 67 68 cout << cnt << " " << cost << " " << maxhappy << " " << maxhappy / ans.size() << endl; 69 cout << s[1]; 70 for(int i = 0; i < ans.size(); i++) cout << "->" << s[ans[i]]; 71 return 0; 72 }
1088 Rational Arithmetic
注意相乘会爆long long,判断负可以分别判断x 和 y
1 // 1088 Rational Arithmetic 2 #include <bits/stdc++.h> 3 using namespace std; 4 5 typedef long long ll; 6 ll gcd(ll a, ll b) { 7 return b == 0 ? a : gcd(b, a % b); 8 } 9 10 void func(ll x, ll y) { 11 bool f = 0; 12 if(x * y == 0) { 13 if(y == 0) printf("Inf"); 14 else if(x == 0) printf("0"); 15 return ; 16 } 17 if((x > 0 && y < 0) || (x < 0 && y > 0)) f = 1; 18 x = abs(x); y = abs(y); 19 ll val = x / y; 20 ll res = x - val * y; 21 if(f) printf("(-"); 22 if(val != 0) printf("%lld", val); 23 if(res == 0) { 24 if(f) printf(")"); 25 return ; 26 } 27 if(val != 0) printf(" "); 28 ll g = gcd(res, y); 29 res /= g; y /= g; 30 printf("%lld/%lld", res, y); 31 if(f) printf(")"); 32 } 33 34 int main() { 35 ll a, b, c, d; 36 scanf("%lld/%lld %lld/%lld", &a, &b, &c, &d); 37 func(a, b);printf(" + ");func(c, d);printf(" = ");func(a * d + b * c, b * d);printf("\n"); 38 func(a, b);printf(" - ");func(c, d);printf(" = ");func(a * d - b * c, b * d);printf("\n"); 39 func(a, b);printf(" * ");func(c, d);printf(" = ");func(a * c, b * d);printf("\n"); 40 func(a, b);printf(" / ");func(c, d);printf(" = ");func(a * d, b * c); 41 return 0; 42 }
1089 Insert or Merge
先判断是否为插入排序,不是的话,跑一遍归并,直到a数组和b数组一样,再进行一次归并操作。
1 // 1089 Insert or Merge 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 105; 7 int a[N], b[N]; 8 9 int main() { 10 int n, i, j; 11 cin >> n; 12 for (i = 0; i < n; i++) cin >> a[i]; 13 for (i = 0; i < n; i++) cin >> b[i]; 14 for (i = 0; i < n - 1 && b[i] <= b[i + 1]; i++); 15 for (j = i + 1; a[j] == b[j] && j < n; j++); 16 if (j == n) { 17 cout << "Insertion Sort" << endl; 18 sort(a, a + i + 2); 19 } else { 20 cout << "Merge Sort" << endl; 21 int k = 1, f = 1; 22 while(f) { 23 f = 0; 24 for (i = 0; i < n; i++) { 25 if (a[i] != b[i]) f = 1; 26 } 27 k = k * 2; 28 for (i = 0; i < n / k; i++) 29 sort(a + i * k, a + (i + 1) * k); 30 sort(a + n / k * k, a + n); 31 } 32 } 33 for(j = 0; j < n; j++) { 34 if (j != 0) cout << " "; 35 cout << a[j]; 36 } 37 return 0; 38 }
1090 Highest Price in Supply Chain
DFS跑一遍树即可。
1 // 1090 Highest Price in Supply Chain 2 #include <cstdio> 3 #include <vector> 4 using namespace std; 5 6 const int N = 1e5 + 10; 7 int n, u, cnt, root; 8 double p, r, maxp = 0, sellp[N]; 9 vector <int> E[N]; 10 11 void dfs(int a, double price) { 12 sellp[a] = price; 13 maxp = max(maxp, price); 14 for(int i = 0; i < E[a].size(); i++) { 15 int b = E[a][i]; 16 dfs(b, price * (1.0 + r / 100)); 17 } 18 } 19 20 int main() { 21 scanf("%d %lf %lf", &n, &p, &r); 22 for(int i = 0; i < n; i++) { 23 scanf("%d", &u); 24 if(u == -1) root = i; 25 else E[u].push_back(i); 26 } 27 dfs(root, p); 28 for(int i = 0; i < n; i++) { 29 if(sellp[i] == maxp) cnt++; 30 } 31 printf("%.2f %d\n", maxp, cnt); 32 return 0; 33 }
1091 Acute Stroke
3维BFS裸题。
1 // 1091 Acute Stroke 2 #include <queue> 3 #include <cstdio> 4 using namespace std; 5 6 int n, m, l, t, ans = 0; 7 int mp[70][1300][130]; 8 bool vis[70][1300][130]; 9 struct node{ 10 int z, x, y; 11 }; 12 int dx[6] = {0, 0, 0, 0, -1, 1}; 13 int dy[6] = {0, 0, -1, 1, 0, 0}; 14 int dz[6] = {-1, 1, 0, 0, 0, 0}; 15 16 bool check(int z, int x, int y) { 17 if(z >= 1 && z <= l && x >= 1 && x <= n && y >= 1 && y <= m) return true; 18 return false; 19 } 20 21 int bfs(int z, int x, int y) { 22 queue <node> q; 23 node tmp; 24 int res = 0; 25 q.push(node{z, x, y}); 26 vis[z][x][y] = 1; 27 while(!q.empty()) { 28 res++; 29 tmp = q.front(); 30 q.pop(); 31 for(int i = 0; i < 6; i++) { 32 int nz = tmp.z + dz[i]; 33 int nx = tmp.x + dx[i]; 34 int ny = tmp.y + dy[i]; 35 if(check(nz, nx, ny) && !vis[nz][nx][ny] && mp[nz][nx][ny]) { 36 vis[nz][nx][ny] = 1; 37 q.push(node{nz, nx, ny}); 38 } 39 } 40 41 } 42 if(res >= t) return res; 43 else return 0; 44 } 45 46 int main() { 47 scanf("%d%d%d%d", &n, &m, &l, &t); 48 for(int z = 1; z <= l; z++) 49 for(int x = 1; x <= n; x++) 50 for(int y = 1; y <= m; y++) 51 scanf("%d", &mp[z][x][y]); 52 53 for(int z = 1; z <= l; z++) 54 for(int x = 1; x <= n; x++) 55 for(int y = 1; y <= m; y++) 56 if(mp[z][x][y] && !vis[z][x][y]) ans += bfs(z, x, y); 57 58 printf("%d\n", ans); 59 return 0; 60 }
1092 To Buy or Not to Buy
用数组标记一下即可。
1 // 1092 To Buy or Not to Buy 2 #include <iostream> 3 using namespace std; 4 5 int a[123]; 6 string s1, s2; 7 8 int main(){ 9 cin >> s1 >> s2; 10 int c1 = 0, c2 = 0; 11 for(int i = 0; i < s1.size(); i++) a[s1[i]]++; 12 for(int i = 0; i < s2.size(); i++) a[s2[i]]--; 13 for(int i = 48; i < 123; i++) { 14 if(a[i] < 0) c1+=a[i]; 15 else c2+=a[i]; 16 } 17 if(c1 < 0) cout << "No " << -c1 << endl; 18 else cout << "Yes " << c2 << endl; 19 return 0; 20 }
1093 Count PAT's
统计一下P,A,T。在A位置计算前面有几个P,后面有几个T。
1 // 1093 Count PAT's 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 typedef long long ll; 7 const int N = 1e5 + 10; 8 const ll mod = 1000000007; 9 char s[N]; 10 ll sum[N][5]; 11 12 int main() { 13 int n; 14 ll ans = 0; 15 scanf("%s", s + 1); 16 n = strlen(s + 1); 17 for(int i = 1; i <= n; i++) { 18 for(int j = 1; j <= 3; j++) sum[i][j] = sum[i - 1][j]; 19 if(s[i] == 'P') sum[i][1]++; 20 else if(s[i] == 'A') sum[i][2]++; 21 else sum[i][3]++; 22 } 23 for(int i = 1; i <= n; i++) { 24 if(s[i] == 'A') { 25 ans = (ans + (sum[i][1] * (sum[n][3] - sum[i][3])) % mod) % mod; 26 } 27 } 28 printf("%lld\n", ans); 29 return 0; 30 }
1094 The Largest Generation
DFS跑下树,记录下即可。
1 // 1094 The Largest Generation 2 #include <vector> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 105; 7 int n, m, cnt[N]; 8 vector <int> children[N]; 9 10 void dfs(int u, int level) { 11 for(int i = 0; i < children[u].size(); i++) { 12 dfs(children[u][i], level + 1); 13 } 14 cnt[level]++; 15 } 16 17 int main() { 18 scanf("%d%d", &n, &m); 19 while(m--) { 20 int r, k, c; 21 scanf("%d%d", &r, &k); 22 while(k--) { 23 scanf("%d", &c); 24 children[r].push_back(c); 25 } 26 } 27 dfs(1, 1); 28 int mx = 0, id; 29 for(int i = 1; i <= n; i++) { 30 if(cnt[i] > mx) { 31 mx = cnt[i]; 32 id = i; 33 } 34 } 35 printf("%d %d\n", mx, id); 36 return 0; 37 }
1095 Cars on Campus
按同个车牌号并以时间排序,模拟过去即可。
1 // 1095 Cars on Campus 2 #include <map> 3 #include <vector> 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <algorithm> 8 using namespace std; 9 10 const int N = 1e4 + 5; 11 char s[N][10], op[5]; 12 int t[N], f[N], id[N]; 13 int cnt[3600 * 60 + 5], maxtime; 14 map<string, int> m; 15 vector <string> ans; 16 17 bool cmp(int x, int y) { 18 if(!strcmp(s[x], s[y])) { 19 return t[x] < t[y]; 20 } else { 21 return strcmp(s[x], s[y]) < 0; 22 } 23 } 24 25 int main() { 26 int n, k, hh, mm, ss; 27 scanf("%d%d", &n, &k); 28 for(int i = 0; i < n; i++) { 29 id[i] = i; 30 scanf("%s %d:%d:%d %s", s[i], &hh, &mm, &ss, op); 31 t[i] = 3600 * hh + 60 * mm + ss; 32 if(op[0] == 'i') f[i] = 0; 33 else f[i] = 1; 34 } 35 sort(id, id + n, cmp); 36 for(int i = 1; i < n; i++) { 37 if(!strcmp(s[id[i]], s[id[i - 1]])) { 38 if(f[id[i - 1]] == 0 && f[id[i]] == 1) { 39 cnt[t[id[i - 1]]]++; 40 cnt[t[id[i]]]--; 41 m[s[id[i]]] = m[s[id[i]]] + t[id[i]] - t[id[i - 1]]; 42 maxtime = max(maxtime, m[s[id[i]]]); 43 } 44 } 45 } 46 for(int i = 0; i <= 3600 * 60; i++) cnt[i] += cnt[i - 1]; 47 while(k--) { 48 scanf("%d:%d:%d", &hh, &mm, &ss); 49 hh = 3600 * hh + 60 * mm + ss; 50 printf("%d\n", cnt[hh]); 51 } 52 for(int i = 0; i < n; i++) { 53 if(m[s[i]] == maxtime) { 54 ans.push_back(s[i]); 55 m[s[i]] = 0; 56 } 57 } 58 sort(ans.begin(), ans.end()); 59 for(int i = 0; i < ans.size(); i++) cout << ans[i] << " "; 60 hh = maxtime / 3600; maxtime -= 3600 * hh; 61 mm = maxtime / 60; maxtime -= 60 * mm; 62 ss = maxtime; 63 printf("%02d:%02d:%02d\n", hh, mm, ss); 64 return 0; 65 }
1096 Consecutive Factors
暴力找起点,然后找可以被整除的数。
1 // 1096 Consecutive Factors 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 typedef long long ll; 7 8 int main() { 9 ll n, f = 0, maxlen = 0; 10 cin >> n; 11 for(ll i = 2; i * i <= n; i++){ 12 ll m = 1, j = i; 13 for(; j * j <= n; j++) { 14 m *= j; 15 if(n % m != 0) break; 16 } 17 if(j - i > maxlen) { 18 maxlen = j - i; 19 f = i; 20 } 21 } 22 if(f == 0) { 23 cout << 1 << endl << n << endl; 24 return 0; 25 } 26 cout << maxlen << endl; 27 ll tmp = 1; 28 cout << f; 29 for(ll i = f + 1; tmp < n && (i - f) < maxlen; i++) { 30 tmp *= i; 31 cout << "*" << i; 32 } 33 return 0; 34 }
1097 Deduplication on a Linked List
从根跑下树,把仍然保留的移除的都存起来。最后输出即可。
1 #include <map> 2 #include <vector> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e5 + 10; 9 int nxt[N], val[N]; 10 map<int, int> m; 11 12 struct node{ 13 int u, w, v; 14 }; 15 16 vector <node> Remain, Remove; 17 18 int main() { 19 int root, cur, n; 20 scanf("%d %d", &root, &n); 21 for(int i = 1; i <= n; i++) { 22 int u, v, w; 23 scanf("%d %d %d", &u, &w, &v); 24 nxt[u] = v; 25 val[u] = w; 26 } 27 cur = root; 28 while(cur != -1) { 29 if(!m[abs(val[cur])]) { 30 Remain.push_back({cur, val[cur], nxt[cur]}); 31 m[abs(val[cur])] = 1; 32 } else { 33 Remove.push_back({cur, val[cur], nxt[cur]}); 34 } 35 cur = nxt[cur]; 36 } 37 for(int i = 0; i < Remain.size(); i++) { 38 if(i != Remain.size() - 1) printf("%05d %d %05d\n", Remain[i].u, Remain[i].w, Remain[i + 1].u); 39 else printf("%05d %d -1\n", Remain[i].u, Remain[i].w); 40 } 41 for(int i = 0; i < Remove.size(); i++) { 42 if(i != Remove.size() - 1) printf("%05d %d %05d\n", Remove[i].u, Remove[i].w, Remove[i + 1].u); 43 else printf("%05d %d -1\n", Remove[i].u, Remove[i].w); 44 } 45 return 0; 46 }
1098 Insertion or Heap Sort
判断下排序类型,插入排序很容易解决,再扔个堆排序上去即可。
1 // 1098 Insertion or Heap Sort 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 105; 9 int a[N], b[N]; 10 11 void solve(int l, int r) { 12 int i = 0, j = 1; 13 while(j <= r) { 14 if(j + 1 <= r && b[j] < b[j + 1]) j++; 15 if(b[j] > b[i]) { 16 swap(b[j], b[i]); 17 i = j; 18 j = 2 * (i + 1) -1; 19 } else { 20 break; 21 } 22 } 23 } 24 25 int main() { 26 int n, k, f = 1; 27 scanf("%d", &n); 28 for(int i = 0; i < n; i++) scanf("%d", &a[i]); 29 for(int i = 0; i < n; i++) scanf("%d", &b[i]); 30 for(k = 0; k < n - 1 && b[k] <= b[k + 1]; k++) ; 31 for(int i = k + 1; i < n; i++) { 32 if(a[i] != b[i]) {f = 0; break;} 33 } 34 if(f) { 35 printf("Insertion Sort\n"); 36 sort(b, b + k + 2); 37 for(int i = 0; i < n; i++){ 38 printf("%d", b[i]); 39 if(i != n - 1) printf(" "); 40 } 41 } else { 42 printf("Heap Sort\n"); 43 int p = n - 1; 44 while(p > 0 && b[p - 1] <= b[p]) { 45 p--; 46 } 47 swap(b[0], b[p]); 48 solve(0, p - 1); 49 for(int i = 0; i < n; i++){ 50 printf("%d", b[i]); 51 if(i != n - 1) printf(" "); 52 } 53 } 54 return 0; 55 }
1099 Build A Binary Search Tree
先排序,中序遍历即为填数的顺序。最后再层次遍历输出。
1 // 1099 Build A Binary Search Tree 2 #include <queue> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 queue <int> q; 9 const int N = 105; 10 int lch[N], rch[N], val[N], a[N], cnt = 0; 11 12 void dfs(int u) { 13 if(lch[u] != -1) dfs(lch[u]); 14 val[u] = a[cnt++]; 15 if(rch[u] != -1) dfs(rch[u]); 16 } 17 18 int main() { 19 int n, f = 0; 20 scanf("%d", &n); 21 for(int i = 0; i < n; i++) { 22 int l, r; 23 scanf("%d %d", &l, &r); 24 lch[i] = l; rch[i] = r; 25 } 26 for(int i = 0; i < n; i++) scanf("%d", &a[i]); 27 sort(a, a + n); 28 dfs(0); 29 q.push(0); 30 while(!q.empty()) { 31 int cur = q.front(); 32 q.pop(); 33 if(!f) f = 1; 34 else printf(" "); 35 printf("%d", val[cur]); 36 if(lch[cur] != -1) q.push(lch[cur]); 37 if(rch[cur] != -1) q.push(rch[cur]); 38 } 39 return 0; 40 }
1100 Mars Numbers
模拟。
1 // 1100 Mars Numbers 2 #include <iostream> 3 using namespace std; 4 5 string s; 6 string str[25] = { 7 "tret", "jan", "feb", "mar", "apr", "may", "jun", 8 "jly", "aug", "sep", "oct", "nov", "dec", "tam", "hel", 9 "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", 10 "mer", "jou" 11 }; 12 13 void cal1() { 14 if(s[0] == '0') { 15 cout << "tret" << endl; 16 return ; 17 } 18 int num = 0; 19 for(int i = 0; i < s.size(); i++) num = 10 * num + (s[i] - '0'); 20 if(num / 13) cout << str[num / 13 + 12]; 21 if((num / 13) && (num % 13)) cout << " "; 22 if(num % 13) cout << str[num % 13]; 23 cout << endl; 24 } 25 26 void cal2() { 27 int t1 = 0, t2 = 0; 28 string s1 = s.substr(0, 3), s2 = ""; 29 if(s.size() > 4) s2 = s.substr(4, 3); 30 for(int i = 1; i <= 24; i++) { 31 if(str[i] == s1) t1 = i; 32 if(str[i] == s2) t2 = i; 33 } 34 if(t1 > 12) cout << (t1 - 12) * 13 + t2 << endl; 35 else cout << t1 + t2 << endl; 36 } 37 38 int main() { 39 int n; 40 cin >> n; 41 getchar(); 42 for(int i = 1; i <= n; i++) { 43 getline(cin, s); 44 if(s[0] >= '0' && s[0] <= '9') cal1(); 45 else cal2(); 46 } 47 return 0; 48 }
1101 Quick Sort
用个数组记录下从前往后,对于当前位置的最大值;从后往前,对于当前位置的最小值。最后再遍历一遍数组即可。
1 // 1101 Quick Sort 2 #include <vector> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 vector <int> ans; 9 const int N = 1e5 + 10; 10 int a[N], pre[N], nxt[N]; 11 12 int main() { 13 int n; 14 scanf("%d", &n); 15 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 16 for(int i = 1; i <= n; i++) { 17 pre[i] = max(pre[i - 1], a[i]); 18 } 19 nxt[n + 1] = 1e9 + 10; 20 for(int i = n; i >= 1; i--) { 21 nxt[i] = min(nxt[i + 1], a[i]); 22 } 23 for(int i = 1; i <= n; i++) { 24 if(pre[i] <= a[i] && nxt[i] >= a[i]) { 25 ans.push_back(a[i]); 26 } 27 } 28 printf("%d\n", ans.size()); 29 sort(ans.begin(), ans.end()); 30 for(int i = 0; i < ans.size(); i++) { 31 if(i == 0) printf("%d", ans[i]); 32 else printf(" %d", ans[i]); 33 } 34 printf("\n"); 35 return 0; 36 }
1102 Invert a Binary Tree
先找下根,中序和层序,把先左后右的顺序调换下。
1 // 1102 Invert a Binary Tree 2 #include <queue> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N = 15; 10 int n, root, f; 11 int lch[N], rch[N], fa[N]; 12 13 void level_order() { 14 queue <int> q; 15 q.push(root); 16 while(!q.empty()) { 17 int u = q.front(); 18 if(!f) f = 1; 19 else printf(" "); 20 printf("%d", u); 21 q.pop(); 22 if(rch[u] != -1) q.push(rch[u]); 23 if(lch[u] != -1) q.push(lch[u]); 24 } 25 printf("\n"); 26 } 27 28 void in_order(int u) { 29 if(rch[u] != -1) in_order(rch[u]); 30 if(!f) f = 1; 31 else printf(" "); 32 printf("%d", u); 33 if(lch[u] != -1) in_order(lch[u]); 34 } 35 36 int main() { 37 cin >> n; 38 memset(fa, -1, sizeof(fa)); 39 memset(lch, -1, sizeof(lch)); 40 memset(rch, -1, sizeof(rch)); 41 for(int i = 0; i < n; i++) { 42 char c1, c2; 43 cin >> c1 >> c2; 44 if(c1 != '-') { 45 lch[i] = (c1 - '0'); 46 fa[(c1 - '0')] = i; 47 } 48 if(c2 != '-') { 49 rch[i] = (c2 - '0'); 50 fa[(c2 - '0')] = i; 51 } 52 } 53 for(int i = 0; i < n; i++){ 54 if(fa[i] == -1) root = i; 55 } 56 f = 0; 57 level_order(); 58 f = 0; 59 in_order(root); 60 printf("\n"); 61 return 0; 62 }
1103 Integer Factorization
预处理下,再DFS+剪枝。更新下答案。
1 // 1103 Integer Factorization 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 int a[405], b[405], ans[405]; 8 int n, m, k, c; 9 10 void dfs(int id, int cnt, int val) { 11 b[cnt] = id; 12 if(cnt > m || val > n) return ; 13 if(val == n && cnt == m) { 14 bool f = 0; 15 for(int i = 1; i <= m; i++) { 16 if(ans[i] != b[i]) { 17 if(b[i] > ans[i]) f = 1; 18 break; 19 } 20 } 21 for(int i = 1; i <= m; i++) ans[i] = b[i]; 22 return ; 23 } 24 for(int i = id; i <= c && val + a[i] <= n; i++) { 25 dfs(i, cnt + 1, val + a[i]); 26 } 27 } 28 29 int POW(int u, int v) { 30 int res = 1; 31 for(int i = 1; i <= v; i++) res *= u; 32 return res; 33 } 34 35 int main() { 36 scanf("%d%d%d", &n, &m, &k); 37 for(int i = 1; POW(i, k) <= n; i++) { 38 a[i] = POW(i, k); 39 c = i; 40 } 41 for(int i = 1; i <= c; i++) dfs(i, 1, a[i]); 42 int res = 0; 43 for(int i = 1; i <= m; i++) res += a[ans[i]]; 44 if(res == n) { 45 printf("%d = %d^%d", n, ans[m], k); 46 for(int i = m - 1; i >= 1; i--) { 47 printf(" + %d^%d", ans[i], k); 48 } 49 printf("\n"); 50 } else { 51 printf("Impossible\n"); 52 } 53 return 0; 54 }
1 #include <cstdio> 2 using namespace std; 3 4 const int N = 1e5 + 10; 5 double a[N]; 6 7 int main() { 8 int n; 9 double ans = 0; 10 scanf("%d", &n); 11 for(int i = 1; i <= n; i++) scanf("%lf", &a[i]); 12 for(int i = 1; i <= n; i++) { 13 ans += a[i] * i * (n - i + 1); 14 } 15 printf("%.2f\n", ans); 16 return 0; 17 }
1105 Spiral Matrix
蛇形填数,注意下细节。
1 // 1105 Spiral Matrix 2 #include <cmath> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e4 + 10; 9 int a[N]; 10 int ans[N][N]; 11 12 int main() { 13 int n, k; 14 scanf("%d", &n); 15 for(int i = 1; i <= n; i++) scanf("%d", &a[i]); 16 if(n == 1) { 17 printf("%d", a[1]); 18 return 0; 19 } 20 sort(a + 1, a + 1 + n); 21 int m = sqrt(1.0 * n); 22 while(n % m != 0) m++; 23 if((n / m) > m) m = n/ m; 24 //m n/m 25 k = n; n = n / m; 26 int i = 1, j = 1; 27 int U = 1, D = m, L = 1, R = n; 28 while(k > 0) { 29 while(k > 0 && j < R) { 30 ans[i][j] = a[k--]; 31 j++; 32 } 33 while(k > 0 && i < D) { 34 ans[i][j] = a[k--]; 35 i++; 36 } 37 while(k > 0 && j > U) { 38 ans[i][j] = a[k--]; 39 j--; 40 } 41 while(k > 0 && i > L) { 42 ans[i][j] = a[k--]; 43 i--; 44 } 45 U++; D--; L++; R--; 46 i++; j++; 47 if(k == 1) { 48 ans[i][j] = a[k--]; 49 } 50 } 51 for(int i = 1; i <= m; i++) { 52 for(int j = 1; j <= n; j++) { 53 printf("%d%c", ans[i][j], (j == n) ? '\n' : ' '); 54 } 55 } 56 return 0; 57 }
1106 Lowest Price in Supply Chain
跑下树。在叶子节点更新下最小值,最后再遍历下叶子节点即可。
1 // 1106 Lowest Price in Supply Chain 2 #include <cstdio> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 1e5 + 10; 9 int n, k, id; 10 double p, r, val[N], mi = 1e12; 11 vector <int> E[N]; 12 13 void dfs(int u) { 14 if(E[u].size() == 0) mi = min(mi, val[u]); 15 for(int j = 0; j < E[u].size(); j++) { 16 int v = E[u][j]; 17 val[v] = val[u] * (1.0 + r / 100.0); 18 dfs(v); 19 } 20 } 21 22 int main() { 23 scanf("%d %lf %lf", &n, &p, &r); 24 for(int i = 0; i < n; i++) { 25 scanf("%d", &k); 26 for(int j = 1; j <= k; j++) { 27 scanf("%d", &id); 28 E[i].push_back(id); 29 } 30 } 31 val[0] = p; 32 dfs(0); 33 int cnt = 0; 34 for(int i = 0; i < n; i++) if(val[i] == mi && E[i].size() == 0) cnt++; 35 printf("%.4f %d\n", mi, cnt); 36 return 0; 37 }
1107 Social Clusters
并查集搞搞。
1 // 1107 Social Clusters 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N = 1e3 + 10; 10 vector <int> E[N], ans; 11 int fa[N], C[N]; 12 13 int fi(int x) { 14 return x == fa[x] ? x : fa[x] = fi(fa[x]); 15 } 16 17 void Union(int x, int y) { 18 int fx = fi(x), fy = fi(y); 19 if(fx != fy) { 20 fa[fy] = fx; 21 } 22 } 23 24 int main() { 25 bool f = 0; 26 int n, k, u, cnt = 0; 27 scanf("%d", &n); 28 for(int i = 1; i <= n; i++) { 29 fa[i] = i; 30 scanf("%d:", &k); 31 while(k--) { 32 scanf("%d", &u); 33 E[u].push_back(i); 34 } 35 } 36 for(int i = 1; i < N; i++) { 37 for(int j = 1; j < E[i].size(); j++) { 38 u = E[i][j]; 39 Union(u, E[i][0]); 40 } 41 } 42 for(int i = 1; i <= n; i++) { 43 C[fi(i)]++; 44 if(i == fa[i]) cnt++; 45 } 46 printf("%d\n", cnt); 47 for(int i = 1; i <= n; i++) { 48 if(C[i] != 0) ans.push_back(C[i]); 49 } 50 sort(ans.begin(), ans.end()); 51 for(int i = ans.size() - 1; i >= 0; i--) { 52 if(i != ans.size() - 1) printf(" "); 53 printf("%d", ans[i]); 54 } 55 return 0; 56 }
1108 Finding Average
注意 一个合法数字 的输出格式。
1 // 1108 Finding Average 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 6 char str[50]; 7 8 double check() { 9 int i = 0, f = 1, cnt = 0; 10 if(str[0] == '-') i = 1, f = -1; 11 int m = strlen(str); 12 for(; i < m; i++) { 13 if(str[i] >= '0' && str[i] <= '9') continue; 14 if(str[i] == '.') { 15 cnt++; 16 if(cnt >= 2) return 2000; 17 } 18 else return 2000; 19 } 20 double res = 0; 21 i = 0; 22 if(str[0] == '-') i = 1, f = -1; 23 for(; i < m; i++) { 24 if(str[i] == '.') { 25 if(i + 3 < m) return 2000; 26 if(i + 2 < m) res += (str[i + 2] - '0') / 100.0; 27 if(i + 1 < m) res += (str[i + 1] - '0') / 10.0; 28 return f * res; 29 } 30 res = 10 * res + 1.0 * (str[i] - '0'); 31 } 32 return f * res; 33 } 34 35 int main() { 36 double average = 0; 37 int n, c = 0; 38 scanf("%d", &n); 39 while(n--) { 40 scanf("%s", str); 41 double val = check(); 42 if(val >= -1000 && val <= 1000) average += check(), c++; 43 else{ 44 printf("ERROR: %s is not a legal number\n", str); 45 } 46 } 47 if(c == 1) printf("The average of %d number is %.2f\n", c, average / c); 48 else if(c > 1) printf("The average of %d numbers is %.2f\n", c, average / c); 49 else printf("The average of 0 numbers is Undefined\n"); 50 return 0; 51 }
1109 Group Photo
模拟。
1 // 1109 Group Photo 2 #include <vector> 3 #include <cstdio> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 struct node { 9 int h; 10 string name; 11 } tmp; 12 13 bool cmp(node x, node y) { 14 if(x.h == y.h) return x.name < y.name; 15 return x.h > y.h; 16 } 17 18 vector<node> p; 19 20 int main() { 21 int n, k, per, last, id = 0; 22 cin >> n >> k; 23 per = n / k; 24 last = per + (n % k); 25 for (int i = 1; i <= n; i++) { 26 cin >> tmp.name >> tmp.h; 27 p.push_back(tmp); 28 } 29 sort(p.begin(), p.end(), cmp); 30 for (int i = 0; i < k; i++) { 31 int now = per, f = -1; 32 if(i == 0) now = last; 33 int pos = now / 2; 34 vector<string> ans(n); 35 while(pos >= 0 && pos < now) { 36 ans[pos] = p[id++].name; 37 pos += f; 38 if(f < 0) { 39 f *= -1; 40 f++; 41 } else { 42 f *= -1; 43 f--; 44 } 45 } 46 for (int j = 0; j < now; j++) { 47 cout << ans[j]; 48 if(j != now - 1) cout << " "; 49 else cout << endl; 50 } 51 ans.clear(); 52 } 53 return 0; 54 }
1110 Complete Binary Tree
完全二叉树递归出的最大下标值一定等于总节点数。
1 // 1110 Complete Binary Tree 2 #include <queue> 3 #include <iostream> 4 using namespace std; 5 6 int fa[25], lch[25], rch[25]; 7 int n, root, last, maxindex = 0; 8 9 void dfs(int u, int index) { 10 if(u == -1) return ; 11 if(index > maxindex) { 12 maxindex = index; 13 last = u; 14 } 15 dfs(lch[u], 2 * index); 16 dfs(rch[u], 2 * index + 1); 17 } 18 19 int main() { 20 cin >> n; 21 for(int i = 0; i < n; i++) fa[i] = lch[i] = rch[i] = -1; 22 for(int i = 0; i < n; i++) { 23 string s1, s2; 24 cin >> s1 >> s2; 25 if(s1 != "-") { 26 fa[atoi(s1.c_str())] = i; 27 lch[i] = atoi(s1.c_str()); 28 } 29 if(s2 != "-") { 30 fa[atoi(s2.c_str())] = i; 31 rch[i] = atoi(s2.c_str()); 32 } 33 } 34 for(int i = 0; i < n; i++) { 35 if(fa[i] == -1) root = i; 36 } 37 dfs(root, 1); 38 if(maxindex == n) { 39 cout << "YES " << last << endl; 40 } else { 41 cout << "NO " << root << endl; 42 } 43 return 0; 44 }
1112 Stucked Keyboard
模拟。
1 // 1112 Stucked Keyboard 2 #include <map> 3 #include <iostream> 4 using namespace std; 5 6 string str; 7 map<char, int> ok; 8 9 int main() { 10 int n, k, c = 1; 11 cin >> k >> str; 12 n = str.size(); 13 for (int i = 1; i < n; i++) { 14 if(str[i] == str[i - 1]) { 15 c++; 16 } else { 17 if(c % k != 0) ok[str[i - 1]] = 1; 18 c = 1; 19 } 20 } 21 if(c > 0 && c % k != 0) ok[str[n - 1]] = 1; 22 for (int i = 0; i < n; i++) { 23 if(ok[str[i]] == 0) { 24 cout << str[i]; 25 ok[str[i]] = -1; 26 } 27 } 28 cout << endl; 29 c = 1; 30 for (int i = 1; i < n; i++) { 31 if(str[i] == str[i - 1]) { 32 c++; 33 } else { 34 if(c % k == 0) { 35 if(ok[str[i - 1]] != 1) for (int j = 1; j <= c / k; j++) cout << str[i - 1]; 36 else for (int j = 1; j <= c; j++) cout << str[i - 1]; 37 } 38 else { 39 for (int j = 1; j <= c; j++) cout << str[i - 1]; 40 } 41 c = 1; 42 } 43 } 44 if(c > 0 && c % k == 0) { 45 if(ok[str[n - 1]] != 1) for (int j = 1; j <= c / k; j++) cout << str[n - 1]; 46 else for (int j = 1; j <= c; j++) cout << str[n - 1]; 47 } 48 else { 49 for (int j = 1; j <= c; j++) cout << str[n - 1]; 50 } 51 return 0; 52 }
1113 Integer Set Partition
先排序,再前面加一加,后面加一加。
1 // 1113 Integer Set Partition 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 typedef long long ll; 7 const int N = 1e5 + 10; 8 ll a[N]; 9 10 int main() { 11 int n, m; 12 ll s1 = 0, s2 = 0; 13 cin >> n; 14 for (int i = 1; i <= n; i++) cin >> a[i]; 15 sort(a + 1, a + 1 + n); 16 for (int i = n / 2 + 1; i <= n; i++) s1 += a[i]; 17 for (int i = 1; i < n / 2 + 1; i++) s2 += a[i]; 18 cout << (n % 2) << " "; 19 cout << abs(s1 - s2) << endl; 20 return 0; 21 }
1114 Family Property
暴力模拟即可。
1 // 1114 Family Property 2 #include <map> 3 #include <vector> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 const int N = 10000 + 10; 9 int fa[N], M[N], A[N]; 10 int fi(int x) { 11 return fa[x] == x ? x : fa[x] = fi(fa[x]); 12 } 13 14 void Union(int x, int y) { 15 if(x == -1 || y == -1) return ; 16 int fx = fi(x), fy = fi(y); 17 if(fx != fy) { 18 fa[fx] = fy; 19 } 20 } 21 22 void init() { 23 for(int i = 0; i < N; i++) fa[i] = i; 24 } 25 26 int cnt = 0; 27 vector<int> V, E[N]; 28 map<int, int> mp; 29 30 struct node { 31 int ansID, ansM; 32 double avg1, avg2; 33 }ans[N]; 34 35 map<int, int> vis; 36 37 bool cmp(node x, node y) { 38 if(x.avg2 == y.avg2) return x.ansID < y.ansID; 39 return x.avg2 > y.avg2; 40 } 41 42 int main() { 43 init(); 44 int n; 45 cin >> n; 46 for (int i = 1; i <= n; i++) { 47 int id, father, mother, child, k; 48 cin >> id >> father >> mother >> k; 49 Union(id, father); 50 if(father != -1) V.push_back(father); 51 Union(id, mother); 52 if(mother != -1) V.push_back(mother); 53 while (k--) { 54 cin >> child; 55 if(child != -1) V.push_back(child); 56 Union(id, child); 57 } 58 V.push_back(id); 59 cin >> M[id] >> A[id]; 60 } 61 for (int i = 0; i < V.size(); i++) { 62 int u = V[i]; 63 if(fa[u] == u && !mp[u]) { 64 cnt++; 65 mp[u] = cnt; 66 } 67 } 68 for (int i = 0; i < V.size(); i++) { 69 int u = V[i]; 70 if(!vis[u]) E[mp[fi(u)]].push_back(u), vis[u] = 1; 71 } 72 for (int i = 1; i <= cnt; i++) { 73 int minid = 123456; 74 double sum1 = 0; 75 double sum2 = 0; 76 for(int j = 0; j < E[i].size(); j++) { 77 int v = E[i][j]; 78 minid = min(minid, v); 79 sum1 += M[v]; 80 sum2 += A[v]; 81 } 82 ans[i].ansID = minid; 83 ans[i].ansM = E[i].size(); 84 ans[i].avg1 = sum1 / E[i].size(); 85 ans[i].avg2 = sum2 / E[i].size(); 86 } 87 sort(ans + 1, ans + 1 + n, cmp); 88 cout << cnt << endl; 89 for (int i = 1; i <= cnt; i++) { 90 printf("%04d %d %.3f %.3f\n", ans[i].ansID, ans[i].ansM, ans[i].avg1, ans[i].avg2); 91 } 92 return 0; 93 }
1115 Counting Nodes in a BST
用链表实现二叉搜索树的插入。构建完树后,DFS一遍,在对应深度更新节点数。
1 // 1115 Counting Nodes in a BST 2 #include <iostream> 3 using namespace std; 4 5 struct node { 6 int val; 7 struct node *left, *right; 8 }; 9 10 const int N = 1000 + 10; 11 int num[N], maxd = -1; 12 13 node *build(node *root, int v) { 14 if(root == NULL) { 15 root = new node(); 16 root -> val = v; 17 root -> left = root -> right = NULL; 18 } else if(v > root->val) { 19 root->right = build(root->right, v); 20 } else { 21 root->left = build(root->left, v); 22 } 23 return root; 24 } 25 26 void dfs(node *root, int dp) { 27 if(root == NULL) { 28 maxd = max(maxd, dp); 29 return ; 30 } 31 num[dp]++; 32 dfs(root->left, dp + 1); 33 dfs(root->right, dp + 1); 34 } 35 36 int main() { 37 int n, k; 38 node *root = NULL; 39 cin >> n; 40 while(n--) { 41 cin >> k; 42 root = build(root, k); 43 } 44 dfs(root, 0); 45 cout << num[maxd - 1] << " + " << num[maxd - 2] << " = " << num[maxd - 1] + num[maxd - 2] << endl; 46 return 0; 47 }
1116 Come on! Let's C
模拟。
1 // 1116 Come on! Let's C 2 #include <map> 3 #include <iostream> 4 using namespace std; 5 6 map<int, int> Rank; 7 8 bool check(int x) { 9 if(x < 2) return false; 10 for(int i = 2; i * i <= x; i++) { 11 if(x % i == 0) return false; 12 } 13 return true; 14 } 15 16 int main() { 17 int n, k; 18 cin >> n; 19 for (int i = 1; i <= n; i++) { 20 cin >> k; 21 Rank[k] = i; 22 } 23 cin >> n; 24 while (n--) { 25 cin >> k; 26 printf("%04d: ", k); 27 if(Rank[k] == -1) cout << "Checked" << endl; 28 else if(Rank[k] == 0) cout << "Are you kidding?" << endl; 29 else if(Rank[k] == 1) cout << "Mystery Award" << endl, Rank[k] = -1; 30 else if(check(Rank[k])) cout << "Minion" << endl, Rank[k] = -1; 31 else cout << "Chocolate" << endl, Rank[k] = -1; 32 } 33 return 0; 34 }
1117 Eddington Number
前缀和
1 // 1117 Eddington Number 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 6 const int N = 1e6 + 10; 7 int d[N]; 8 9 int main() { 10 int n, k, ans = 0; 11 cin >> n; 12 for(int i = 1; i <= n; i++) { 13 cin >> k; 14 d[k]++; 15 } 16 for(int i = 1; i < N; i++) d[i] += d[i - 1]; 17 for(int i = 1; i < N; i++) { 18 if((n - d[i]) >= i) ans = max(ans, i); 19 } 20 cout << ans << endl; 21 return 0; 22 }
1118 Birds in Forest
并查集
1 // 1118 Birds in Forest 2 #include <cstdio> 3 #include <vector> 4 using namespace std; 5 6 const int N = 1e4 + 10; 7 bool vis[N]; 8 int fa[N], cnt = 0; 9 vector <int> birds; 10 11 int fi(int x) { 12 return x == fa[x] ? x : fa[x] = fi(fa[x]); 13 } 14 15 void Union(int x, int y) { 16 int fx = fi(x), fy = fi(y); 17 if(fx != fy) { 18 fa[fx] = fy; 19 } 20 } 21 22 void init() { 23 for (int i = 0; i < N; i++) fa[i] = i; 24 } 25 26 int main() { 27 init(); 28 int m, n, k, u, v; 29 scanf("%d", &n); 30 while (n--) { 31 scanf("%d", &k); 32 for (int i = 1; i <= k; i++) { 33 scanf("%d", &v); 34 if(!vis[v]) vis[v] = 1, birds.push_back(v); 35 if(i == 1) u = v; 36 else Union(u, v); 37 } 38 } 39 for (int i = 0; i < birds.size(); i++) { 40 u = birds[i]; 41 if(fa[u] == u) cnt++; 42 } 43 printf("%d %d\n", cnt, birds.size()); 44 scanf("%d", &m); 45 while (m--) { 46 scanf("%d %d", &u, &v); 47 if(fi(u) == fi(v)) printf("Yes\n"); 48 else printf("No\n"); 49 } 50 return 0; 51 }
1119 Pre- and Post-order Traversals
通过前序和后序先建棵树。(题目中说明可以随意建棵符合的树)再中序遍历即可。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn=35; 5 int preOrder[maxn], postOrder[maxn]; 6 bool isUnique=true; 7 8 struct Node{ 9 int left=-1,right=-1; 10 }node[maxn]; 11 12 void build(int preL,int preR,int postL,int postR){ 13 if(preL>=preR){ 14 return; 15 } 16 int fa=preL; 17 int val=preOrder[preL+1]; 18 int postIdx; 19 for(int i=postL;i<postR;i++){ 20 if(val==postOrder[i]){ 21 postIdx=i; 22 break; 23 } 24 } 25 int num=postIdx-postL; 26 if(preR-preL-1==num){ 27 isUnique=false; 28 } 29 node[fa].left=preL+1; 30 build(preL+1,preL+num+1,postL,postIdx); 31 if(preR-preL-1>num){ 32 node[fa].right=preL+num+2; 33 build(preL+num+2,preR,postIdx+1,postR-1); 34 } 35 } 36 37 bool first=true; 38 39 void inOrder(int root){ 40 if(root==-1){ 41 return; 42 } 43 inOrder(node[root].left); 44 if(first){ 45 first=false; 46 printf("%d",preOrder[root]); 47 } 48 else 49 printf(" %d",preOrder[root]); 50 inOrder(node[root].right); 51 } 52 53 int main(){ 54 int n; 55 scanf("%d",&n); 56 for(int i=1;i<=n;i++) scanf("%d",&preOrder[i]); 57 for(int i=1;i<=n;i++) scanf("%d",&postOrder[i]); 58 build(1,n,1,n); 59 if(isUnique) printf("Yes\n"); 60 else printf("No\n"); 61 inOrder(1); 62 printf("\n"); 63 return 0; 64 }
1120 Friend Numbers
模拟
1 // 1120 Friend Numbers 2 #include <set> 3 #include <cstdio> 4 #include <iostream> 5 using namespace std; 6 7 const int N = 1e4 + 10; 8 int a[N], b[N]; 9 10 int cal(int x) { 11 int sum1 = 0; 12 while (x) { 13 sum1 += x % 10; 14 x /= 10; 15 } 16 return sum1; 17 } 18 19 int check(int x, int y) { 20 if (x == y) return x; 21 return 0; 22 } 23 24 set<int> ans; 25 26 int main() { 27 int n; 28 bool f = 0; 29 scanf("%d", &n); 30 for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = cal(a[i]); 31 for (int i = 1; i <= n; i++) { 32 for (int j = i; j <= n; j++) { 33 int val = check(b[i], b[j]); 34 if (val) ans.insert(val); 35 } 36 } 37 printf("%d\n", ans.size()); 38 for (auto x : ans) { 39 if (!f) f = 1; 40 else printf(" "); 41 printf("%d", x); 42 } 43 return 0; 44 }
1121 Damn Single
模拟
1 // 1121 Damn Single 2 #include <map> 3 #include <vector> 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 map<int, int> m, vis; 10 vector<int> p; 11 const int N = 1e4 + 10; 12 int a[N]; 13 14 int main() { 15 int n, x, y; 16 scanf("%d", &n); 17 while (n--) { 18 scanf("%d %d", &x, &y); 19 x++; y++; 20 m[x] = y; 21 m[y] = x; 22 } 23 scanf("%d", &n); 24 for (int i = 1; i <= n; i++) { 25 scanf("%d", &a[i]); 26 a[i]++; 27 vis[a[i]] = 1; 28 } 29 for (int i = 1; i <= n; i++) { 30 if (vis[ m[a[i]] ]) continue; 31 p.push_back(a[i]); 32 } 33 sort(p.begin(), p.end()); 34 printf("%d\n", p.size()); 35 for (int i = 0; i < p.size(); i++) { 36 if (i != 0) printf(" "); 37 printf("%05d", p[i] - 1); 38 } 39 return 0; 40 }
1122 Hamiltonian Cycle
模拟
1 // 1122 Hamiltonian Cycle 2 #include <set> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 using namespace std; 8 9 const int N = 205; 10 int MAP[N][N]; 11 set<int> s; 12 13 int main() { 14 memset(MAP, -1, sizeof(MAP)); 15 int n, m, k; 16 scanf("%d %d", &n, &m); 17 for (int i = 1; i <= m; i++) { 18 int u, v; 19 scanf("%d %d", &u, &v); 20 MAP[u][v] = 1; MAP[v][u] = 1; 21 } 22 scanf("%d", &k); 23 while (k--) { 24 bool f = 1; 25 int u, v, root; 26 scanf("%d", &m); 27 scanf("%d", &root); 28 s.insert(root); 29 u = root; 30 for (int i = 1; i < m; i++) { 31 scanf("%d", &v); 32 s.insert(v); 33 if (MAP[u][v] == -1) f = 0; 34 u = v; 35 } 36 if (root != u || s.size() != n || m != n + 1) f = 0; 37 if (!f) printf("NO\n"); 38 else printf("YES\n"); 39 s.clear(); 40 } 41 return 0; 42 }