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的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...
随机推荐
- linux安装phpstorm
1.下载phpStorm安装包,下载地址:https://download.jetbrains.8686c.com/webide/PhpStorm-2018.3.1.tar.gz 2.解压到/usr/ ...
- 3-安装hive
1.解压.修改权限 tar -zvxf apache-hive-1.2.1-bin.tar.gz -C /opt/app/ sudo chown -R hadoop:hadoop /opt/app/a ...
- cat & 文件结束符
语法: 连接显示 选项: -n,显示行号. -v,显示不可见打印符. -E,显示“行结束符”($). 显示行号 $ cat -n /etc/fstab /dev/VolGroup00/LogVol00 ...
- 安装Anaconda3进行python版本管理
1.下载Anaconda3,我选择了python3的64位版本 2.windows安装,选择加入了系统目录 3.进入命令行进行版本安装 // 安装一个指定版本conda create --name p ...
- 关系型数据库和NoSQL数据库
一.数据库排名和流行趋势 1.1 Complete ranking 链接: https://db-engines.com/en/ranking 在这个网站列出了所有数据库的排名,还可以看到所属数据库类 ...
- GPUImage中亮度调整的实现——GPUImageBrightnessFilter
亮度brightness其实是对RGB的调整,RGB值越大,效果越亮:反之则越暗. GPUImage中提供了对图像亮度调整的Filter,其核心代码如下(fragment): varying high ...
- 混合式应用真机调试(Android Studio + Chrome)
如何在chrome上调试混合式APP https://developers.google.com/web/tools/chrome-devtools/remote-debugging/ Get Sta ...
- js求时间差,两个日期月份差
var date1=new Date(); //开始时间 alert("aa"); var date2=new Date(); //结束时间 var date3=date2 ...
- 认识JavaWeb,servlet, JSP, Tomcat, http协议,Web服务器
JavaWeb通常指服务器端的Java应用开发. 一般来说,服务器是在网络通信条件下工作的,这就离不开http协议. HTTP协议,是为服务器和客户端通信提供的规范,其中规定了信息的格式,符合规范格式 ...
- 回溯法 leetcode题解 Combination Sum 递归法
题目大意:给出一个数组,用这些数组里的元素去凑一个target.元素可以重复取用. 感觉对这种题目还是生疏的.脑子里有想法,但是不知道怎么表达出来. 先记录下自己的递归法.应该还可以用循环实现. 回溯 ...