Gym 100512G Grand Tour (拓扑排序)
题意:一个团队要去参观一些学校,某些学校要在某些学校之前先参观,并且每个学校有一个权值,团队去的时间与权值的差作为难过度(最小是0),
所有的难过度的最大值是伤心度,让你安排参观顺序,使得这个伤心度最小。
析:拓扑排序,并且要逆序排,这样的话,时间大的优先,可以用优先队列实现。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int id, val;
Node() { }
Node(int i, int v) : id(i), val(v) { }
bool operator < (const Node& p) const{
return val < p.val;
}
};
vector<int> G[maxn];
int in[maxn], a[maxn];
int ans[maxn]; int main(){
freopen("grand.in", "r", stdin);
freopen("grand.out", "w", stdout);
while(scanf("%d", &n) == 1 && n){
for(int i = 1; i <= n; ++i){ scanf("%d", a+i); G[i].clear(); }
scanf("%d", &m);
int u, v;
for(int i = 0; i < m; ++i){
scanf("%d %d", &u, &v);
G[v].push_back(u);
++in[u];
} priority_queue<Node> pq;
for(int i = 1; i <= n; ++i) if(!in[i]) pq.push(Node(i, a[i]));
int cnt = n, num = 0;
while(!pq.empty()){
Node u = pq.top(); pq.pop();
num = Max(num, Max(0, cnt-u.val));
ans[cnt--] = u.id;
int x = u.id;
for(int j = 0; j < G[x].size(); ++j){
int t = G[x][j];
--in[t];
if(!in[t]) pq.push(Node(t, a[t]));
}
} printf("%d\n", num);
printf("%d", ans[1]);
for(int i = 2; i <= n; ++i) printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
Gym 100512G Grand Tour (拓扑排序)的更多相关文章
- HDU - 5695 Gym Class (优先队列+拓扑排序)
题意:有N个人,每个人的ID为1~N,部分同学A不希望部分同学B排在他之前,排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分数.在满足这个前提的情况下,将N个人排 ...
- 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)
Gym Class Accepts: 849 Submissions: 4247 Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 2016 百度之星初赛 Gym Class(优先队列+拓扑排序)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pract ...
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- 【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs' Box Boutique
给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排 ...
- HDU - 5695 Gym Class 【拓扑排序】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5695 思路 给定一些关系 进行拓扑排序 但是有一个要求 对于哪些没有确切的位置的点 要按照ID大小 I ...
- Gym 100792 King's Rout 拓扑排序
K. King's Rout time limit per test 4.0 s memory limit per test 512 MB input standard input output st ...
随机推荐
- Python()-类命名空间和对象/实例命名空间
类命名空间和对象/实例命名空间: 创建类, 就会创建一个类的名称空间, 空间:存储类的属性 属性: 静态属性:直接定义在类下面 & 和类名关联 的变量 对象属性:在类内和self关联 & ...
- 清北省选 DAY last 集锦
这是题目描述的链接: http://lifecraft-mc.com/wp-content/uploads/2018/03/problems1.pdf (虽然这次没去清北,但还是厚颜无耻的做了一下这套 ...
- MySQL中的数据类型的长度范围和显示宽度(转)
长度范围是随数据类型就已经是固定的值,而显示宽度与长度范围无关. 以下是每个整数类型的存储和范围(来自MySQL手册) 类型 字节 最小值 最大值 (带符号的/无符号的) (带符号的/无符号的) TI ...
- Java的循环结构
以下内容引用自http://wiki.jikexueyuan.com/project/java/loop-control.html: 可能存在一种情况,当需要执行的代码块数次,通常被称为一个循环.Ja ...
- 使用图像扫描控件ScanOnWeb实现在线图像扫描
今天上网查资料,看到一篇文章,描述的是一个开发OA软件的公司解决浏览器嵌入式扫描仪编程的文章,文章描述了改OA厂商的工程师如何辛苦的克服了各种技术难题,最终实现了在线图像扫描处理,然后又在无数个不眠的 ...
- java递归删除文件及目录
package base; import java.io.File; public class delete { public static void main(String[] args) ...
- 输入年份,然后打印出该年的万年历,以及标识出当天日期。相似于linux下的cal -y结果。
public class Permanent { public static boolean isLeapYear(int year){//能被4整除但不能被100整除.或者能被400整除 boole ...
- Redis管理key命令
1 DEL key该命令用于在 key 存在时删除 key. 2 DUMP key 序列化给定 key ,并返回被序列化的值. 3 EXISTS key 检查给定 key 是否存在. 4 EXPIRE ...
- linux【第六篇】用户和用户管理及定时任务复习
定时任务复习 1.什么是定时任务? 2.如何编辑查看定时任务(配置文件位置?),语法的特殊字符意义是什么?- * , / 3.书写定时任务有哪些要领? 4.生产如何调试定时任务 5.生产场景配置定时任 ...
- Java 文件路径的读取
记得在操作系统中了解到文件读取有两种方式,当然这在各编程语言中也是通用的,所以java路径也分,相对和绝对路径. 绝对路径 绝对路径URI ,听着和URL非常相似.那我们就来看看吧. URI(Unif ...