Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2
题意
一个大小为n的数组a[],问最多有多少个不相交的区间和相等
题解
- 离散化用值来做,贪心选择较前的区间
代码
#include<bits/stdc++.h>
#define M 5000005
#define ll long long
#define pb push_back
using namespace std;
struct N{int l,r;N(int l=0,int r=0):l(l),r(r){}};
int n,i,j,p,ans,cnt,u,r,sz=0;
ll a[2005],b[M];
vector<N>g[M];
int main(){
cin>>n;
for(i=1;i<=n;i++){
scanf("%lld",&a[i]);a[i]+=a[i-1];
for(j=i;j>=1;j--){
b[sz++]=a[i]-a[j-1];
}
}
sort(b,b+sz);sz=unique(b,b+sz)-b;
for(i=1;i<=n;i++){
for(j=1;j<=i;j++){
p=lower_bound(b,b+sz,a[i]-a[j-1])-b;
g[p].pb({j,i});
}
}
ans=1;
for(i=0;i<sz;i++){
r=g[i][0].r;cnt=1;
for(j=1;j<g[i].size();j++){
if(g[i][j].l>r){
cnt++;r=g[i][j].r;
}
}
if(cnt>=ans){
ans=cnt;u=i;
}
}
printf("%d\n%d %d\n",ans,g[u][0].l,g[u][0].r);
r=g[u][0].r;
for(i=1;i<g[u].size();i++){
if(g[u][i].l>r){
printf("%d %d\n",g[u][i].l,g[u][i].r);
r=g[u][i].r;
}
}
}
Codeforces Round #547 (Div. 3) F 贪心 + 离散化的更多相关文章
- Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理
https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop (前缀和预处理+贪心+dp)
题目:http://codeforces.com/contest/1154/problem/F 题意:给你n个商品,然后还有m个特价活动,你买满x件就把你当前的x件中最便宜的y件价格免费,问你买k件花 ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
随机推荐
- Vue的路由实现:hash模式 和 history模式
hash模式:在浏览器中符号“#”,#以及#后面的字符称之为hash,用 window.location.hash 读取.特点:hash虽然在URL中,但不被包括在HTTP请求中:用来指导浏览器动作, ...
- TensorFlow NormLization
local_response_normalization local_response_normalization出现在论文”ImageNet Classification with deep Con ...
- python大法好—模块 续
1.sys模块 sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传递参数. sys.exit([arg]): 程序中间的退出,arg=0为正常退出. sys.getdefaulten ...
- Sql入门学习——基本语法函数
--------使用工具navicat 快捷键操作 --------基本操作DML &DDL --------运算比较 --------数据类型 --------常用函数 --------常用 ...
- Java入门到精通第4版汇总
- ArcGIS自定义工具箱-清空工作空间
ArcGIS自定义工具箱-清空工作空间 联系方式:谢老师,135-4855-4328,xiexiaokui#qq.com 目的:删除工作空间里的要素类,栅格和独立表 使用方法: 例如"C:\ ...
- VC6的工程转到VC2010或更高版本出现fatal error C1189编译错误的解决方法
以前也遇到过,当时解决了没写下来,这次正好又遇到了,就顺手写一下吧,别下次又忘记了. 当VC6的工程转到VC2010或更高版本时编译出现如下错误: c:\program files\microsoft ...
- js中json的使用
- NYOJ252-01串-(数位dp)
252-01串 内存限制:64MB 时间限制:1000ms 特判: No通过数:33 提交数:49 难度:2 题目描述: ACM的zyc在研究01串,他知道某一01串的长度,但他想知道不含有“11”子 ...
- html2canvas
最近公司有个需求,实现html 页面元素转为png图像,这边用了html2canvas来实现.,这里记录一下,避免以后忘了~~ 官网链接: http://html2canvas.hertzen.com ...