【HDOJ】4393 Throw nails
水题,优先级队列。
1 /* 4393 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 #define lson l, mid, rt<<1 41 #define rson mid+1, r, rt<<1|1 42 43 typedef struct node_t { 44 int dis, id; 45 46 47 node_t() {} 48 node_t(int dis, int id): 49 dis(dis), id(id) {} 50 51 friend bool operator< (const node_t& a, const node_t& b) { 52 if (a.dis == b.dis) 53 return a.id > b.id; 54 return a.dis < b.dis; 55 } 56 57 } node_t; 58 59 const int maxn = 50005; 60 const int maxq = 101; 61 int delta[maxq]; 62 priority_queue<node_t> Q[maxq]; 63 int ans[maxn]; 64 65 void init() { 66 memset(delta, 0, sizeof(delta)); 67 rep(i, 1, maxq) 68 while (!Q[i].empty()) 69 Q[i].pop(); 70 } 71 72 int main() { 73 ios::sync_with_stdio(false); 74 #ifndef ONLINE_JUDGE 75 freopen("data.in", "r", stdin); 76 freopen("data.out", "w", stdout); 77 #endif 78 79 int t; 80 int f, s; 81 int n; 82 83 scanf("%d", &t); 84 rep(tt, 1, t+1) { 85 scanf("%d", &n); 86 init(); 87 rep(i, 1, n+1) { 88 scanf("%d %d", &f, &s); 89 Q[s].push(node_t(f, i)); 90 } 91 92 int mx, v, id, tmp; 93 node_t nd; 94 95 rep(i, 0, n) { 96 mx = -1; 97 rep(j, 1, 101) { 98 if (!Q[j].empty()) { 99 nd = Q[j].top(); 100 if (nd.dis+delta[j] > mx) { 101 mx = delta[j] + nd.dis; 102 id = nd.id; 103 v = j; 104 } else if (nd.dis+delta[j]==mx && nd.id<id) { 105 id = nd.id; 106 v = j; 107 } 108 } 109 delta[j] += j; 110 } 111 ans[i] = id; 112 Q[v].pop(); 113 } 114 115 printf("Case #%d:\n", tt); 116 printf("%d", ans[0]); 117 rep(i, 1, n) 118 printf(" %d", ans[i]); 119 putchar('\n'); 120 } 121 122 #ifndef ONLINE_JUDGE 123 printf("time = %d.\n", (int)clock()); 124 #endif 125 126 return 0; 127 }