Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
边界条件真难处理#include <stdio.h> #include <stdlib.h> int n, m , v, f[201000], h[201000]; struct q{int a, b;}q[1001000]; void geng (int p, int r) { int k ; if (p < r) { k = hui (p, r ) ; geng (p, k ) ; geng (k + 1 , r) ;} } int hui (int p, int r) { int i = p - 1, j = r + 1, x = q[p].a, t ; while (1) { do { j -- ;}while (q[j].a > x) ; do { i ++ ;}while (q[i].a < x) ; if (i < j){ t = q[i].a; q[i].a = q[j].a; q[j].a = t ; t = q[i].b; q[i].b = q[j].b; q[j].b = t ;} else return j; } } void down (int i) { int t = i , r ; if (i * 2 <= h[0] && h[i] < h[i * 2]) t = i * 2; if (i * 2 + 1 <= h[0] && h[i * 2 + 1] > h[t]) t = i * 2 + 1 ; if (t != i){ r = h[t]; h[t] = h[i]; h[i] = r ; down (t) ;} } void up (int i ) { int t, r ; while (i > 1 && h[i / 2] < h[i]){ t = i / 2 ; r = h[i]; h[i] = h[t]; h[t] = r ; i /= 2 ;} } main () { int i , j , t ; scanf ("%d%d%d", &n, &m, &v) ; for (i = 1; i <= m; i ++ ) scanf ("%d%d", &q[i].a, &q[i].b) ; geng (1, m) ; n /= 2 , f[n] = h[0] = 0 ; for (i = 1; i <= n; i ++ ){ f[n] += q[i].b ; h[0] ++ ; h[h[0]] = q[i].b; up (h[0]) ;} while (i <= m){ if (q[i].b < h[1]){ f[i] = f[i - 1] + q[i].b - h[1] ; h[1] = q[i].b ; down (1) ;} else f[i] = f[i - 1] ; i ++ ;} t = 0, h[0] = 0; for (i = m; i > m - n; i -- ){ t += q[i].b; h[0] ++ ; h[h[0]] = q[i].b; up (h[0]) ;} if (t + q[i].b + f[i - 1] <= v) i ++ ; while (i >= n + 2 ){ if (q[i].b < h[1]){ t += q[i].b - h[1] ; h[1] = q[i].b ; down (1) ;} if (t + q[i - 1].b + f[i - 2] <= v ){i -- ; break ;} i -- ;} printf ("%d\n", i != n + 1 || t + f[i - 1] + q[i].b <= v? q[i].a : - 1) ; system ("pause") ; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator