2016, IX Samara Regional Intercollegiate Programming Contest I. Deadline
题目看这里

Alex works as a developer, and he has hard times now. He has to complete n tasks. Every task i has the latest time di when it can be completed (starting from the moment of time 0), the time ci needed to complete it, and the tasks that must be completed before task i to get the possibility to start it. We’ll say that the i-th task depends on these tasks.

What should be the order of doing tasks to complete all of them in time?

Input
The first line contains a single integer n (1 ≤ n ≤ 200000) — the number of tasks.

Next n lines describe tasks. The i-th line starts with three space-separated integers di, ci and ri (1 ≤ di,  ci ≤ 109,  0 ≤ ri ≤ n - 1) — the latest time to complete the i-th task, the time needed to complete it and the number of tasks which it depends on. Then in the same line ri space-separated integers — the numbers of these tasks — are written. It’s guaranteed that there is no number i among these ri numbers.

The tasks are numbered from 1 in order of their appearance in the input. The sum of all ri in the input doesn’t exceed 200000.

Output
In the first line output «YES» (without quotes) if Alex will be able to complete all the tasks without exceeding any deadlines, or «NO» (without quotes) otherwise.

If the answer is «YES», in the second line output n space-separated integers — the numbers of tasks in the order they must be done to fit into all deadlines. If there are many solutions, output any of them.

算法:贪心,dfs,优先队列

题意:共有n个任务,每个任务有对应的deadline d和花费的时间 c,在做第i个任务前必须保证它的前置任务ri已经做完,前置任务可以没有。
判断是否能在ddl之前完成任务,如能输出任意一种顺序。

核心思路
先贪心将任务按照deadline从小到大排序,然后依次做任务,如果当前任务有前置任务,将前置任务排序按照ddl从小到大排序后将前置任务依次做。
这里涉及到一个递归,可能并不是最优解,使用拓扑排序效率会更高。

这里使用优先队列存储任务,dfs搜索,state[i]判断是否有环,vis[i]判断该任务是否已做,每次把now+a[i]的所花费的时间加上和a[i]的ddl比较,如果大于则no,否则将a[i].num存入vector中。

