【CF】222 Div.1 B Preparing for the Contest
这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心。
对时间二分, 对费用采用贪心。
1 /* 377B */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector> 9 #include <deque> 10 #include <algorithm> 11 #include <cstdio> 12 #include <cmath> 13 #include <ctime> 14 #include <cstring> 15 #include <climits> 16 #include <cctype> 17 #include <cassert> 18 #include <functional> 19 #include <iterator> 20 #include <iomanip> 21 using namespace std; 22 //#pragma comment(linker,"/STACK:102400000,1024000") 23 24 #define sti set<int> 25 #define stpii set<pair<int, int> > 26 #define mpii map<int,int> 27 #define vi vector<int> 28 #define pii pair<int,int> 29 #define vpii vector<pair<int,int> > 30 #define rep(i, a, n) for (int i=a;i<n;++i) 31 #define per(i, a, n) for (int i=n-1;i>=a;--i) 32 #define clr clear 33 #define pb push_back 34 #define mp make_pair 35 #define fir first 36 #define sec second 37 #define all(x) (x).begin(),(x).end() 38 #define SZ(x) ((int)(x).size()) 39 #define lson l, mid, rt<<1 40 #define rson mid+1, r, rt<<1|1 41 42 typedef struct node_t { 43 int x, id; 44 node_t() {} 45 node_t(int x_, int id_): 46 x(x_), id(id_) {} 47 friend bool operator< (const node_t& a, const node_t& b) { 48 return a.x > b.x; 49 } 50 } node_t; 51 52 53 typedef struct student { 54 int x, p, id; 55 student() {} 56 student(int x_, int p_, int id_): 57 x(x_), p(p_), id(id_) {} 58 friend bool operator< (const student& a, const student& b) { 59 return a.x > b.x; 60 } 61 } student; 62 63 const int maxn = 1e5+5; 64 int a[maxn]; 65 int n, m, s; 66 node_t N[maxn]; 67 student S[maxn]; 68 69 bool comp(const student& a , const student& b) { 70 return a.x > b.x; 71 } 72 73 bool judge(int t) { 74 priority_queue<int,vi, greater<int> > Q; 75 int i, j, k; 76 int p; 77 int tot = s; 78 79 i = 0; // i for student 80 j = 0; // j for Q 81 while (j < m) { 82 while (i<n && S[i].x>=N[j].x) { 83 Q.push(S[i].p); 84 ++i; 85 } 86 if (Q.empty()) 87 break; 88 p = Q.top(); 89 Q.pop(); 90 if (p > tot) 91 return false; 92 tot -= p; 93 j += t; 94 } 95 96 return j>=m; 97 } 98 99 void bfs(int t) { 100 priority_queue<pii,vpii, greater<pii> > Q; 101 int i, j, k, id; 102 int m_; 103 104 i = 0; // i for student 105 j = 0; // j for Q 106 while (j < m) { 107 while (i<n && S[i].x>=N[j].x) { 108 Q.push(mp(S[i].p, S[i].id)); 109 ++i; 110 } 111 id = Q.top().sec; 112 Q.pop(); 113 m_ = min(m, j+t); 114 for (k=j; k<m_; ++k) 115 a[N[k].id] = id; 116 j += t; 117 } 118 } 119 120 int main() { 121 ios::sync_with_stdio(false); 122 #ifndef ONLINE_JUDGE 123 freopen("data.in", "r", stdin); 124 freopen("data.out", "w", stdout); 125 #endif 126 127 int ans = -1; 128 129 scanf("%d %d %d", &n, &m, &s); 130 rep(i, 0, m) { 131 scanf("%d", &N[i].x); 132 N[i].id = i; 133 } 134 rep(i, 0, n) { 135 scanf("%d", &S[i].x); 136 S[i].id = i+1; 137 } 138 rep(i, 0, n) 139 scanf("%d", &S[i].p); 140 141 sort(N, N+m); 142 sort(S, S+n); 143 144 if (S[0].x < N[0].x) { 145 puts("NO"); 146 return 0; 147 } 148 149 int l = 1; 150 int r = m+1; 151 int mid; 152 153 while (l <= r) { 154 mid = (l+r)>>1; 155 if (judge(mid)) { 156 ans = mid; 157 r = mid - 1; 158 } else { 159 l = mid + 1; 160 } 161 } 162 163 if (ans > 0) { 164 puts("YES"); 165 bfs(ans); 166 rep(i, 0, m) 167 printf("%d ", a[i]); 168 putchar('\n'); 169 } else { 170 puts("NO"); 171 } 172 173 #ifndef ONLINE_JUDGE 174 printf("time = %d.\n", (int)clock()); 175 #endif 176 177 return 0; 178 }