题目:

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. Python爬虫(六)

    源码: import requests import re from my_mysql import MysqlConnect # 获取问答信息 def get_contents(page,heade ...

  2. Python+selenium之获取文本值和下拉框选择数据

    Python+selenium之获取文本值和下拉框选择数据 一.结合实例进行描述 1. 实例如下所示: #新增标签操作 def func_labels(self): self.driver.find_ ...

  3. WPF Expander控件(扩展面板)

    这算是我比较喜欢的一个控件,以前在Winform中也常用类似的.它包装了一块内容,通过单击一个小箭头按钮可以显示或隐藏所包含的内容.在线帮助以及Web页面经常使用这种技术,因为既可以包含大量内容,而又 ...

  4. 如何在AWS中为自己的S3托管站点添加SSL/TSL证书(https)

    概要 利用AWS的S3服务托管静态网站后,如何将自己的域名与该站点绑定,并为此域名提供SSL/TSL证书(https). 面向人群 已经掌握如何利用S3服务托管静态网站. 已经拥有自己的域名. 希望为 ...

  5. 《C++ Primer Plus》学习笔记 第1章 预备知识

    第一章 预备知识C++在C语言的基础上添加了对"面向对象编程"的支持和对"泛型编程"的支持.类 —— 面向对象模板 —— 泛型编程1.1 C++简介1.2 C+ ...

  6. 在 Ubuntu Mate 16.04 上通过 PPA 升级 Mate 1.14

    导读 Mate 桌面环境 1.14 现在可以在 Ubuntu Mate 16.04 ("Xenial Xerus") 上使用了.根据这个版本的描述,为了全面测试 Mate 1.14 ...

  7. NIO中几个非常重要的技术点

    参考:http://ifeve.com/selectors/ 参考:https://www.ibm.com/developerworks/cn/education/java/j-nio/j-nio.h ...

  8. Java IO 修改文件名

    /** *//**文件重命名 * @param path 文件目录 * @param oldname 原来的文件名 * @param newname 新文件名 */ public void renam ...

  9. Servlet------>servletDemo 及细节注意

    原理图: 前提:我用的命令行都是mac系统下用的,非win jsp实质是一个servlet,所以要先了解servlet,如上页面是一个servletdemo,下面是尝试的步骤 1.先写好Demo.ja ...

  10. shell (check return of each line)(PIPESTATUS[@])and sudoer

    shell result from cmdline echo $? if 0 then success ;else failure (shell 执行每部返回值,rm -rf 错误,打包不能覆盖) 我 ...