#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
int n,i,j,k,r,w,now=0;      //now:currentTime
struct node{int d,c,num;            //结束时间 所花时间 编号
}a[maxn],b[maxn];
struct cmp1{bool operator()(const node &x,const node &y)     { //重载,排序优先队列的函数return x.d>y.d;}
};
bool cmp2(node xx,node yy){return xx.d<yy.d;
}
priority_queue<node,vector<node>,cmp1> pq[maxn];//按照d值由小到大排序,队列排序取反
vector<int > v[maxn],ans;
bool vis[maxn],state[maxn];
void dfs(int p){if(vis[p]==1)                 //已被搜过return ;state[p]=1;                      //状态为1这点正激活但还未被用到 vis[p]=1;while(!pq[p].empty()){node t=pq[p].top();if(state[t.num]==1) {//如果出现已经激活的点说明有环,输出no cout<<"NO"<<endl;exit(0);}dfs(t.num);pq[p].pop();}now+=b[p].c;ans.push_back(p);               if(b[p].d<now){cout<<"NO"<<endl;exit(0);}state[p]=0;                      //回溯return ;
}
int main(){ cin>>n;for(i=1;i<=n;i++){a[i].num=i;  cin>>a[i].d>>a[i].c>>r;b[i]=a[i];                    //b[i]用来dfs,a[i]用来排序while(r--){cin>>w;v[i].push_back(w);}  }for(i=1;i<=n;i++){if(!v[i].empty()){for(k=0;k<v[i].size();k++){pq[i].push(a[v[i][k]]);            //优先队列排前置任务}}}sort(a+1,a+n+1,cmp2);                      //排序for(i=1;i<=n;i++)    dfs(a[i].num);cout<<"YES"<<endl;for(auto it:ans)  cout<<it<<" ";    return 0;
}

2016, IX Samara Regional Intercollegiate Programming Contest I. Deadline相关推荐

  1. 2019, XII Samara Regional Intercollegiate Programming Contest 解题报告

    2019 XII Samara Regional Intercollegiate Programming Contest 传送门 A. Rooms and Passages 题意: 有 n+1n+1n ...

  2. 2019, XII Samara Regional Intercollegiate Programming Contest

    A.双指针如果定义好 l 和 r 的含义就变得很好写了. #include <bits/stdc++.h> using namespace std; typedef long long L ...

  3. 2019, XII Samara Regional Intercollegiate Programming Contest 全部题解

    英语巨烂的我,把两个签到题读成了不可写题-感觉给我一个中文题面,有机会ak- A. Rooms and Passages 题意:有 n + 1个点在一排,有 n 条边连接,依次求点 i 往 点 n 的 ...

  4. 2018-2019 ICPC Northwestern European Regional Programming Contest (NWERC 2018)

    2018-2019 ICPC Northwestern European Regional Programming Contest (NWERC 2018) 题号 题目 知识点 难度 A Access ...

  5. 2016 China Collegiate Programming Contest Final

    2016 China Collegiate Programming Contest Final Table of Contents 2016 China Collegiate Programming ...

  6. 2021-2022 ACM-ICPC Latin American Regional Programming Contest

    2021-2022 ACM-ICPC Latin American Regional Programming Contest J. Joining Pairs 思路: 本题显然答案为NNN的情况为两条 ...

  7. 2020-2021 ICPC Southeastern European Regional Programming Contest (SEERC 2020)

    2020-2021 ICPC Southeastern European Regional Programming Contest (SEERC 2020) B. Reverse Game 题目描述: ...

  8. The 2020 ICPC Asia Shenyang Regional Programming Contest I题 Rise of Shadows(数论)

    题目链接The 2020 ICPC Asia Shenyang Regional Programming Contest 题目大意: 一天内有H小时,每小时M分钟,时针分针以恒定速率旋转. 现在若时针 ...

  9. The 2020 ICPC Asia Yinchuan Regional Programming Contest

    The 2020 ICPC Asia Yinchuan Regional Programming Contest A 开三个vector数组存储x,y,z轴上的点,unique+erase去重 #in ...

最新文章

  1. AI如何落地企业?UCloud三步走战略:Build,Train,Deploy
  2. 【Spring】23、ApplicationContext ,ApplicationContextAware,Listener,Event 的关系解读
  3. Android 源码VecotorDrawable
  4. 1099 Build A Binary Search Tree (30 分)【难度: 一般 / 知识点: 建立二叉搜索树】
  5. python 批量增加文件前缀_Excel VBA工作薄 7.5批量增加工作表前缀/后缀 工作表区分更方便...
  6. css设置不允许复制文本内容
  7. Java即时类| hashCode()方法与示例
  8. cocos2dx飞机大战开发记录(3)
  9. Discuz支持反对提示:抱歉您的请求来路不正确或表单无法提交的解决方法
  10. CFree 5.0最新注册码
  11. UE4官方文档阅读笔记——编程指南
  12. w10查看端口_win10系统查看端口是否打开的操作方法
  13. 算法笔记(9)-随机森林算法及Python代码实现
  14. electron tray click right click
  15. 从小米智能家居入手,揭秘物联网关键技术​
  16. win10打开计算机出现马赛克,图片有马赛克怎么去除?win10给图片去除马赛克的方法...
  17. Java 基于JavaMail实现向QQ邮箱发送邮件(未测试)
  18. 拼接字符串并以逗号隔开
  19. 诚之和:“何同学同款”缺货!曾现身罗永浩薇娅直播间,乐歌的网红带货路
  20. android 左右切换对话框 dialog

热门文章

  1. 医院客户关系管理/医院随访/CRM/HCRM
  2. DEEPIN系统下安装wine
  3. 阿里笔试2023-3-15
  4. 在HTML中 ( )属于非成对标记符,第一单元练习.doc
  5. PID循迹机器人及整定
  6. 在公司三年跌宕起伏的经历
  7. Round-Robin算法的verilog实现
  8. 计算机如何进入桌面,电脑如何设置开机直接进入桌面 开机直接进入桌面设置...
  9. 操作系统原理学习-概述
  10. win10硬盘启动从IDE改成ahci后无法启动系统的解决方式