2020.10.10天梯赛补题
7-9 名人堂与代金券 (25分)
题目链接:https://pintia.cn/problem-sets/1314102638464851968/problems/1314104356594376704
思路:排序输出就好。。。
#include <bits/stdc++.h> using namespace std; const int N = 10010; struct node{ string id; int s; }; node a[N]; bool cmp(node a, node b){ if(a.s == b.s) return a.id < b.id; return a.s > b.s; } int main() { int n, g, k; scanf("%d %d %d", &n,&g, &k); int res = 0; for(int i = 1;i <= n; ++ i){ cin >> a[i].id >> a[i].s; if(a[i].s >= g){ res += 50; } else if(a[i].s < 60); else res += 20; } sort(a + 1, a + 1 + n, cmp); cout << res << endl; int i = 1; while(i <= k){ int flag = i; for(int j = flag;j <= n; ++ j){ if(a[j].s != a[i].s) break; cout << i <<" "<< a[j].id << " " << a[j].s << endl; flag ++; } i = flag; } }
7-10 链表去重 (25分)
https://pintia.cn/problem-sets/1314102638464851968/problems/1314104356594376705
思路:把链表用数组存起来,按题意输出。比赛的时候忘记补零。。。
#include <bits/stdc++.h> using namespace std; int n; const int N = 100010; struct node{ int pos, data, next; }; node a[N]; bool c[N]; void print(vector<node>v1) { if(v1.size() == 1){ printf("%05d %d %d\n",v1[0].pos,v1[0].data,-1); return; } printf("%05d %d %05d\n",v1[0].pos,v1[0].data,v1[1].pos); for(int i = 1;i < v1.size();i ++) { if(i != v1.size() - 1){ printf("%05d %d %05d\n",v1[i].pos,v1[i].data,v1[i + 1].pos); } else{ printf("%05d %d %d\n",v1[i].pos,v1[i].data,-1); } } } vector<node> ans1, ans2; int main() { // ios::sync_with_stdio(false); int head; scanf("%d %d", &head, &n); for(int i = 0;i < n; ++ i){ int x, y, z; scanf("%d %d %d",&x, &y, &z); a[x] = {x, y, z}; } int h = head; for(int i = head; i != -1; i = a[i].next){ int val = abs(a[i].data); if(c[val]){ ans2.push_back(a[i]); } else{ ans1.push_back(a[i]); c[val] = true; } } // if(ans1.size()) // print(ans1); // if(ans2.size()) // print(ans2); for(int i = 0;i < ans1.size(); ++ i){ printf("%05d %d ", ans1[i].pos, ans1[i].data); if(i == ans1.size() - 1){ cout << "-1\n"; } else printf("%05d\n",ans1[i+1].pos); } for(int i = 0;i < ans2.size(); ++ i){ printf("%05d %d ", ans2[i].pos, ans2[i].data); if(i == ans2.size() - 1){ cout << "-1\n"; } else printf("%05d\n", ans2[i+1].pos); } }
7-11 部落 (25分)
https://pintia.cn/problem-sets/1314102638464851968/problems/1314104458302054400
思路:并查集,可以高效的查看这俩是不是一个圈子的...
#include <bits/stdc++.h> using namespace std; const int N = 10000; int n, m, cnt,c[N] , pre[N], k[N][N], num; bool a[N], b[N]; set<int> st; int find(int x){ int r = x; while(pre[r] != r){ r = pre[r]; } return r; } void Union(int a, int b){ int x = find(a), y = find(b); pre[x] = y; } int main() { for(int i = 1;i <= 10000; ++ i) pre[i] = i; cin >> n; for(int i = 1;i <= n; ++ i){ int m; scanf("%d", &m); c[i] = m; for(int j = 1;j <= m; ++ j){ int x; scanf("%d",&x); k[i][j] = x; st.insert(x); } for(int j = 1;j <= m - 1; ++ j) Union(k[i][j], k[i][j+1]); } for(int i = 1;i <= 10000; ++ i) pre[i] = find(i); set<int>::iterator it = st.begin(); for(;it != st.end(); ++ it){ if(!a[pre[*it]]) cnt ++, a[pre[*it]] = true; } cout <<st.size()<<" "<< cnt << endl; int q; cin >> q; while(q--){ int x, y; cin >> x >> y; if(pre[x] == pre[y]) puts("Y"); else puts("N"); } }
7-12 月饼 (25分)
https://pintia.cn/problem-sets/1314102638464851968/problems/1314104458302054401
思路:贪心,优先选择单位价格大的月饼
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1010; struct node{ double ton, price; }a[N]; bool cmp(node a, node b){ return ((double)a.price / (double)a.ton) > ((double)b.price / (double)b.ton); } int main() { int n, v; cin >> n >> v; for(int i = 1;i <= n; ++ i){ double x; cin >> x; a[i].ton = x; // a[i].price = y; } for(int i = 1;i <= n; ++ i){ double x; cin >> x; a[i].price = x; } sort(a + 1, a + 1 + n, cmp); double ans = 0; int tot = 1; while(v && tot <= n){ if(a[tot].ton <= v){ ans += (double)a[tot].price; v -= a[tot].ton; } else{ ans += (double) v * (double)a[tot].price / (double)a[tot].ton; v = 0; } tot ++; } printf("%.2lf\n", ans); }