题目:

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 2437(dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2437 思路:只需用一个二维数组记录到达某点时路径长度mod k的最短路径长度,如果余数相同,就更新最小 ...

  2. 编程之美 set 5 寻找数组中最大值和最小值

    解法 1. 设置 min, max 两个变量, 然后遍历一遍数组, 比较次数为 2*N 2. 依然设置 min, max 两个变量并遍历数组, 但将遍历的 step 设置为 2, 比较次数为 1.5 ...

  3. UE4中VR模式下窗口单目双目的问题

    如果要是单目的话使用HMD MIRROR MODE 3或者4  

  4. 关于Win7 x64下过TP保护(应用层)(转)

    非常感谢大家那么支持我上一篇教程.Win10 快出了,所以我打算尽快把应用层的部分说完. 调试对象:DXF调试工具:CE.OD.PCHunter.Windbg调试先言:TP的应用层保护做得比较多,包括 ...

  5. linux安装yaf(ubuntu教程)

    $PHP_BIN/phpize 这个是什么 其实这个是一个扩展 apt-get install php5-dev 这样电脑会帮你自己配置了,很简单的下面我们就下载扩展包了http://pecl.php ...

  6. oracle怎么把一个用户下的表复制给另一个用户?(授予表权限)

    //把system读写权限 授权给scottselect 'Grant all on '||table_name||' to scott;' from all_tables where owner = ...

  7. ipad4没有声音提示消息

    打开『设置』-『通用』-侧边开关用于: 1:锁定屏幕旋转 2:静音 √ 把对号去掉 选择1即可

  8. 170425、centos安装mysql5.6数据库

    # rpm -qa | grep mysql ## 查看该操作系统上是否已经安装了 mysql 数据库, 有的话,可以通过 rpm -e 命令 或者 rpm -e --nodeps 命令来卸载掉 # ...

  9. Vue.js_础学习之DOM操作

    demo说明: 1.{{message}}                           --“Mustache” 语法(双大括号) 2.v-bind:属性名                   ...

  10. C语言实现链表中结构体嵌套

    1.首先,定义两个结构体,一个用于定义链表,一个用于定义数据 // 定义数据相关的结构体 typedef struct Student{ int stu_id; ]; }Stu; // 定义链表相关的 ...