保证女比男大的情况下,两人年龄差距最小时多少
1 #include <cstdio> 2 #include <cstring> 3 4 char ls[12][20] = {"rat", "ox", "tiger", "rabbit", "dragon", "snake", "horse", "sheep", 5 "monkey", "rooster", "dog", "pig"}; 6 int main() 7 { 8 int T; 9 char a[20], b[20]; 10 scanf("%d", &T); 11 while(T--) { 12 scanf(" %s%s", a, b); 13 int c, d; 14 for(int i = 0; i < 12; i++) { 15 if(strcmp(ls[i], a) == 0) { 16 c = i; 17 } 18 if(strcmp(ls[i], b) == 0) { 19 d = i; 20 } 21 } 22 23 if(c == d) 24 printf("12 "); 25 else 26 printf("%d ", (d + 12 - c) % 12); 27 } 28 return 0; 29 }
HDU 6216 A Cubic number and A Cubic Number
给出一个素数,为该素数是否是两个立方数之差,写出代数式,进行优化。
1 #include <cstdio> 2 #include <cmath> 3 #include <set> 4 #include <cstdlib> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 1000010; 9 long long ls[maxn]; 10 int main() 11 { 12 long long i; 13 for(i = 0; i < 1000000; i++) { 14 ls[i] = 3 * i * i + 3 * i + 1; 15 } 16 17 int T; 18 long long x, y; 19 while(scanf("%d", &T) != EOF) { 20 while(T--) 21 { 22 scanf("%lld", &x); 23 24 int i, j, k, l; 25 for(i = l = 0, j = 1000000; j - i > 1; ) 26 { 27 k = (i+j) / 2; 28 if(x > ls[k]) 29 i = k; 30 else if(x < ls[k]) 31 j = k; 32 else 33 { 34 l = 1; 35 break; 36 } 37 38 } 39 40 if(!l) 41 puts("NO"); 42 else 43 puts("YES"); 44 } 45 } 46 return 0; 47 }
计算四个的和,完全是卡边界的问题,具体看代码
#include<stdio.h> #include <iostream> using namespace std; int main() { unsigned long long n,a,b,c,d,sum; scanf("%lld",&n); while(n--) { scanf("%lld%lld%lld%lld",&a,&b,&c,&d); if(a==4611686018427387904&&b==4611686018427387904 &&c==4611686018427387904&&d==4611686018427387904) printf("18446744073709551616 "); else cout<<(unsigned long long)(a+b+c+d)<<endl; } return 0; }
最外边的一只兔子往中间的空位置上跳一次算一个计数,问最多能够跳几次。
从第二个开始计算间距差,累加即可。另外还需要反着跑一遍,因为最外面的兔子有开头和结尾两只。
1 #include<stdio.h> 2 3 int main() 4 { 5 int t,n,i,sum1,sum2; 6 int a[520]; 7 scanf("%d",&t); 8 while(t--) 9 { 10 sum1=0; 11 sum2=0; 12 scanf("%d",&n); 13 for(i=1;i<=n;i++) 14 scanf("%d",&a[i]); 15 for(i=n;i>2;i--) 16 sum1+=(a[i]-a[i-1]-1); 17 for(i=1;i<n-1;i++) 18 sum2+=(a[i+1]-a[i]-1); 19 if(sum1>sum2) 20 printf("%d ",sum1); 21 else 22 printf("%d ",sum2); 23 } 24 return 0; 25 }
计算最长非乱序子序列,计算一边最长上不下降子序列和最长不上升子序列即可。
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 100010; 6 const int inf = 99999999; 7 int A[maxn], B[maxn], g[maxn], d[maxn]; 8 int n, t; 9 10 int main() 11 { 12 int T; 13 scanf("%d", &T); 14 while(T--) { 15 scanf("%d%d", &n, &t); 16 for(int i = 0; i < n; i++) { 17 scanf("%d", &A[i]); 18 } 19 20 for(int i = 1; i <= n; i++) { 21 g[i] = inf; 22 } 23 for(int i = 0; i < n; i++) { 24 int k = upper_bound(g + 1, g + n + 1, A[i]) - g; 25 d[i] = k; 26 g[k] = A[i]; 27 } 28 int s1 = 0; 29 for(int i = 0; i < n; i++) 30 if(s1 < d[i]) 31 s1 = d[i]; 32 33 int j = 0; 34 for(int i = n - 1; i >= 0; i--) { 35 B[j++] = A[i]; 36 } 37 for(int i = 1; i <= n; i++) { 38 g[i] = inf; 39 } 40 for(int i = 0; i < n; i++) { 41 int k = upper_bound(g + 1, g + n + 1, B[i]) - g; 42 d[i] = k; 43 g[k] = B[i]; 44 } 45 int s2 = 0; 46 for(int i = 0; i < n; i++) 47 if(s2 < d[i]) 48 s2 = d[i]; 49 50 //printf("%d %d ",s1,s2); 51 int ans = max(s1, s2);//最长上升或者下降子序列,意味者修改更少 52 if(n - t <= ans) 53 printf("A is a magic array. "); 54 else 55 printf("A is not a magic array. "); 56 } 57 return 0; 58 }
判断一个数组是否是凸形。
1 #include <cstdio> 2 const int maxn = 100000 + 10; 3 int a[maxn]; 4 5 int main() 6 { 7 int T; 8 scanf("%d", &T); 9 while(T--) { 10 int n; 11 scanf("%d", &n); 12 for(int i = 0; i < n; i++) { 13 scanf("%d", &a[i]); 14 } 15 int q = 0, h = 0, i; 16 for(i = 0; i < n - 1; i++) { 17 if(a[i] < a[i + 1]) 18 q++; 19 else 20 break; 21 } 22 for(; i < n - 1; i++) { 23 if(a[i] > a[i + 1]) 24 h++; 25 else 26 break; 27 } 28 //printf("%d %d ", q, h); 29 30 if(q && h && q + h == n - 1) 31 puts("Yes"); 32 else 33 puts("No"); 34 } 35 return 0; 36 }
给出两个数组,问第一个数组通过加上一个数和另一个数的最大耦合度是多少
对应做差,看哪个差出现的次数最多。
1 #include <cstdio> 2 #include <map> 3 4 using namespace std; 5 const int maxn = 100000 + 10; 6 int D[maxn], S[maxn]; 7 int n; 8 9 map<int, int> mp; 10 int main() 11 { 12 int T; 13 scanf("%d", &T); 14 while(T--) { 15 scanf("%d", &n); 16 for(int i = 0; i < n; i++) { 17 scanf("%d", &D[i]); 18 } 19 for(int i = 0; i < n; i++) { 20 scanf("%d", &S[i]); 21 } 22 23 mp.clear(); 24 for(int i = 0; i < n; i++) { 25 mp[S[i] - D[i]]++; 26 } 27 int maxc = 0; 28 map<int, int>::iterator it; 29 for(it = mp.begin(); it != mp.end(); it++) { 30 if((*it).second > maxc) 31 maxc = (*it).second; 32 } 33 printf("%d ", maxc); 34 } 35 return 0; 36 }
ZOJ 4035 Doki Doki Literature Club
给出单词列表,挑出几个使得满意度最大。
简单贪心,不过坑在排序上,细心分析一下。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 const int wl = 15 + 10; 7 const int maxw = 100 + 10; 8 struct Word { 9 char w[wl]; 10 int s; 11 bool operator < (const struct Word &a) const { 12 if(a.s == s) { 13 if(strcmp(a.w, w) > 0) 14 return 1; 15 else 16 return 0; 17 } 18 return a.s < s; 19 } 20 }wo[maxw]; 21 22 int n, m; 23 int main() 24 { 25 int T; 26 scanf("%d", &T); 27 while(T--) { 28 scanf("%d%d", &n, &m); 29 for(int i = 1; i <= n; i++) { 30 scanf("%s %d", wo[i].w, &wo[i].s); 31 } 32 33 sort(wo + 1, wo + n + 1); 34 /*for(int i = 1; i <= n; i++) { 35 printf("%s %d ", wo[i].w, wo[i].s); 36 }*/ 37 long long sc = 0; 38 for(int i = 1; i <= m; i++) { 39 sc += (long long)(m - i + 1) * wo[i].s; 40 } 41 printf("%lld",sc); 42 for(int i = 1; i <= m; i++) { 43 printf(" %s", wo[i].w); 44 } 45 puts(""); 46 } 47 return 0; 48 }
给出一个字符数组,问最少需要更改几次字符。
去掉头和尾,直接数一遍,去最小即可。
1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4 5 const int maxn = 100000 + 10; 6 char a[maxn]; 7 int n, m; 8 9 int main() 10 { 11 int T; 12 scanf("%d", &T); 13 while(T--) { 14 scanf("%d%d", &n, &m); 15 scanf(" %s", a+1); 16 int rn = 0; 17 for(int i = 2; i <= m; i++) { 18 if(a[i] == 'R') 19 rn++; 20 } 21 int ln = 0; 22 for(int i = m; i <= n - 1; i++) { 23 if(a[i] == 'L') 24 ln++; 25 } 26 printf("%d ",min(rn, ln)); 27 } 28 return 0; 29 }
给出技能的顺序,问最多能放几次大招。
使用六种按法直接暴力。其中使用了erase方法。
1 #include <cstdio> 2 #include <algorithm> 3 #include <vector> 4 #include <iostream> 5 using namespace std; 6 7 int f(vector<char> v, char a, char b, char c) { 8 int ans = 0; 9 vector<char> tp = v; 10 11 int fa = -1; 12 for(int i = 0; i < tp.size() - 2; i++) { 13 if(tp[i] == a && tp[i + 1] == a && tp[i + 2] == a) { 14 fa = i; 15 break; 16 } 17 } 18 if(fa != -1) { 19 tp.erase(tp.begin() + fa, tp.begin() + fa + 3); 20 ans++; 21 } 22 else { 23 for(int i = 0; i < tp.size(); i++) { 24 if(tp[i] == a){ 25 tp.erase(tp.begin() + i); 26 i = 0; 27 } 28 } 29 } 30 31 int fb = -1; 32 for(int i = 0; i < tp.size() - 2; i++) { 33 if(tp[i] == b && tp[i + 1] == b && tp[i + 2] == b) { 34 fb = i; 35 break; 36 } 37 } 38 if(fb != -1) { 39 tp.erase(tp.begin() + fb, tp.begin() + fb + 3); 40 ans++; 41 } 42 else { 43 for(int i = 0; i < tp.size(); i++) { 44 if(tp[i] == b){ 45 tp.erase(tp.begin() + i); 46 i = 0; 47 } 48 } 49 } 50 51 int fc = -1; 52 for(int i = 0; i < tp.size() - 2; i++) { 53 if(tp[i] == c && tp[i + 1] == c && tp[i + 2] == c) { 54 fc = i; 55 break; 56 } 57 } 58 if(fc != -1) { 59 tp.erase(tp.begin() + fc, tp.begin() + fc + 3); 60 ans++; 61 } 62 else { 63 for(int i = 0; i < tp.size(); i++) { 64 if(tp[i] == c){ 65 tp.erase(tp.begin() + i); 66 i = 0; 67 } 68 } 69 } 70 71 return ans; 72 } 73 74 int main() 75 { 76 int T; 77 char ch; 78 scanf("%d", &T); 79 while(T--) { 80 vector<char> v; 81 vector<int> a; 82 for(int i = 0; i < 9; i++) { 83 scanf(" %c", &ch); 84 v.push_back(ch); 85 } 86 a.push_back(f(v, 'g', 'a', 'o')); 87 a.push_back(f(v, 'g', 'o', 'a')); 88 a.push_back(f(v, 'a', 'g', 'o')); 89 a.push_back(f(v, 'a', 'o', 'g')); 90 a.push_back(f(v, 'o', 'g', 'a')); 91 a.push_back(f(v, 'o', 'a', 'g')); 92 int ans = 0; 93 for(int i = 0; i < a.size(); i++) { 94 ans = max(ans, a[i]); 95 } 96 printf("%d ",ans); 97 } 98 return 0; 99 }
给出几个集合,问他们相交的集合中大于m的长度之和。
直接暴力。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 100 + 10l; 6 int n, m, x, y; 7 struct Node { 8 int l, r; 9 }a[maxn], b[maxn]; 10 11 int main() 12 { 13 int T; 14 scanf("%d", &T); 15 while(T--) { 16 scanf("%d%d%d%d", &n, &m, &x, &y); 17 for(int i = 0; i < x; i++) { 18 scanf("%d%d", &a[i].l, &a[i].r); 19 } 20 for(int i = 0; i < y; i++) { 21 scanf("%d%d", &b[i].l, &b[i].r); 22 } 23 24 25 int ans = 0; 26 for(int i = 0; i < x; i++) { 27 if(a[i].r - a[i].l + 1 < m) 28 continue; 29 30 for(int j = 0; j < y; j++) { 31 if(b[j].r - b[j].l + 1 < m) 32 continue; 33 34 int L, R; 35 L = max(a[i].l, b[j].l); 36 R = min(a[i].r, b[j].r); 37 int len = R - L + 1; 38 if(len >= m) { 39 ans += (len - m + 1); 40 } 41 } 42 } 43 printf("%d ", ans); 44 } 45 return 0; 46 }
其中几道区域赛的题和浙江省赛的题,刷题还是很慢呐,不过相比之前还是感觉有进步的,接下来就是尽可能的多熟悉一些算法,多敲模板,找到比赛的感觉。