Codeforces - 590A. Median Smoothing
2017-07-05 18:13
225 查看
Median Smoothing
题目链接分类:思维、构造
1.题意概述
给你一个长度为n的01串a[1…n],进行多轮变换,每轮操作:b[1]=a[1],b[n]=a[n]
位于2…n-1的数b[i]为a[i−1],a[i],a[i+1]从大到小排列的中位数
现在问你进行多少轮变换后b数组不再发生变化,且求它的最终状态。
2.解题思路
尝试多写出一个来,发现规律:…010…-> …000…
…1101011…->…1110111…->…1111111…
…01010…->…00100…->…00000…
…01010…->…00100…->…00000…
…1101010…->..1110100…->…1111000
最终趋向总是一堆0一堆1,而会改变的,都是那些01间杂的序列,最终次数肯定是最大的间隔数,我们统计一下容易发现:
当间隔长度为奇数时候,结果总是一堆0或者一堆1!
当间隔长度为偶数时候,结果总是一半0一半1!
3.AC代码
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define maxn 500010 #define lson root << 1 #define rson root << 1 | 1 #define lent (t[root].r - t[root].l + 1) #define lenl (t[lson].r - t[lson].l + 1) #define lenr (t[rson].r - t[rson].l + 1) #define N 1111 #define eps 1e-6 #define pi acos(-1.0) #define e exp(1.0) using namespace std; const int mod = 1e9 + 7; typedef long long ll; typedef unsigned long long ull; int a[maxn]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long _begin_time = clock(); #endif int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[n + 1] = a ; int cnt = 1, ans = 0, l = 1, r = 1; for (int i = 2; i <= n + 1; i++) { if (a[i] != a[i - 1]) { db97 r = i; cnt++; } else { ans = max(ans, (cnt - 1) / 2); if (cnt & 1) { for (int j = l; j <= r; j++) a[j] = a[l]; } else { int mid = l + r >> 1; for (int j = l; j <= r; j++) a[j] = j <= mid ? a[l] : a[r]; } cnt = 1; l = r = i; } } printf("%d\n", ans); for (int i = 1; i <= n; i++) if (i == 1) printf("%d", a[i]); else printf(" %d", a[i]); puts(""); #ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms.", _end_time - _begin_time); #endif return 0; }
相关文章推荐
- Codeforces 315C
- codeforces 557D Vitaly and Cycle
- CodeForces 651 C Watchmen
- 思维题-CodeForces 371B Fox Dividing Cheese
- codeforces-789B. Masha and geometric depression(集合+模拟)
- CodeForces - 827B High Load(贪心+思维)
- CodeForces-329C(div1):Graph Reconstruction(随机&构造)
- Codeforces 120F Spiders
- CodeForces 487B Strip
- codeforces 651C Watchmen
- Codeforces 715A Plus and Square Root
- codeforces 414C 归并排序
- Insomnia cure CodeForces - 148A (数论的扩展解法)
- Codeforces 19E 树上差分
- CodeForces 314B - Sereja and Periods 很有思维的DP
- Present - CODEFORCES, 460c 二分 扫描线求和
- codeforces_620D. Professor GukiZ and Two Arrays
- codeforces-500【B思维】【C贪心】
- CodeForces 598B Queries on a String
- codeforces 401D Roman and Numbers (数位dp)