题目:

Problem D. Great Again
Input file: standard input
Output file: standard output
Time limit: 2 seconds
Memory limit: 512 megabytes
The election in Berland is coming. The party United Berland is going to use its influence to win them
again. The crucial condition for the party is to win the election in the capital to show the world that the
protests of opposition in it are inspired by external enemies.
The capital of Berland consists of only one long road with n people living alongside it. United Berland
has a lot of informers, so they know for each citizen whether he is going to attend the election, and if yes,
who is he going to vote for: the ruling party or the opposition.
United Berland has a vast soft power, so they can lobby the desired distribution of districts. Every district
should be a consecutive segment of the road of length between l and r inclusive. Each citizen must be
assigned to exactly one district. The votes are counted in each district separately, and the parties receive
one point for each district, where it receives strictly more votes than the other party. If the parties got
equal result in this district, no one gets its vote. United Berland is going to create the distribution that
maximizes the difference of its points and points of the opposition, and you are asked to compute this
value.
Input
The first line of the input contains three positive integers n, l, r (1 ≤ n ≤ 300 000, 1 ≤ l ≤ r ≤ n) — the
number of citizens in the capital, the lower and the upper bounds on the possible length of a district.
The second line contains n integers a1; a2; : : : ; an (ai 2 f-1; 0; 1g), denoting the votes of the citizens. 1
means vote for the ruling party, -1 means vote for opposition, 0 means that this citizen is not going to
come to the elections.
Output
If there is no way to divide the road into districts of lengths between l and r, print “Impossible” (without
quotes).
Otherwise, print one integer — the maximum possible difference between the scores of United Berland
and the opposition in a valid distribution of citizens among voting districts.
Examples

standard input standard output
5 1 5
1 -1 0 -1 1
1
5 2 3
-1 1 -1 1 -1
-1
6 1 1
1 -1 -1 -1 -1 -1
-4
5 3 3
1 1 1 1 1
Impossible

Note
In the first sample, the optimal division of districts is f1g; f2; 3; 4g; f5g.
In the second sample, the optimal division is f1; 2g; f3; 4; 5g.
In the third sample, there is only one possible division.
There is no way to divide 5 in segments of length 3, so in the fourth sample the answer is “Impossible”.

思路:

  DP:dp[i]=max{dp[j]+f[j+1][i]},(i-l+1<=j<=l-r+1)

  现在难点是怎么做到快速转移。(f[j+1][i]表示区间[j+1,i]的贡献)

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=3e5+;
const int mod=1e9+; int n,tl,tr,py,sum[K],dp[K],v[K*];
priority_queue<PII>q[K*];
int update(int o,int l,int r,int pos,int x)
{
if(l==r) return v[o]=x;
int mid=l+r>>;
if(pos<=mid) update(o<<,l,mid,pos,x);
else update(o<<|,mid+,r,pos,x);
v[o]=max(v[o<<],v[o<<|]);
}
int query(int o,int l,int r,int nl,int nr)
{
if(l==nl&&r==nr) return v[o];
int mid=l+r>>;
if(nr<=mid) return query(o<<,l,mid,nl,nr);
else if(nl>mid) return query(o<<|,mid+,r,mid+,nr);
return max(query(o<<,l,mid,nl,mid),query(o<<|,mid+,r,mid+,nr));
}
void add(int x)
{
if(x<) return ;
int fx=sum[x]+n+;
if(q[fx].size()==||q[fx].top().first<dp[x])
update(,,*n+,fx,dp[x]);
q[fx].push(MP(dp[x],x));
}
void del(int x)
{
if(x<) return;
int fx=sum[x]+n+;
while(q[fx].size()&&q[fx].top().second<=x) q[fx].pop();
if(q[fx].size()==)
update(,,*n+,fx,-mod);
else
update(,,*n+,fx,q[fx].top().first);
}
int main(void)
{
scanf("%d%d%d",&n,&tl,&tr);
for(int i=,mx=n*+;i<=mx;i++) v[i]=-mod;
for(int i=,x;i<=n;i++) scanf("%d",&x),sum[i]=sum[i-]+x;
for(int i=;i<=n;i++)
{
del(i-tr-);add(i-tl);
int q1=query(,,*n+,,sum[i]-+n+);
int q2=query(,,*n+,sum[i]+n+,sum[i]+n+);
int q3=query(,,*n+,sum[i]++n+,n+n+);
if(q1==q2&&q2==q3&&q1==-mod)
dp[i]=-mod;
else
dp[i]=max(max(q1+,q2),q3-);
}
if(dp[n]==-mod) printf("Impossible\n");
else printf("%d",dp[n]);
return ;
}

XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again的更多相关文章

  1. XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem K. Piecemaking

    题目:Problem K. PiecemakingInput file: standard inputOutput file: standard outputTime limit: 1 secondM ...

  2. 【分块】【暴力】XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem I. Rage Minimum Query

    1000w的数组,一开始都是2^31-1,然后经过5*10^7次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小 ...

  3. XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)

    A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...

  4. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of SPb

    A. Base $i - 1$ Notation 两个性质: $2=1100$ $122=0$ 利用这两条性质实现高精度加法即可. 时间复杂度$O(n)$. #include<stdio.h&g ...

  5. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia

    1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...

  6. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof

    A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...

  7. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki

    A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...

  8. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea

    A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...

  9. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov

    A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...

随机推荐

  1. Linq系列(5)——表达式树之案例应用

    在进入今天的正题之前,先感慨下本人的blog的人气一篇不如一篇.再加上换公司后人身自由受到了比之前大得多得多的限制,实在令本人有些郁闷.不过每次提笔写些东西跟大家分享,总是能让我感到愉悦和欣慰,希望我 ...

  2. centos7上面安装MySQL

    date:2018-04-03  14:07:54 本文摘自网上,经本人整理后如下:原作者及出处为: [日期:2016-09-18] 来源:Linux社区  作者:xyang81 1.配置YUM源 下 ...

  3. PHP HTTP协议(报头/状态码/缓存)

    一.HTTP协议介绍 1. #HTTP协议       # (1 建立在TCP/IP协议基础上       # (2 web开发数据传输依赖于http协议       # (3 http 协议全称是文 ...

  4. Oracle中与日期时间有关的运算函数

    1            ADD_MONTHS 格式:ADD_MONTHS(D,N) 说明:返回日期时间D加N月后对应的日期时间.N为正时则表示D之后:N为负时则表示为D之前:N为小数则会自动先删除小 ...

  5. json写入到excel表

    1. 拼接返回的json数据 // 拼接需要下载报表的HTML,并返回html;reportHtml(reporttData) { let html = `<html xmlns:o=" ...

  6. [Jenkins] 解决 Gradle 编译包含 SVG Drawable 出现异常

    异常信息 java.awt.AWTError: Can't connect to X11 window server using 'localhost:10.0' as the value of th ...

  7. HUD2647 Reward_反向建图拓扑排序

    HDU2647 Reward 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意:老板要发奖金了,有n个人,给你m对数,类似a b,这样的一对 ...

  8. 修改Android模拟器的system分区,以及加入SuperSU

      http://www.claudxiao.net/2013/10/persistent-change-emulator-system-partition/ 对Android的模拟器,如果要修改其s ...

  9. Net Core MVC6 RC2 启动过程分析

    入口程序 如果做过Web之外开发的人,应该记得这个是标准的Console或者Winform的入口.为什么会这样呢?.NET Web Development and Tools Blog ASP.NET ...

  10. 把www.domain.com均衡到本机不同的端口 反向代理 隐藏端口 Nginx做非80端口转发 搭建nginx反向代理用做内网域名转发 location 规则

    负载均衡-Nginx中文文档 http://www.nginx.cn/doc/example/loadbanlance.html 负载均衡 一个简单的负载均衡的示例,把www.domain.com均衡 ...