@Dmaxiya
2018-08-17T02:28:59.000000Z
字数 5519
阅读 1248
Codeforces
Contests 链接:Educational Codeforces Round 35
过题数:5
排名:116/9081
给定一个长度为 的序列,找到所有数字 中的最小值,找到所有等于最小值的两个值中,距离最近的两个数字,输出这个距离。数据保证至少有两个最小值。
按照题意模拟一遍。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 100000 + 100;int n;int Min;int num[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {Min = INT_MAX;for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);Min = min(Min, num[i]);}int dis = INT_MAX;int last = -200000;for(int i = 0; i < n; ++i) {if(num[i] == Min) {dis = min(dis, i - last);last = i;}}printf("%d\n", dis);}return 0;}
有两块蛋糕,分别被切成 片与 片,要放到 个盘子里,使得每个盘子里只能有一种蛋糕,每片蛋糕至少被放在一个盘子里,要让所有盘子中的蛋糕片数的最小值最大,输出这个最大值 。
因为要保证每片蛋糕都被放在一个盘子里,所以让 作为片数小的那个,从 到 遍历最小值,每次判断该最小值是否成立,都先放 片蛋糕,再放 片蛋糕。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longint n, a, b;bool judge(int least, int aa, int bb) {int i;for(i = 0; i < n; ++i) {if(aa >= least) {aa -= least;} else if(bb >= least) {bb -= least;} else {break;}}return i == n;}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d%d", &n, &a, &b) != EOF) {int ans = 0;if(a > b) {swap(a, b);}for(int i = a; i >= 0; --i) {if(judge(i, a, b)) {ans = i;break;}}printf("%d\n", ans);}return 0;}
有三种霓虹灯,第 种霓虹灯每 秒亮一次,问,如果给定 ,能否找到三个打开这三种灯的时间点,使得最后一盏灯被打开后,每一秒都至少有一盏灯在发亮。
因为有三盏灯要轮流发亮,所以如果三盏灯的间隔时间 都大于 ,则必然找不到任何的方式使得每一秒都至少有一盏灯在发亮,对于不大于 的情况,暴力枚举判断即可。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 50;int k1, k2, k3;bool flag;bool lit[maxn];bool judge(int a, int b, int c) {int aa = a;int bb = b;int cc = c;memset(lit, 0, sizeof(lit));while(aa < maxn) {lit[aa] = true;aa += k1;}while(bb < maxn) {lit[bb] = true;bb += k2;}while(cc < maxn) {lit[cc] = true;cc += k3;}for(int i = max(a, max(b, c)); i < maxn; ++i) {if(!lit[i]) {return false;}}return true;}int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d%d", &k1, &k2, &k3) != EOF) {if(k1 >= 4 && k2 >= 4 && k3 >= 4) {printf("NO\n");continue;}flag = false;for(int i = 1; i <= 5; ++i) {for(int j = 1; j <= 5; ++j) {for(int k = 1; k <= 5; ++k) {if(judge(i, j, k)) {flag = true;break;}}if(flag) {break;}}if(flag) {break;}}if(flag) {printf("YES\n");} else {printf("NO\n");}}return 0;}
给定一个长度为 的全排列 ,有 次操作,每次操作给定区间 ,将序列从 到 反转,求每次操作后,序列逆序对的奇偶性。
改变任意两个数字的的位置,只看这两个数字的话,整体的逆序对奇偶性改变,将 区间上的所有数字反转,则逆序对改变的次数为 ,其中 为反转长度,即 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 2000;int n, flag, m, l, r;int num[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {flag = 0;for(int i = 0; i < n; ++i) {scanf("%d", &num[i]);for(int j = 0; j < i; ++j) {if(num[j] > num[i]) {++flag;}}}flag %= 2;scanf("%d", &m);for(int i = 0; i < m; ++i) {scanf("%d%d", &l, &r);LL tmp = r - l + 1;if((tmp - 1) * tmp / 2 % 2 == 1) {flag = 1 - flag;}if(!flag) {printf("even\n");} else {printf("odd\n");}}}return 0;}
如果一个序列,可以通过进栈出栈操作达到非递减的状态,那么这个序列就是“可栈排序”的序列。现在有一个长度为 的排列,给出前 个数字 ,如果能够在这 个数字后将这个排列补全,使这个排列成为一个“可栈排序”序列,则该问题有解,输出字典序最大的那个解,否则输出 。
先对前 个数字用“栈排序”跑一遍,在栈内则存了一个不可“栈排序”的序列,这个序列一定由多个连续的递减序列拼成,从后往前扫一遍这个栈,每当碰到一个数字,就将小于这个数字的,没有出现过的数字添加按从大到小添加到原序列尾部。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>#include <functional>#include <iomanip>using namespace std;#define LL long longconst int maxn = 200000 + 100;int n, k, cnt, Index;bool vis[maxn];int num[maxn];int sta[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);// freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%d%d", &n, &k) != EOF) {cnt = 0;Index = 1;memset(vis, 0, sizeof(vis));for(int i = 0; i < k; ++i) {scanf("%d", &num[i]);vis[num[i]] = true;sta[cnt++] = num[i];while(cnt != 0 && sta[cnt - 1] == Index) {++Index;--cnt;}}for(int i = cnt - 1; i >= 0; --i) {int tmp = sta[i] - 1;while(tmp > 0 && !vis[tmp]) {num[k++] = tmp;--tmp;}}int tmp = n;while(tmp > 0 && !vis[tmp]) {num[k++] = tmp;--tmp;}cnt = 0;Index = 1;for(int i = 0; i < n; ++i) {sta[cnt++] = num[i];while(cnt != 0 && sta[cnt - 1] == Index) {++Index;--cnt;}}if(cnt == 0) {for(int i = 0; i < n; ++i) {if(i != 0) {printf(" ");}printf("%d", num[i]);}printf("\n");} else {printf("-1\n");}}return 0;}