2019浙江省省赛
B - Element Swapping ZOJ - 4101
https://blog.csdn.net/qq_41700151/article/details/89646207
E - Sequence in the Pocket ZOJ - 4104
题意:t 组数据,每组给出 n 个数,每次从 n 个数中选出 1 个数移动到序列的开头,问使序列不减少所需的最小操作次数
思路:建一个备份数组,将序列复制并排序后,从后向前与原数组比较,每找到一个相同的说明不需要移动,最后输出最小移动数即可(寻找最大,次大,次次大......,每一个相同就说明不需移动)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<string> 5 #include<cstring> 6 #include<cmath> 7 #include<ctime> 8 #include<algorithm> 9 #include<utility> 10 #include<stack> 11 #include<queue> 12 #include<vector> 13 #include<set> 14 #include<map> 15 #define EPS 1e-9 16 #define PI acos(-1.0) 17 #define INF 0x3f3f3f3f 18 #define LL long long 19 const int MOD = 1E9 + 7; 20 const int N = 1000 + 5; 21 const int dx[] = { 0,0,-1,1,-1,-1,1,1 }; 22 const int dy[] = { -1,1,0,0,-1,1,-1,1 }; 23 using namespace std; 24 int a[N]; 25 int b[N]; 26 int main() { 27 int t; 28 scanf("%d", &t); 29 while (t--) { 30 int n; 31 scanf("%d", &n); 32 for (int i = 1; i <= n; i++) 33 scanf("%d", &a[i]), b[i] = a[i]; 34 sort(b + 1, b + n + 1); 35 int cnt = n; 36 for (int i = cnt; i >= 1; i--) 37 if (a[i] == b[cnt]) 38 cnt = cnt - 1; 39 printf("%d\n", cnt); 40 } 41 return 0; 42 }
F - Abbreviation ZOJ - 4105
题意:t 组数据,每组给出一个字符串,要求删除字符串中的 a、e、i、o、u、y 并输出,如果这些字母在字首的话不必删除
思路:首字母不必考虑直接输出,然后对剩下的字符暴力枚举即可
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<string> 5 #include<cstring> 6 #include<cmath> 7 #include<ctime> 8 #include<algorithm> 9 #include<utility> 10 #include<stack> 11 #include<queue> 12 #include<vector> 13 #include<set> 14 #include<map> 15 #define EPS 1e-9 16 #define PI acos(-1.0) 17 #define INF 0x3f3f3f3f 18 #define LL long long 19 const int MOD = 1E9+7; 20 const int N = 1000+5; 21 const int dx[] = {0,0,-1,1,-1,-1,1,1}; 22 const int dy[] = {-1,1,0,0,-1,1,-1,1}; 23 using namespace std; 24 int main() { 25 int t; 26 scanf("%d",&t); 27 while(t--){ 28 memset(str,'\0',sizeof(str)); 29 scanf("%s",str); 30 printf("%c",str[0]); 31 for(int i=1;i<strlen(str);i++){ 32 if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='y') 33 continue; 34 printf("%c",str[i]); 35 } 36 printf("\n"); 37 } 38 return 0; 39 }
G - Lucky 7 in the Pocket ZOJ - 4106
题意:t 组数据,给出一个数 n,输出一个大于等于 n 的数 m,要求 m 能被 7 整除但不能被 4 整除
思路:首先判断能否被 7 整除,然后再判断能否被 4 整除,在这个过程中设置一个变量 x 来记录数的因子,每次令因子 +1 来减少枚举次数
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<string> 5 #include<cstring> 6 #include<cmath> 7 #include<ctime> 8 #include<algorithm> 9 #include<utility> 10 #include<stack> 11 #include<queue> 12 #include<vector> 13 #include<set> 14 #include<map> 15 #define EPS 1e-9 16 #define PI acos(-1.0) 17 #define INF 0x3f3f3f3f 18 #define LL long long 19 const int MOD = 1E9+7; 20 const int N = 1000+5; 21 const int dx[] = {0,0,-1,1,-1,-1,1,1}; 22 const int dy[] = {-1,1,0,0,-1,1,-1,1}; 23 using namespace std; 24 int main() { 25 int t; 26 scanf("%d",&t); 27 while(t--){ 28 memset(str,'\0',sizeof(str)); 29 scanf("%s",str); 30 printf("%c",str[0]); 31 for(int i=1;i<strlen(str);i++){ 32 if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='y') 33 continue; 34 printf("%c",str[i]); 35 } 36 printf("\n"); 37 } 38 return 0; 39 }
H - Singing Everywhere ZOJ - 4107
题意:t 组数据,每组给出 n 个数,在这 n 个数中,设 1<k<n,若满足 ak-1<ak 并且 ak+1<ak 则 k 视为一个峰,现在可以在这 n 个数中删除 0 或 1 个数,且要使删除后峰的个数最小,求删除后峰的个数
1 #include<bits/stdc++.h> 2 const int maxn = 1e5 + 10; 3 using namespace std; 4 int arr[maxn], brr[maxn], crr[maxn]; 5 int main() { 6 int T; 7 while (~scanf("%d", &T)) { 8 while (T--) { 9 int n; 10 scanf("%d", &n); 11 int cnt = 0; 12 for (int i = 1;i <= n;i++) scanf("%d", &arr[i]); 13 for (int i = 2;i < n;i++) { 14 if (arr[i] > arr[i - 1] && arr[i] > arr[i + 1])brr[++cnt] = arr[i], crr[cnt] = i; 15 } 16 int ans = cnt; 17 int flag1 = 0, flag2 = 0; 18 for (int i = 1;i <= cnt - 1;i++) { 19 if (crr[i] + 2 == crr[i + 1]) { 20 if (brr[i] == brr[i + 1])flag1 = 1; 21 else flag2 = 1; 22 } 23 } 24 if (flag1)ans -= 2; 25 else if (flag2) ans -= 1; 26 printf("%d\n", ans); 27 } 28 } 29 return 0; 30 }
I - Fibonacci in the Pocket ZOJ - 4108
题意:给出两个数a b 要你求斐波拉契数列中第a项到第b项之和是奇数还是偶数
思路:我们先来观察斐波拉契数列的奇偶性构成为 奇 奇 偶 奇 奇 偶 ...... 奇 奇 偶 这样三个一组的循环,那我们就来判断a b分别为某个循环中的第几个数 就用string来存a和b 然后每一个字符转换成整型-48就是对应的每位数字用各个数对3求余 就能得到是某个循环中的哪位数了
1 #include<iostream> 2 #include<map> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #include<cstdio> 9 #include<set> 10 #define ll long long int 11 #define inf 0x3f3f3f3f 12 const int maxn = 110; 13 using namespace std; 14 int main() { 15 int T; 16 while (~scanf("%d", &T)) { 17 while (T--) { 18 char a[10005], b[10005]; 19 scanf("%s %s", a, b); 20 int suma = 0, sumb = 0; 21 int lena = strlen(a), lenb = strlen(b); 22 for (int i = 0;i < lena;i++) { 23 suma = suma + (a[i] - '0'); 24 } 25 for (int i = 0;i < lenb;i++) { 26 sumb = sumb + (b[i] - '0'); 27 } 28 int fa, fb; 29 fa = suma % 3, fb = sumb % 3; 30 if (fa == 0 && fb == 0)printf("0\n"); 31 if (fa == 0 && fb == 1)printf("1\n"); 32 if (fa == 0 && fb == 2)printf("0\n"); 33 if (fa == 1 && fb == 0)printf("0\n"); 34 if (fa == 1 && fb == 1)printf("1\n"); 35 if (fa == 1 && fb == 2)printf("0\n"); 36 if (fa == 2 && fb == 0)printf("1\n"); 37 if (fa == 2 && fb == 1)printf("0\n"); 38 if (fa == 2 && fb == 2)printf("1\n"); 39 } 40 } 41 return 0; 42 }
J - Welcome Party ZOJ - 4109
题意:给你 n 个人,n 个人有 m 对关系,对于一个人,如果他上场了,发现没有他认识的人,会产生一个值,也就是 ans ++,否则不会产生,问安排上场顺序让 ans 最小,且让上场序列的字典序最小。
题解:将 n 个人的 m 对关系来用并查集连起来,合并的时候只往小了合并,让小的先上场,这样合并完,跑一边 fa [ i ] 数组,就可以知道有几个连通块,ans 就是连通块的个数,把所有连通块的第一个节点都放到 0 里面,对 0 节点开始进行遍历,然后 BFS 时用优先队列优化。
注释掉的是正确的,留着的是错的(还未解决)
1 #include<iostream> 2 #include<map> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<cmath> 7 #include<queue> 8 #include<cstdio> 9 #include<set> 10 #define ll long long int 11 #define inf 0x3f3f3f3f 12 const int maxn = 1e6+10; 13 using namespace std; 14 const int MAX = 1000010; 15 int f[MAX], n, m; 16 int ans[MAX], cnt; 17 //int book[MAX]; 18 vector<int> v[MAX]; 19 int getf(int v) 20 { 21 return f[v] == v ? v : f[v] = getf(f[v]); 22 } 23 void merge(int u, int v) 24 { 25 int t1 = getf(u); 26 int t2 = getf(v); 27 if (t1 > t2) f[t1] = t2; 28 else f[t2] = t1; 29 } 30 void bfs(int x) 31 { 32 priority_queue<int, vector<int>, greater<int> > pq;//因为同等条件下先输出字典序小的所以用优先队列 33 pq.push(x); 34 while (!pq.empty()) 35 { 36 int now = pq.top(); 37 pq.pop(); 38 //if (book[now] == 0) 39 //{ 40 //book[now] = 1; 41 ans[cnt++] = now; 42 for (int i = 0;i < v[now].size();i++) 43 { 44 //if (book[v[now][i]] == 0) 45 //{ 46 pq.push(v[now][i]); 47 //} 48 } 49 //} 50 51 } 52 } 53 void init(int n) { 54 for (int i = 0;i <= n;i++) 55 { 56 f[i] = i; 57 v[i].clear(); 58 //book[i] = 0; 59 } 60 } 61 int main() 62 { 63 int t; 64 scanf("%d", &t); 65 while (t--) 66 { 67 scanf("%d%d", &n, &m); 68 init(n); 69 int x, y; 70 for (int i = 1;i <= m;i++) 71 { 72 scanf("%d%d", &x, &y); 73 x<y? v[x].push_back(y): v[y].push_back(x); 74 /* v[x].push_back(y); 75 v[y].push_back(x);*/ 76 merge(x, y); 77 } 78 int ans1 = 0; 79 for (int i = 1;i <= n;i++) 80 { 81 if (f[i] == i) 82 { 83 v[0].push_back(i); 84 ans1++; 85 } 86 } 87 cnt = 0; 88 bfs(0); 89 printf("%d\n", ans1); 90 for (int i = 1;i < cnt;i++) 91 { 92 printf("%d%c", ans[i], i == cnt - 1 ? '\n' : ' '); 93 } 94 } 95 return 0; 96 }

浙公网安备 33010602011771号