题目:

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. hdu 1140:War on Weather(计算几何,水题)

    War on Weather Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. hdu 1560(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1560 思路:关键是启发式函数h()的构造,我们可以这样想:每次给主串增加一个字符和字符串的最后一位比较 ...

  3. ADT(Android Developer Tools)中配置SVN

    1:打开adt-bundle-windows-x86\eclipse目录.新创建目录subclipse (注:adt-bundle-windows-x86 为我的eclipse目录名) 2: 打开Ec ...

  4. 设置eclipse中python脚本的编码格式

    今天在运行python脚本时报如下错误: SyntaxError: Non-ASCII character '\xe5' in file D:\pythonlearn1\src\day01\direc ...

  5. springMVC问题

    网站中springmvc.xml配置: <bean id="viewResolver" class="org.springframework.web.servlet ...

  6. node 同异步处理

    同步:序列执行,需等待 异步:非序列执行,无需等待 node同步处理:读取->输出->完毕(队列式执行) node异步处理:读取->完毕(回调输出)(后两步同时进行,谁先到谁先输出) ...

  7. Struts2中的类型转换与复杂对象配合使用

    form 标签可以被映射到一个属性的属性 manager.java package com.atguigu.struts2.model; import java.util.Date; public c ...

  8. 3、二、c# 面向对像编程。类,结构、C# 数据类型(引用类型、值 类型、指针类型)、ref参数与out参数、方法的重载、静态类型与静态成员、继承与多态、委托与事件

    一.类 定义类使用class关键字. <access specifier> class class_name { // member variables 成员变量 <access s ...

  9. AJAX Form Submit Framework 原生js post json

    https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest <!doctype ht ...

  10. intelij IDEA在启动tomcat时控制台日志乱码

    1.在idea安装目录的bin下修改idea.exe.vmoptions和idea64.exe.vmoptions,添加 -Dfile.encoding=UTF-8 -Dconsole.encodin ...