XVII Open Cup named after E.V. Pankratiev Grand Prix of Moscow Workshops, Sunday, April 23, 2017 Problem D. Great Again
题目:
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的更多相关文章
- 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 ...
- 【分块】【暴力】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次随机位置的随机修改,问你每次的全局最小值. 有效的随机修改的期望次数很少,只有当修改到的位置恰好是当前最小值的位置时才需要扫一下更新最小 ...
- XVII Open Cup named after E.V. Pankratiev. Grand Prix of America (NAIPC-2017)
A. Pieces of Parentheses 将括号串排序,先处理会使左括号数增加的串,这里面先处理减少的值少的串:再处理会使左括号数减少的串,这里面先处理差值较大的串.确定顺序之后就可以DP了. ...
- 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 ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Siberia
1. GUI 按题意判断即可. #include<stdio.h> #include<iostream> #include<string.h> #include&l ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Peterhof
A. City Wall 找规律. #include<stdio.h> #include<iostream> #include<string.h> #include ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Khamovniki
A. Ability Draft 记忆化搜索. #include<stdio.h> #include<iostream> #include<string.h> #i ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea
A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- 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$处区间求和即可. 树 ...
随机推荐
- 【mysql】windows7 安装 Mysql
From: http://jingyan.baidu.com/article/e52e3615a1128c40c70c5174.html 安装(解压) ZIP Archive版是免安装的.只要解压就行 ...
- powershell---高级函数的介绍
https://guhuajun.wordpress.com/2009/05/11/windows-powershell-v2-介绍(5)-高级函数(上)/ https://guhuajun.word ...
- PowerShell如何使用自定义公共函数
http://blog.csdn.net/flyliuweisky547/article/details/18565705
- AWS系列-EC2实例选择镜像
Centos Ubuntu Redhat 打开EC2控制台,点击启动实例,选择AWS Marketplace Centos.org说明为centos官网镜像 如下图,这种镜像是收费的镜像 Ubuntu ...
- linux C之alarm函数 转
原文出处:http://blog.sina.com.cn/s/blog_6a1837e90100uhl3.html alarm也称为闹钟函数,alarm()用来设置信号SIGALRM在经过参数seco ...
- Dubbo+Zookeeper视频教程
http://www.roncoo.com/course/view/f614343765bc4aac8597c6d8b38f06fd#boxTwo
- Django学习笔记第十篇--实战练习六--发送邮件
一.发送邮件需要引入的包依赖文件(Django1.8 Python2.7) from django.core.mail import send_mail,send_mass_mail 其中send_m ...
- WCF入门(十)——服务对象模型
当发生一次WCF请求-响应操作时,会经过如下几个步骤 WCF Client想WCF Server发送一个服务请求 WCF Server创建WCF服务对象 WCF Server调用WCF服务对象接口,将 ...
- 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于SQL)
1:创建一个基于身份认证的应用程序(具体参见上篇基于AD) SQL-MembershipProvider 成员SQL-RoleManager 角色 2:修改管理中心,我们创建的应用程序,还有Web服务 ...
- msvcp71.dll 怎么丢失的?如何修复
解决方法:另一台电脑上下载这个dll,再用优盘拷回来,复制到c:\windows\system32\下. 个人遇到的情况:迅雷下载东西时,或者在操作迅雷时出现的. win7 64位下 点击下载