CF1012F Passports
http://codeforces.com/problemset/problem/1012/F
题解
考虑\(p=1\)的情况。
我们可以把题意理解成平面上有一些线段,你需要给每条线段分配一个长度给定的预备线段,使得预备线段要在它所属的线段前面,并且所有线段不能相交。
因为我们只需要弄出一组解来,所以我们可以直接设\(dp[s]\)表示\(s\)集合内的所有旅行都办好了签证,最后拿到护照的最小时间。
于是我们发现\(p=2\)时我们只需要把它们分成两组,每组都满足条件就行了,并且需要满足每次办理签证的当前不能再某次旅行途中。
所以对于\(p=2\)的情况,我们还是按照上面去\(dp\),最后枚举划分就好了。
然后考虑怎么\(dp\)。
我们可以直接枚举一个状态,然后枚举下一次办理那个国家的签证,然后判断一下就行。
如何判断新加入一个国家时需要的时间,我们设上一次的时间为\(last\)。
我们可以把所有初始,的线段排序,然后我们从前往后扫描每个线段,当当前时间与这个线段冲突时就把时间往后推。
然后发现这个是有单调性的,于是可以把所有要转移的按照签证时间排序,双指针即可。
代码
#include<bits/stdc++.h>
#define N 25
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int n,p;
int bit[1<<23];
int ansl[N];
ll ansr[N];
int tot,b[N],tot1,c[N];
ll dp[1<<23],tag[1<<23];
inline ll rd(){
ll x=0;char c=getchar();bool f=0;
while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f?-x:x;
}
struct node{
int l,r,t,id;
inline bool operator <(const node &b)const{
return l<b.l;
}
}a[N];
inline bool cmp(int x,int y){
return a[x].t<a[y].t;
}
void dfs(int s,int tg){
if(!s)return;
int x=tag[s];
ansr[a[x].id]=dp[s]-a[x].t;
ansl[a[x].id]=tg;
dfs(s-(1<<x-1),tg);
}
int main(){
n=rd();p=rd();
for(int i=1;i<=n;++i){
a[i].l=rd();
int x=rd();
a[i].r=a[i].l+x-1;
a[i].t=rd();
a[i].id=i;
}
sort(a+1,a+n+1);
memset(dp,0x3f,sizeof(dp));
for(int i=1;i<=n;++i)bit[1<<i-1]=i;
int maxn=(1<<n);
dp[0]=1;
for(int i=0;i<maxn;++i)if(dp[i]<1e15){
tot=tot1=0;
for(int j=1;j<=n;++j)if(!(i&(1<<j-1)))b[++tot]=j;else c[++tot1]=j;
sort(b+1,b+tot+1,cmp);
int p1=1,p2=1;
ll last=dp[i];
for(int j=1;j<=tot;++j){
int now=b[j];
while(1){
bool tg=0;
if(last>=a[p1].l&&p1<=n){
last=max(last,(ll)a[p1].r+1);
p1++;tg=1;
}
while(last>a[c[p2]].l&&p2<tot1)p2++;
if(last<=a[c[p2]].l&&p2<=tot1){
int xx=c[p2];
if(a[xx].l<=last+a[now].t)last=max(last,(ll)a[xx].r+1),p2++,tg=1;
}
if(!tg)break;
}
int s=i^(1<<now-1);
if(last+a[now].t<a[now].l&&last+a[now].t<dp[s])dp[s]=last+a[now].t,tag[s]=now;
}
}
if(p==1){
if(dp[maxn-1]<1e15){
dfs(maxn-1,1);
puts("YES");
for(int j=1;j<=n;++j)cout<<ansl[j]<<" "<<ansr[j]<<"\n";
}
else puts("NO");
}
else{
for(int i=0;i<maxn;++i)if(dp[i]<1e15&&dp[maxn-1-i]<1e15){
dfs(i,1);dfs(maxn-1-i,2);
puts("YES");
for(int j=1;j<=n;++j)cout<<ansl[j]<<" "<<ansr[j]<<"\n";
return 0;
}
puts("NO");
}
return 0;
}
CF1012F Passports的更多相关文章
- 新概念英语(1-15)Your passports please
Is there a problem wtih the Customers officer? A:Are you Swedish? B:No. We are not. We are Danish. A ...
- Windows Server 2008 R2常规安全设置及基本安全策略
这篇文章主要介绍了Windows Web Server 2008 R2服务器简单安全设置,需要的朋友可以参考下 用的腾讯云最早选购的时候悲催的只有Windows Server 2008 R2的系统,原 ...
- json 转化
1. 把java 对象列表转换为json对象数组,并转为字符串 复制代码代码如下: JSONArray array = JSONArray.fromObject(userlist); S ...
- RFID 仿真/模拟/监控/拦截/检测/嗅探器
Sound card based RFID sniffer/emulator (Too tired after recon.cx to do draw the schematics better th ...
- RFID 基础/分类/编码/调制/传输
不同频段的RFID产品会有不同的特性,本文详细介绍了无源的感应器在不同工作频率产品的特性以及主要的应用. 目前定义RFID产品的工作频率有低频.高频和甚高频的频率范围内的符合不同标准的不同的产品,而且 ...
- json串转对象
// 引入相应的包 //json-lib-2.2-jdk15.jar import net.sf.json.JSONArray;import net.sf.json.JSONObject; 1. // ...
- JSON字符串与java对象的转换
所需的jar包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar ...
- JSON 字符串 与 java 对象的转换
jsonLib 经典文章:http://json-lib.sourceforge.net/xref-test/net/sf/json/TestJSONObject.html // 引入相应的包 //j ...
- 英语阅读积累<一>
Passage 1.Woodpecker 惹treat or trick There are many apple trees in a garden. They’re good friend ...
随机推荐
- js实现完整轮播
1.封装一个简单的动画函数 function animate(obj,target,callback){ clearInterval(obj.timer);//清除定时器防止定时器重复添加 obj.t ...
- django基础篇06-ModelForm操作及验证
本文内容主要来自银角大王的博客 学习大纲: 一.ModelForm 二.Ajax - 原生(jQuery) - 伪Ajax操作 三.文件上传(预览) - Form提交 - Ajax文件上传 四. 图片 ...
- FPGA引脚锁定 注意err和高阻状态
1.fpga没有用的的管脚一定要设置成高阻状态设置路径如下: Assignmen->Device->Device&Pin Option->Unused pins->As ...
- 06java进阶——集合框架(list和泛型)
1.ArrayList ArrayList集合是程序中最常见的一种集合,它属于引用数据类型(类).在ArrayList内部封装了一个长度可变的数组,当存入的元素超过数组长度时,ArrayList会在内 ...
- 树状数组求LIS模板
如果数组元素较大,需要离散化. #include <iostream> #include <cstdio> #include <cstring> #include ...
- Codeforces 938 正方形方格最多0/1 足球赛dijkstra建图
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- seleniummaster
http://seleniummaster.com/sitecontent/index.php/component/banners/click/6 Step 1: create a Java proj ...
- MySQL导出和导入
MySQL的几句脚本 最近做了几次mysql的备份和恢复, 找了一些资料, 写了一些脚本, 记录一下. #导出 mysqldump $_login_info_ $_src_db_name_ --no- ...
- 去掉html中的标签
//去掉html中的图片 String regEx_image = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)"; Pattern p_s ...
- unittest----skip(跳过用例)
我们在执行测试用例时,有时有些用例时不需要执行的,这时就需要用到unittest给我们提供的跳过用例的方法 @unittest.skip(reason):强制跳过,不需要判断条件.reason是跳过原 ...