[AtCoder ARC077 .E] [扫描线] [乱搞]guruguru
来源:互联网 发布:淘宝电商会计做帐流程 编辑:程序博客网 时间:2024/06/05 06:34
一次从a到b的变化可以看成一个(a,b)(a< b),或(a,m),(0,b)(a>b)的线段
可以枚举favorite值,用扫描线记录穿过了多少线段,那么从左端点到favorite值的部分就可以省去,注意a>b的情况,当穿过(0,b)的线段时,还要把(a,m)的部分删去
#include <cstdio>#include <iostream>#include <algorithm>#include <queue>#include <vector>#define fi first#define se secondusing namespace std;const int N=200010;typedef long long ll;int n,m,cnt,a[N];struct stp{ int fi,se,lk; stp(int x=0,int y=0,int z=0):fi(x),se(y),lk(z){} friend bool operator <(stp a,stp b){ return a.fi>b.fi; }}p[N];priority_queue<stp> Q;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void rea(int &x){ char c=nc(); x=0; for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());}inline int cmp(stp a,stp b){ return a.fi<b.fi; }int main(){ rea(n); rea(m); for(int i=1;i<=n;i++) rea(a[i]); for(int i=1;i<n;i++){ if(a[i]<a[i+1]) p[++cnt]=stp(a[i],a[i+1]); else p[++cnt]=stp(a[i],m),p[++cnt]=stp(0,a[i+1],m-a[i]); } ll ans=1LL<<60,cur=0; for(int i=1;i<=cnt;i++) cur+=p[i].se-p[i].fi; sort(p+1,p+1+cnt,cmp); int q=1; for(int i=1;i<=m;i++){ while(!Q.empty()&&Q.top().fi<i) cur+=Q.top().fi-Q.top().se-1+Q.top().lk,Q.pop(); cur-=Q.size(); while(q<=cnt&&p[q].fi<i) cur-=i-p[q].fi+p[q].lk-1,Q.push(stp(p[q].se,p[q].fi,p[q].lk)),q++; if(cur<ans) ans=cur; } cout<<ans<<endl; return 0;}
阅读全文
0 0
- [AtCoder ARC077 .E] [扫描线] [乱搞]guruguru
- [AtCoder ARC077 .F] [字符串] [乱搞] SS
- AtCoder RC 077E: guruguru 题解
- AtCoder:guruguru(思维 & 差分)
- AtCoder 077E gururu
- Atcoder ARC 077 E
- Atcoder 080 E
- 【AtCoder】(AtCoder Grand Contest 004)E
- Codeforces 463 E 乱搞
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
- [NOIP模拟题][数学][乱搞][DP?][扫描线][线段树]
- 【AtCoder】(square869120Contest #3)E
- Atcoder Grand Contest 011E
- Atcoder Grand Contest 013E
- AtCoder Regular Contest 077 E
- AtCoder Regular Contest 077 E
- AtCoder Regular Contest 075 E
- AtCoder Grand Contest 018 E
- java中保留两位小数
- Python reduce未定义
- CJOJ 1087 【NOIP2010】乌龟棋 / Luogu 1541 乌龟棋
- swift3.0 unexpectedly found nil while unwrapping an Optional value的解释 && Swift中Optional
- Delphi新注释
- [AtCoder ARC077 .E] [扫描线] [乱搞]guruguru
- MySQL基础(4)——子查询(嵌套查询)、联结表、组合查询
- 欢迎使用CSDN-markdown编辑器
- Hbase常用参数
- Genymotion启动异常 “Unable to load VirtualBox engine” 解决
- Python探索记(06)——while
- TensorFlow-Slim图像分类库
- Python练习10----习题1
- Hive初探