在多个连续的区间段中,选出连续重复度最高的区间,这样连续选出多个重复度最高的不相交区间,然后从第一个区间的左边已经右边开始,连续贪心即可,答案取最小值

#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<vector>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define LL long long
using namespace std;
vector<pii>v;
const int maxx = ;
int a[maxx];
int b[maxx];
int n;
LL go(int x){
LL ans=;
for(int i=;i<v.size();i++){
if (v[i].first>x){
ans+=(v[i].first-x+)/;
if (i<v.size()-){
if ((v[i].first-x)%){ ///长度为奇数
if (v[i+].first>v[i].second && v[i].second-v[i].first>){
x=v[i].first+;
}else x=v[i].first;
}else x=v[i].first;
}else x=v[i].first;
}else {
ans+=(x-v[i].second+)/;
if (i<v.size()-){
if ((x-v[i].second)%){
if(v[i+].second<v[i].first && v[i].second-v[i].first>){
x=v[i].second-;
}else x=v[i].second;
}else x=v[i].second;
}else x=v[i].second;
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
v.clear();
scanf("%d",&n);
scanf("%d%d",&a[],&b[]);
int l=a[],r=b[];
for (int i=;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
if (a[i]<=r && b[i]>=l){
l=max(l,a[i]);
r=min(r,b[i]);
}else {
v.pb(mp(l,r));
l=a[i];
r=b[i];
}
}
v.pb(mp(l,r));
LL ans=2e18;
ans=min(ans,go(v[].first));
ans=min(ans,go(v[].second));
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 重温Http协议--请求报文和响应报文
  2. 简单说下COALESCE这个日常使用的函数
  3. react+redux官方实例TODO从最简单的入门(6)-- 完结
  4. mysql 字符串 日期互转
  5. log4net 自定义Layout日志字段
  6. Storm 单机版环境搭建
  7. JQUERY MOBILE 中文API站 和 官方论坛
  8. 【JDK源码分析】String的存储区与不可变性
  9. Android中Bitmap和Drawable
  10. mongoose学习文档
  11. java适配器模式随笔记
  12. HDU4349--Xiao Ming&#39;s Hope(数论)
  13. Android 2D绘图初步
  14. HDU 3078 Network LCA
  15. 读《不要告诉我你懂margin(海玉的博客)》有感
  16. php多个文件上传
  17. .NET开发一个微信跳一跳辅助程序
  18. npm 设置和取消代理配置
  19. c#mvc实现登录
  20. leetcode — container-with-most-water

热门文章

  1. 关系数据库理论 ch.6
  2. python 变量离散化
  3. Leetcode669.Trim a Binary Search Tree修建二叉树
  4. git出现“The file will have its original line endings in your working directory”错误
  5. 运行Jmeter时,响应数据中文乱码问题解决办法
  6. SCAN listener and Node listener – How does it work
  7. docker容器时区问题
  8. 【水滴石穿】React Native 组件之SafeAreaView
  9. 从 Program Manager 看 Leader 是什么角色
  10. Pycurl介绍