CodeForces-1132C-Painting the Fence-(前缀和)
You have a long fence which consists of nn sections. Unfortunately, it is not painted, so you decided to hire qq painters to paint it. ii-th painter will paint all sections xx such that li≤x≤rili≤x≤ri.
Unfortunately, you are on a tight budget, so you may hire only q−2q−2 painters. Obviously, only painters you hire will do their work.
You want to maximize the number of painted sections if you choose q−2q−2 painters optimally. A section is considered painted if at least one painter paints it.
Input
The first line contains two integers nn and qq (3≤n,q≤50003≤n,q≤5000) — the number of sections and the number of painters availible for hire, respectively.
Then qq lines follow, each describing one of the painters: ii-th line contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).
Output
Print one integer — maximum number of painted sections if you hire q−2q−2painters.
Examples
7 5
1 4
4 5
5 6
6 7
3 5
7
4 3
1 1
2 2
3 4
2
4 4
1 1
2 2
2 3
3 4
3
解题过程:
遍历一下每个栅栏有多少人会在这里刷漆,对于在漆工范围内的栅栏存储总量。用前缀和数组记录刷一次和刷两次的位置。
暴力寻找哪两个漆工去掉后会让栅栏不能刷漆,取最小值。用总量-最小值。大牛只需要分三种情况,在下天资愚钝,分了6种情况。
第一种:
.........................
.........................
第二种:
.......................
..............................
第三种:
.........................
..............
第四种:
.........................
..................
第五种:
.........................
...........................
第六种:
.............
............
上代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long struct node
{
int l;
int r;
int idx;
};
int x,y;
node a[];
int t[];
int one[];
int two[];///前缀和数组 bool cmp(node p1,node p2)
{
if(p1.l==p2.l)
return p1.r<p2.r;
else return p1.l<p2.l;
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,,sizeof(a));
memset(t,,sizeof(t));
for(int i=;i<m;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
for(int j=a[i].l;j<=a[i].r;j++)
t[j]++;
}
int ans=,minn=inf;
for(int i=;i<=n;i++)
{
if(t[i])
{
ans++;
}
one[i]=one[i-]+(t[i]==);///前缀和赋值不能放括号里面
two[i]=two[i-]+(t[i]==);///前缀和需要连续赋值,如果if不成功则不赋值,造成中断
}
sort(a,a+m,cmp);
for(int i=;i<m;i++)///hello
{
for(int j=i+;j<m;j++)///下标j在i后面,j的l始终大于等于i的l
{
if( a[i].l==a[j].l && a[i].r==a[j].r)///
minn=min(minn,( two[ a[i].r ]-two[ a[i].l- ] ) );
else if(a[i].l==a[j].l && a[i].r<a[j].r)///
minn=min(minn,( two[ a[i].r ]-two[ a[i].l- ] ) + ( one[ a[j].r ]-one[ a[i].r ] ) );
else if( a[i].r>a[j].r )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[j].r ]-two[ a[j].l- ] ) + ( one[ a[i].r ]-one[ a[j].r ]) );
else if( a[i].r==a[j].r )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[j].r ]-two[ a[j].l- ] ) );
else if( a[i].r<a[j].r && a[i].r>=a[j].l )///
minn=min(minn,( one[ a[j].l- ]-one[ a[i].l- ] ) + ( two[ a[i].r ]-two[ a[j].l- ] ) + ( one[ a[j].r ]-one[ a[i].r ]) );
else if( a[i].r<a[j].l )///
minn=min(minn,( one[ a[i].r ]-one[ a[i].l- ] ) + ( one[ a[j].r ]-one[ a[j].l- ] ) );
}
}
printf("%d\n",ans-minn); }
return ;
}
CodeForces-1132C-Painting the Fence-(前缀和)的更多相关文章
- Codeforces 1132C - Painting the Fence - [前缀和优化]
题目链接:https://codeforces.com/contest/1132/problem/C 题意: 栅栏有 $n$ 个节,有 $q$ 个人可以雇佣来涂栅栏,第 $i$ 个人可以涂第 $l_i ...
- Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化
题意就是给出多个区间,要求去掉两个区间,使得剩下的区间覆盖范围最大. 当然比赛的时候还是没能做出来,不得不佩服大佬的各种姿势. 当时我想的是用线段树维护区间和,然后用单点判0,维护区间间断个数.然后打 ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
[luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...
- 洛谷 画栅栏Painting the Fence 解题报告
P2205 画栅栏Painting the Fence 题目描述 \(Farmer\) \(John\) 想出了一个给牛棚旁的长围墙涂色的好方法.(为了简单起见,我们把围墙看做一维的数轴,每一个单位长 ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- 【Codeforces 1132C】Painting the Fence
Codeforces 1132 C 题意:给一些区间\([l_i,r_i]\),从中删掉两个,求剩下的区间最多能够覆盖的格子数量. 思路:首先枚举第一个删掉的区间,然后我们可以通过差分来求出每个格子被 ...
- [Codeforces 448C]Painting Fence
Description Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Champion ...
- codeforces C. Painting Fence
http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...
随机推荐
- raid1与raid5
raid 1 就是两个磁盘同时读同时写, 当其中一个坏了 不影响使用, 直接更换一个,这样磁盘的容量只有一个盘的raid 5 就是 N-1个磁盘的容量,当其中任何一个磁盘坏,不影响使用,更换一个就可以 ...
- SDOI2018IIIDX
/* 题目转换为 n个节点的一片森林,n个权值,要给每个节点分配一个权值,保证子节点的权值不小于父节点的权值,并且1~n的权值的字典序最大. 考场上的贪心很显然 建立出 树来 将所有数值从大到小排序 ...
- phalcon断点调试(phpStorm+xdebug)
1.下载并添加chrome插件xdebug helper,下载地址:http://www.downcc.com/soft/261091.html 2.php添加xdebug扩展 mkdir -p /u ...
- mac maven lombok报错
maven已导入lombok的jar包,注解@Data,但是用到getter,setter时依然出错.解决办法: 打开eclipse.ini文件,加上如下两句: -Xbootclaspath//Use ...
- rem 布局的闪现问题
<script type="text/javascript"> var sizeRate = document.documentElement.clientWidth/ ...
- GNU coreutils
内核实用程序,针对文本及文件操作.涉及到102条linux命令.命令列表:cp.install.ln.mv.ls.echo…… 常见选项 1.退出状态 2.备份选项 cp, install, ln, ...
- Docker的概述
什么是Docker 1.容器技术 在计算机的世界中,容器拥有一段漫长且传奇的历史.容器与管理程序虚拟化(hypervisor virtualization,HV)有所不同,管理程序虚拟化通过中间层将一 ...
- iOS工程结构理解
iOS开发中关于工程结构有三个关键部分,分别是:Targets,projects 和 workspaces. Targets指定了工程或者库文件如何编译,包括building setting,comp ...
- maven依赖出现问题:failed to collect dependencies
问题:在maven项目中,install dao层之后,在interface调用显示failed to collect dependencies: 解决办法: 1.检查依赖信息是否正确,不确定删除后重 ...
- Python基础5 常用模块学习
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...