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 ...
随机推荐
- BZOJ1916: [Usaco2010 Open]冲浪
n<=50000个点m<=150000的带边权DAG,保证1入度0,n出度0,其他点入度出度均不为0,求:从一号点开始到n,期间有可能K<=10次随机选边走,最坏情况下总边权多少. ...
- PatentTips - Solid State Memory Wear Leveling
BACKGROUND OF THE INVENTION Solid-state memory devices encompass rewritable non-volatile memory devi ...
- CritterAI与Recast Navigation寻路
版权声明:本文为博主吴欣伟原创文章,未经博主允许不得转载. 前言 这篇文章写于去年,由于工作需要,故写出这个研究文档,发现网上有关此寻路库的中文资源十分稀少,故发布出来与诸位共享交流,如文中有不对之处 ...
- php之memcache学习
php之memcache学习 简介: memcache是一个分布式高速缓存系统. 分布式是说可以部署在多台服务器上,实现集群效果: 高速是因为数据都是维护在内存中的: 特点和使用场景: 1.非持久化存 ...
- Linux中的进程与线程
介绍了Linux下fork()创建进程以及使用pthread_create()创建线程的方法 1. 基于进程的斐波那契数列 在下面的代码中,由子进程进行斐波那契数列的输出,父进程要等待子进程输出完毕, ...
- 转 asterisk拨号规则
asterisk拨号规则 一.前言 本文档以asterisk-1.4.32为基础写作而成,可能和其他版本有些区别. 二.Asterisk dialplan 基本结构 Asterisk dial ...
- 下载数据到Excel,工具类
使用反射将model数据下载到Excel中 package test.upload.utils; import java.lang.reflect.Method; import java.math.B ...
- kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置
KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...
- 理解Android进程创建流程(转)
/frameworks/base/core/java/com/android/internal/os/ - ZygoteInit.java - ZygoteConnection.java - Runt ...
- 怎样更好的设计你的REST API之基于REST架构的Web Service设计及REST框架实现
一.REST 含状态传输(英文:Representational State Transfer,简称REST)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格. 眼下在 ...