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的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...
随机推荐
- nvm-windows安装
linux上的nvm太好用了,windows也出了,不过需要手动下载安装 地址: https://github.com/coreybutler/nvm-windows/releases 博主安装的是 ...
- github提交代码后没有contribution问题
好气啊,好几天的代码提交后发现没有contribution,强迫症表示不能忍,仔细排查了下原因 可能有以下几个原因 1.提交代码的用户名和邮箱和账号不符合 方法1:git命令行设置用户名和邮箱 git ...
- Access 分页
access分页 pageSize 每页显示多少条数据 pageNumber 页数 从客户端传来 pages) SQL语句 select top pageSize * from 表名 where id ...
- 三种常见的Web安全问题
XSS漏洞 1.XSS简介 跨站脚本(cross site script)简称为XSS,是一种经常出现在web应用中的计算机安全漏洞,也是web中最主流的攻击方式. XSS是指恶意攻击者利用网站没有对 ...
- 【Jmeter自学】Jmeter脚本录制(二)
==================================================================================================== ...
- 操作笔记:tomcat在正式环境的常见问题和idea的远程调试
1,一台服务器有两个容器,比如:jetty,tomcat. 出现问题: jetty启动的时候,tomcat就不能启动了. 此时,需要修改tomcat的配置文件:servler.xml <?xml ...
- Java file方法的路径特性
1.在flle方法里,直接写空白的路径,是会默认获取当前Java编译工作空间的路径. 例子如下: package example_1; import java.io.File; import java ...
- swt text 回车 defaultSelected
今天试了一下SWT控件 TEXT 中的回车事件,使用 defaultSelected 进行处理,结果怎么也不能触发事件. 经过仔细排查,发现是TEXT选中了 wrap 的原因,毕竟如果是多行的话,肯定 ...
- uva-10714-贪心
题意:有一条杆,长度为L,上面很几只蚂蚁,蚂蚁的朝向未知,爬速1cm/s,在爬行过程中,蚂蚁相撞了就往反方向爬,问,杆上没有蚂蚁至少要多久,至多要多久 解题思路: 蚂蚁1和蚂蚁2相撞,我们只要交换一下 ...
- SAPCertifiedTechnologyAssociate-SystemAdministration(SAPHANAasaDatabase)
C_TADM55_75 SAP Certified Technology Associate - System Administration (SAP HANA as a Database) with ...