[agc006d]Median Pyramid Hard
来源:互联网 发布:磁盘删除分区linux 编辑:程序博客网 时间:2024/06/11 22:09
前言
一道见过的题。
题目大意
一个长度为奇数的排列,令b[i]=median(a[i],a[i+1],a[i+2])。
median表示中位数。
不断执行上述变化序列最终变成1个数,它是什么?
做法
二分后变成01序列,考虑最终什么时候会是0。
我们发现相邻两个是同一个数字,这两列都是柱子,即从下到上都一样。
case 1:正中间是0柱子。
case 2:两边最近的柱子都是0柱子。
case 3:其中一边最近的柱子是1柱子,另一边最近的柱子是0柱子,0柱子离中间更近。
case 4:只有一边有柱子,那个柱子是0柱子。
case 5:没有柱子,0的数量多于1的数量。
#include<cstdio>#include<algorithm>#define fo(i,a,b) for(i=a;i<=b;i++)#define fd(i,a,b) for(i=a;i>=b;i--)using namespace std;const int maxn=100000+10;int a[maxn*2],b[maxn*2];bool bz[maxn*2];int i,j,k,l,r,mid,t,n,m;bool check(int ans){ int i,j,k,t; fo(i,1,2*n-1) b[i]=(a[i]>ans); fo(i,1,2*n-1) bz[i]=0; fo(i,1,2*n-2) if (b[i]==b[i+1]) bz[i]=bz[i+1]=1; if (b[n]==0&&bz[n]) return 1;//case 1,0 in middle fd(j,n,0) if (!j||bz[j]) break; fo(k,n,2*n) if (k>2*n-1||bz[k]) break; if (j&&k<=2*n-1){ if (b[j]==0&&b[k]==0) return 1;//case 2,left and right are both 0 if (b[j]==1&&b[k]==0&&k-n<n-j) return 1; if (b[j]==0&&b[k]==1&&n-j<k-n) return 1;//case 3,one side is 1 and another side is 0,0 nearer } else{ if (j&&b[j]==0) return 1; if (k<=2*n-1&&b[k]==0) return 1;//case 4,only one side have 0 t=0; fo(i,1,2*n-1) if (!b[i]) t++;else t--; if (t>0) return 1;//case 5,have no } return 0;}int main(){ scanf("%d",&n); fo(i,1,2*n-1) scanf("%d",&a[i]); l=1;r=2*n-1; while (l<r){ mid=(l+r)/2; if (check(mid)) r=mid;else l=mid+1; } printf("%d\n",l);}
阅读全文
0 0
- [agc006d]Median Pyramid Hard
- [AtCoder2165]Median Pyramid Hard 二分答案
- AtCoder:Median Pyramid Hard(思维)
- AtCoder:Median Pyramid Easy(思维)
- Median of Two Sorted Arrays(hard)
- [hard]295. Find Median from Data Stream
- [Hard]Median of Two Sorted Arrays
- 4. Median of Two Sorted Arrays Hard
- 4. Median of Two Sorted Arrays Hard
- No4(Hard) Median of Two Sorted Arrays
- <LeetCode><Hard> 4 Median of Two Sorted Arrays
- [4, Hard, C++] Median of Two Sorted Arrays
- Hard-题目50:4. Median of Two Sorted Arrays
- leetcode 4. Median of Two Sorted Arrays(Hard)
- Leetcode 4. Median of Two Sorted Arrays (Hard) (cpp)
- Leetcode 295. Find Median from Data Stream (Hard) (cpp)
- LeetCode第四关:Median of Two Sorted Arrays --> Difficulty: Hard
- 4. Median of Two Sorted Arrays (Hard)
- chosen选择多项
- spring boot application properties配置详解
- 好的用户界面-界面设计的一些技巧
- jsp文件中fn函数报“The function fn:split is undefined”错
- vue cli脚手架工具安装以及搭建vue项目
- [agc006d]Median Pyramid Hard
- 微信开发-公众号支付(2)-统一下单
- 版本差异对比(六) -- 比较规则定义
- Nginx配置文件(nginx.conf)配置详解(二)
- TextRank 处理短文本获得指定关键字
- 比RBAC更好的权限认证方式(Auth类认证)
- 在CentOS中都安装Java和Perl(一)
- Python基础:列表,元组,字典,集合相关基本操作
- 程序员快速处理代码bug的5大技巧