2020.10.03天梯赛练习1题解-qfnu

    科技2022-07-13  123

    7-1谁先倒

    题意:本人语文功底太差,有些许不是很明白,但是这个是这个水题,也没必要深究
    #include <iostream> using namespace std; int A,B; int n; int a,b,c,d; int j,y; int main() { cin >> A >> B; cin >> n; int ying = 0; while(n --) { cin >> a >> b >> c >> d; int e = a + c; if(ying != 0){ continue; } if(e == b && e == d){ j++; y++; } else if(e == b){ j++; } else if(e == d){ y++; } if(j >= A){ ying = 2; } if(j >= B){ ying = 1; } } if(ying == 1){ cout << 'A' << endl; cout << y << endl; } else{ cout << 'B' << endl; cout << j << endl; } return 0; }
    结果只是得了14分,主要是题意还是没读明白

    7-2 重要的话说三遍

    题意:把一串字符串输出三遍就好了
    #include <iostream> using namespace std; int A,B; int n; int a,b,c,d; int j,y; int main() { printf("I'm gonna WIN!\n"); printf("I'm gonna WIN!\n"); printf("I'm gonna WIN!\n"); return 0; }

    7-3 奇偶分家

    题意:给定N个正整数,请统计奇数和偶数各有多少个?
    #include <iostream> using namespace std; int main() { int n; int odd = 0; cin >> n; for(int i = 0;i < n;i ++) { int x; cin >> x; if(x & 1){ odd++; } } cout << odd << ' ' << n - odd << endl; return 0; }

    7- 4 输出GPLT

    题意:给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT…这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。
    /* 统计出gplt每个字符的个数,然后按照顺序有啥输出啥就OK了 */ #include <iostream> using namespace std; int a,b,c,d; int main() { string str; cin >> str; for(int i = 0;str[i];i ++) { if(str[i] == 'G' || str[i] == 'g'){ a++; } else if(str[i] == 'P' || str[i] == 'p'){ b++; } else if(str[i] == 'L' || str[i] == 'l'){ c++; } else if(str[i] == 'T' || str[i] == 't'){ d++; } } while(1) { if(a){ a--; cout << 'G'; } if(b){ b--; cout << 'P'; } if(c){ c--; cout << 'L'; } if(d){ d--; cout << 'T'; } if(a == 0 && b == 0 && c == 0 && d == 0){ break; } } return 0; }

    7- 5 点赞

    题意:点赞
    #include <iostream> #include <unordered_map> using namespace std; int a[10000]; int main() { int n; cin >> n; int m = 0,id; while(n --) { int x,y; cin >> y; while(y --) { cin >> x; a[x]++; if(a[x] > m){ m = a[x]; id = x; } if(a[x] == m){ if(x > id){ id = x; } } } } cout << id << ' ' << m << endl; return 0; }

    7-6 情人节 (15分)

    题意: 情人节
    #include <iostream> #include <vector> using namespace std; vector<string>v; int main() { string str; while(cin >> str && str != ".") { v.push_back(str); } if(v.size() >= 14){ cout << v[1] << " and " << v[13] << " are inviting you to dinner..." << endl; } else if(v.size() < 2){ cout << "Momo... No one is for you ..." << endl; } else{ cout << v[1] << " is the only one for you..." << endl; } return 0; }

    7-7 最佳情侣身高差

    题意:7-7 最佳情侣身高差
    #include <iostream> #include <vector> using namespace std; int main() { int n; char sex; double he; cin >> n; while(n --) { cin >> sex >> he; if(sex == 'M'){ he = he / 1.09; } else{ he = he * 1.09; } printf("%.2lf\n",he); } return 0; }

    7-8 宇宙无敌大招呼 (5分)

    题意:7-8 宇宙无敌大招呼
    #include <iostream> #include <vector> using namespace std; int main() { string name; cin >> name; cout << "Hello " << name << endl; return 0; }

    7-9 排座位 (25分)

    题意:如果两位宾客之间是朋友,且没有敌对关系,则输出No problem;如果他们之间并不是朋友,但也不敌对,则输出OK;如果他们之间有敌对,然而也有共同的朋友,则输出OK but…;如果他们之间只有敌对关系,则输出No way。
    /* 用一个二维数组来存储敌对关系,用并查集来存储朋友之间的关系,如果两个人有共同好友,那么两个人肯定在一个集合中 */ #include <iostream> #include <vector> using namespace std; const int N = 110; int p[N]; int g[N][N]; int find(int x) { if(p[x] != x){ p[x] = find(p[x]); } return p[x]; } int n,m,k; int main() { cin >> n >> m >> k; for(int i = 1;i < N;i ++) { p[i] = i; } for(int i = 0;i < m;i ++) { int a,b,c; cin >> a >> b >> c; int fa = find(a); int fb = find(b); g[a][b] = g[b][a] = c; if(c == 1){ p[fa] = fb; } } for(int i = 0;i < k;i ++) { int a,b; cin >> a >> b; int fa = find(a); int fb = find(b); if(fa == fb && g[a][b] != -1) { cout << "No problem" << endl; } else if(fa != fb && g[a][b] != -1){ cout << "OK" << endl; } else if(fa == fb && g[a][b] == -1){ cout << "OK but..." << endl; } else if(fa != fb && g[a][b] == -1){ cout << "No way" << endl; } } return 0; }

    7-10 最长对称子串 (25分)

    题意:对给定的字符串,本题要求你输出最长对称子串的长度。例如,给定Is PAT&TAP symmetric?,最长对称子串为s PAT&TAP s,于是你应该输出11。
    #include <iostream> #include <cstring> #include <algorithm> using namespace std; string str; bool check(int l,int r) { while(l < r) { if(str[l] != str[r]){ return false; } l ++; r --; } return true; } int main() { int maxn = 1; getline(cin,str); int len = str.size(); if(str.size() == 0){ cout << 0 << endl; return 0; } for(int i = 0;i < len;i ++) { for(int j = len - 1;j > i;j --) { if(j - i + 1 < maxn){ break; } if(str[j] == str[i]){ if(check(i,j)){ maxn = j - i + 1; } } } } cout << maxn << endl; return 0; }

    7-12 分而治之 (25分)

    题意:简单
    /* 简单dfs */ #include <iostream> #include <cstring> #include <algorithm> #include <unordered_set> #include <vector> using namespace std; const int N = 1e4 + 10; int n,m,k; vector<int>v[N]; unordered_set<int>st; bool dfs(int u) { for(auto &x : v[u]) { if(!st.count(x)){ return true; } } return false; } bool check() { for(int i = 1;i <= n;i ++) { if(st.count(i)){ continue; } if(dfs(i)){ return false; } } return true; } int main() { cin >> n >> m; for(int i = 0;i < m;i ++) { int a,b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } cin >> k; while(k --) { st.clear(); int t; cin >> t; while(t --) { int x; cin >> x; st.insert(x); } if(check()){ cout << "YES" << endl; } else{ cout << "NO" << endl; } } return 0; }

    7-13 垃圾箱分布

    题意:lj输入,恶心心,就是dijkstra,当然我喜欢spfa来求最短路
    #include <iostream> #include <cstring> #include <algorithm> #include <sstream> #include <vector> #include <queue> using namespace std; const int N = 1020; typedef pair<int,int> PII; vector<PII>v[N]; int n,m,k,d; int dist[N]; bool st[N]; void spfa(int u) { memset(dist,0x3f,sizeof dist); dist[u] = 0; queue<int>q; q.push(u); while(q.size()) { auto t = q.front();q.pop(); st[t] = false; for(auto &x : v[t]) { if(dist[x.first] > dist[t] + x.second){ dist[x.first] = dist[t] + x.second; if(!st[x.first]){ st[x.first] = true; q.push(x.first); } } } } } int main() { cin >> n >> m >> k >> d; for(int i = 0;i < k;i ++) { string str1,str2;int c; cin >> str1 >> str2 >> c; if(str1[0] == 'G' && str2[0] == 'G'){ int s1; if(str1.size() == 3){ s1 = n + 10; } else{ s1 = n + str1[1] - '0'; } int s2; if(str2.size() == 3){ s2 = n + 10; } else{ s2 = n + str2[1] - '0'; } v[s1].push_back({s2,c}); v[s2].push_back({s1,c}); } else if(str1[0] == 'G'){ stringstream ss(str2); int s2; ss >> s2; int s1; if(str1.size() == 3){ s1 = n + 10; } else{ s1 = n + str1[1] - '0'; } v[s1].push_back({s2,c}); v[s2].push_back({s1,c}); } else if(str2[0] == 'G'){ stringstream ss(str1); int s1; ss >> s1; int s2; if(str2.size() == 3){ s2 = n + 10; } else{ s2 = n + str2[1] - '0'; } v[s1].push_back({s2,c}); v[s2].push_back({s1,c}); } else{ stringstream ss1(str1); stringstream ss2(str2); int s1,s2; ss1 >> s1; ss2 >> s2; v[s1].push_back({s2,c}); v[s2].push_back({s1,c}); } } int ans = 0; double ave = 0; int id = 0; for(int i = 1;i <= m;i ++) { spfa(n + i); int maxn = 0; int minn = 1e9; int sum = 0; for(int j = 1;j <= n;j ++) { maxn = max(maxn,dist[j]); minn = min(minn,dist[j]); sum += dist[j]; // cout << dist[j] << ' '; } // cout << sum << endl; if(maxn > d){ continue; } else{ if(minn > ans){ ans = minn; ave = sum; id = i; } else if(minn == ans){ if(ave > sum){ ans = minn; ave = sum; id = i; } } } } if(id == 0){ cout << "No Solution" << endl; } else{ cout << 'G' << id << endl; double avg = ave * 1.0 / (n); printf("%.1lf %.1lf\n",ans * 1.0,avg); } return 0; }

    7-14 社交集群

    题意:晦涩难懂,就是你想的那种暴力,纯暴力,纯暴力,然后就过了,正解我觉得应该是并查集
    #include <iostream> #include <cstring> #include <algorithm> #include <set> #include <vector> using namespace std; const int N = 1100; int n,x,y; vector<int>v[N]; set<int>stt; vector<int>res; bool st1[N]; int main() { cin >> n; for(int i = 1;i <= n;i ++) { scanf("%d:",&x); for(int j = 0;j < x;j ++) { cin >> y; v[i].push_back(y); } stt.insert(i); } while(1) { set<int>st; int cnt = 1; for(int i = 1;i <= n;i ++) { if(st1[i] == false){ for(auto &x : v[i]) { st.insert(x); } stt.erase(i); st1[i] = true; break; } } if(st.size() == 0){ break; } for(int i = 1;i <= n;i ++) { if(st1[i]){ continue; } for(auto &x : v[i]){ if(st.count(x)){ cnt++; for(auto &x : v[i]){ st.insert(x); } stt.erase(i); st1[i] = true; for(auto &x : stt) { i = x - 1; break; } break; } } } res.push_back(cnt); } cout << res.size() << endl; sort(res.begin(),res.end()); reverse(res.begin(),res.end()); bool tf = false; for(auto &x : res) { if(tf){ cout << ' '; } tf = true; cout << x; } return 0; }
    Processed: 0.010, SQL: 8