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 ...
随机推荐
- 杭州集训Day4
别问我为什么没有前三天,有时间再补~ 60+60+50=170. T1 . 坐等 memset0 ( 1s 256MB )( 原题:洛谷CF1151E Number of Components ) 树 ...
- 解决MySql忘记密码
描述:忘记了mysql的登录密码,无法登录的情况下该怎么办? 环境:CentOS 7,数据库:mysql 5.7 1.停止数据库(先查看mysql服务是否运行) # ps -ef | -i grep ...
- 用adb logcat抓取log
实时打印的主要有:logcat main,logcat radio,logcat events,tcpdump,还有高通平台的还会有QXDM日志 状态信息的有:adb shell dmesg, ...
- 题解 AT1357 【n^p mod m】
此题就是快速幂取模 先简单讲一讲快速幂 首先,快速幂的目的就是做到快速求幂,假设我们要求a^b,按照朴素算法就是把a连乘b次,这样一来时间复杂度是O(b)也即是O(n)级别,快速幂能做到O(logn) ...
- css是干什么的
css这些长篇累牍的参数,其实就是这些所谓的css编程者每天要干的事情了,他们把这些参数熟记于心,就像c++程序员,把这些函数库熟记于心一样,都是编程. css定制了每一个单独的组件,这些组件要么是相 ...
- Python list和tuple的相互转换?
list转为tuple: temp_list = [1,2,3,4,5] 将temp_list进行强制转换:tuple(temp_list) 查看是否转换成功:print type(temp_list ...
- 【NOI2007】项链工厂 ——老题新做.jpg
第一次是用 ODT 过的...(虽说跑得飞慢但它就是能过) 而且还写了发题解... 第二次是在考场上碰到了这道题,然后居然打了线段树,各种 bug 直接让代码爆零 但还是补好了代码重新交了一发,发现跑 ...
- mongodb导出导入数据
在使用mongodump导出单个表的时候,遇到了一个错误 # mongodump --host xxx --port 27017 --username 'admin' -p '123456' -d 数 ...
- vue项目1-pizza点餐系统11-设计menu页面
菜单的页面设计是基于bootstrap实现的,主要用到的是table标签,其中获取data数据用到遍历. <template> <div class=""> ...
- Hive常用非交互式命令
[hadoop@hadoop hive-0.13.1]$ bin/hive -help usage: hive -d,--define <key=value> Variable subsi ...