DUANG: http://codeforces.com/contest/660
A
求让给定数组相邻元素互质所需要插入的数的个数并输出互质数组
不互质的中间加个1
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1111; ll num[maxn]; ll gcd(ll a, ll b){ if(b == 0) return a; return gcd(b, a%b); } int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d", &num[i]); vector<int> k; for(int i = 1; i < n; ++i){ if(gcd(num[i-1], num[i]) != 1){ k.push_back(i); } } printf("%d ", k.size()); int j = 0; for(int i = 0; i < n; ++i){ if(j < k.size() && i == k[j]){ printf("1 "); ++j; } printf("%d ", num[i]); } return 0; }
B
输入n行m人,按照规则坐好,再按照规则离开,输出离开顺序
瞎搞
#include <bits/stdc++.h> using namespace std; const int maxn = 444; bool vis[maxn]; int main(){ int n, m; scanf("%d %d", &n, &m); int a = 2*n+1, b = 1, c = 2*n+2, d = 2; //printf("%d %d %d %d ", a, b, c, d); memset(vis, true, sizeof(vis)); for(int i = 0; i < 100; ++i){ if(a <= m && vis[a]){ printf("%d ", a); vis[a] = false; a += 2; } if(b <= m && vis[b]){ printf("%d ", b); vis[b] = false; b += 2; } if(c <= m && vis[c]){ printf("%d ", c); vis[c] = false; c += 2; } if(d <= m && vis[d]){ printf("%d ", d); vis[d] = false; d += 2; } } return 0; }
C
给你n个数和k个把0变成1的机会,求最多几个1并输出那种情况
!!!maple师兄指点下撸出第一发尺取
#include <bits/stdc++.h> using namespace std; const int maxn = 333333; int num[maxn]; int main(){ int n, m; scanf("%d %d", &n, &m); for(int i = 0; i < n; ++i) scanf("%d", &num[i]); int l = 0, r = 0, ll = 0, rr = 0, ans = 0, tmp = 0, cnt = m; while(r < n){ if(num[r]){ tmp++; r++; } else{ if(cnt){ r++; cnt--; tmp++; } else{ if(!num[l]) cnt++; l++; tmp--; } } if(tmp > ans){ ans = tmp; ll = l; rr = r; } } printf("%d ", ans); for(int i = 0; i < ll; ++i) printf("%d ", num[i]); for(int i = ll; i < rr; ++i) printf("%d ", 1); for(int i = rr; i < n; ++i) printf("%d ", num[i]); return 0; }
D
给n个点求能组多少个平行四边形
题解给的想法更新了一下我的大脑 yeah~
两组能组成平行四边形的点的x坐标和相等、y坐标和相等
#include <bits/stdc++.h> using namespace std; const int maxn = 2222; int x[maxn], y[maxn]; int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; ++i) scanf("%d %d", &x[i], &y[i]); map<pair<int, int>, int> m; m.clear(); for(int i = 0; i < n; ++i) for(int j = 0; j < i; ++j) m[{x[i]+x[j], y[i]+y[j]}]++; int ans = 0; for(auto& p : m) ans += p.second*(p.second-1)/2; printf("%d ", ans); return 0; }
for(auto& p : m) 容器遍历新姿势?
另一种想法: 能构成平行四边形的一定是两个相同的变量
http://www.cnblogs.com/qscqesze/p/5373418.html
E、F..........233333333