A.Berstagram CodeForces 1250A
开两个数组,一个用来储存当前数列,另一个id[i]用来储存id为i的博客的当前位置,模拟即可(用链表写的我就是个five
1 #include<bits/stdc++.h> 2 using namespace std; 3 int mx[1000005],mn[1000005],a[1000005],id[1000005]; 4 int main(){ 5 int n,m;scanf("%d%d",&n,&m); 6 for (int i=1;i<=n;i++) mx[i]=mn[i]=a[i]=id[i]=i; 7 for (int i=1;i<=m;i++){ 8 int x;scanf("%d",&x); 9 int p=id[x];int y=a[p-1]; 10 if (p==1) continue; 11 12 swap(a[p-1],a[p]); 13 id[y]++;id[x]--; 14 mx[y]=max(mx[y],id[y]); 15 mn[x]=min(mn[x],id[x]); 16 } 17 for (int i=1;i<=n;i++) printf("%d %d ",mn[i],mx[i]); 18 }
B.The Feast and the Bus CodeForces 1250B
先给输入的组按人数排序
对于巴士的大小s,每次循环优先让人数最大的组上车,然后判断能不能让人数最小的上车,能上就上,之后除去上了车的组继续循环。
而巴士的大小存在一个范围,即>=最大组的人数,<=最大组的人数+次大组的人数,其次巴士的大小必然为两组的人数的和,不然会产生浪费,所以我们先用set进行预处理
1 #include <iostream> 2 #include<queue> 3 #include<map> 4 #include<utility> 5 #include<vector> 6 #include<algorithm> 7 #include<cstring> 8 #include<string> 9 #include<stdio.h> 10 #include<sstream> 11 #include<fstream> 12 #include<cmath> 13 #include<set> 14 #include<math.h> 15 using namespace std; 16 typedef long long ll; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 17 typedef pair<int, int> P; 18 int m, n, t,f,w,x; 19 int dat[10000+105] = {}; 20 set<int> s; 21 void solve() { 22 for (int i = 0; i < m; i++) { 23 int temp; 24 cin >> temp; 25 dat[temp]++; 26 } 27 sort(dat + 1, dat + n + 1); 28 ll ans = 9223372036854775807; 29 if (n == 1) { 30 ans = dat[1]; 31 cout << ans; 32 return; 33 } 34 s.insert(dat[n]); 35 for(int i=1;i<n;i++) 36 for (int j = 1; j <=n; j++) { 37 if ((dat[i] + dat[j] > dat[n])&&(i!=j)) 38 s.insert(dat[i] + dat[j]); 39 } 40 for (set<int>::iterator itr = s.begin(); itr != s.end();++itr) { 41 int f = 1, end = n; 42 int s = *itr; 43 int r = 0; 44 while (end>=f) 45 { 46 if (end == f) { r++; break; } 47 if (dat[end] + dat[f] <= s) { 48 end--; 49 f++; 50 } 51 else 52 end--; 53 r++; 54 } 55 if (((ll)r) * s < ans) 56 ans = ((ll)r) * s; 57 } 58 cout << ans; 59 } 60 int main() { 61 cin >> m >> n; 62 solve(); 63 return 0; 64 }
C. Trip to Saint Petersburg CodeForces 1250C
线段树 目前不会
E.The Coronation CodeForces 1250E
看了题解才知道是并查集,wtcl。。。
题意:给定几个01字符串,若相同位置有k个字符相同,则认为他们相似,问最少反转多少个字符串,让他们两两相似
用并查集维护他们之间的关系:i表示正常,i+n反转
1.反转一个后相似
2.反不反都相似
3.不反转才相似
4.无论如何都不相似
情况4显然无解,直接输出-1
1:连i,j+n 和j,i+n
2:没有约束关系
3:i j和i+n j+n
最后跑一遍,如果i和i+n在同一连通块内,也是无解的
我并查集加权的代码写错了 卡了我好久好久好久
1 #include <iostream> 2 #include<queue> 3 #include<map> 4 #include<utility> 5 #include<vector> 6 #include<algorithm> 7 #include<cstring> 8 #include<string> 9 #include<stdio.h> 10 #include<sstream> 11 #include<fstream> 12 #include<cmath> 13 #include<set> 14 #include<math.h> 15 using namespace std; 16 typedef long long ll; //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 17 typedef pair<int, int> P; 18 int m, n, k; 19 ll mapp[50][50] = {}, par[105] = {}, ranks[105] = {}, sizes[105] = {}; 20 bool vis[105]; 21 char temp[55] = {}; 22 vector<int> ans; 23 void init(int n) { 24 memset(par, 0, sizeof(par)); 25 memset(ranks, 0, sizeof(ranks)); 26 memset(sizes, 0, sizeof(sizes)); 27 memset(vis, 0, sizeof(vis)); 28 for (int i = 0; i < 2 * n; i++) { 29 par[i] = i; 30 if (i > n - 1) 31 sizes[i] = 1; 32 } 33 } 34 int find(int x) { 35 if (par[x] == x) 36 return x; 37 else 38 return par[x] = find(par[x]); 39 } 40 void uni(int x, int y) { 41 int tx = x, ty = y; 42 x = find(x); 43 y = find(y); 44 if (x == y) 45 return; 46 if (ranks[x] < ranks[y]) { 47 par[x] = y; 48 sizes[y] += sizes[x]; 49 } 50 else { 51 par[y] = x; 52 sizes[x] += sizes[y]; 53 if (ranks[x] == ranks[y])ranks[x]++; 54 } 55 } 56 int check(int l, int j) { 57 int cnt = 0; 58 int flag = 0; 59 for (int i = 0; i < m; ++i) 60 if (mapp[l][i] == mapp[j][i]) 61 cnt++; 62 if (cnt >= k) 63 flag++; 64 cnt = 0; 65 for (int i = 0; i < m; ++i) 66 if (mapp[l][i] == mapp[j][m - i - 1]) 67 cnt++; 68 if (cnt >= k) { 69 if (flag) 70 return 3; 71 return 2; 72 } 73 else { 74 if (flag) 75 return 1; 76 return 0; 77 } 78 } 79 void solve() { 80 init(n); 81 int anss = 0; 82 ans = vector<int>(); 83 for (int i = 0; i < n; i++) 84 for (int j = i + 1; j < n; j++) { 85 if (!check(i, j)) { 86 cout << -1 << endl; 87 return; 88 } 89 if (check(i, j) == 1) { 90 uni(i, j); uni(i + n, j + n); 91 } 92 else if (check(i, j) == 2) { 93 uni(i, j + n); uni(i + n, j); 94 } 95 } 96 for (int i = 0; i < n; i++) { 97 if (find(i) == find(i + n)) { 98 cout << -1 << endl; 99 return; 100 } 101 int r1 = find(i), r2 = find(i + n); 102 if (vis[r1])continue; 103 if (vis[r2]) { ans.push_back(i); continue; } 104 if (sizes[r1] < sizes[r2]) { 105 vis[r1] = 1; 106 } 107 else { 108 vis[r2] = 1; 109 ans.push_back(i); 110 } 111 } 112 cout << ans.size() << endl; 113 for (int i = 0; i < ans.size(); ++i) 114 cout << ans[i] + 1 << ' '; 115 cout << endl; 116 } 117 int main() { 118 int t; 119 cin >> t; 120 for (int i = 0; i < t; i++) { 121 cin >> n >> m >> k; 122 for (int i = 0; i < n; i++) { 123 cin >> temp; 124 for (int j = 0; j < m; j++) 125 mapp[i][j] = ((temp[j] == '1') ? 0 : 1); 126 } 127 solve(); 128 } 129 return 0; 130 }