Codeforces Round #377 (Div. 2) E. Sockets
http://codeforces.com/contest/732/problem/E
?
題目說得很清楚,每個電腦去插一個插座,然后要剛好的,電腦的power和sockets的值相同才行。
如果不同,還有一個操作,就是在sockets中插東西,使得sockets的值變成val / 2 + (val & 1)
要求輸出最多插多少個電腦和最小的代價。
?
開始的時候,就有一個暴力的思路了,
就是,用優先隊列維護sockets,每次彈出插了最小adapters的一個sockets,然后就是電腦那里找,有得插就插。
找的時候,明顯可以把電腦的val排序,然后二分加速。但是有一個問題,就是電腦的val可能是相同的。然后查找完后,
又要標記它用了,后來二分的時候就很麻煩,如果刪除,復雜度也高。
同時因為也要記錄電腦的id(因為要輸出方案),這樣用set來保存的話,我剛開始不懂set.lower_bound中怎么二分其中一個值,
因為我只需要二分val。其實就把整個結構體二分就行了,會根據你的重載運算符來二分的,優先二分val,再二分id。
而且set的刪除也可以Logn,所以這題就相當于是STL的題了。
set刪除迭代器,處理下邊界就好,it == ss.end()的時候、
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL;#include <set> #include <map> #include <queue> #include <string> const int maxn = 200000 + 20; struct nodeComputer {int val, id;bool operator < (const struct nodeComputer &rhs) const {if (val != rhs.val) return val < rhs.val;else return id < rhs.id;}nodeComputer(int a, int b) : val(a), id(b) {} }; struct nodeSocket {int val, id, cnt;bool operator < (const struct nodeSocket &rhs) const {if (cnt != rhs.cnt) return cnt > rhs.cnt;else if (val != rhs.val) return val > rhs.val;else return id > rhs.id;}nodeSocket(int a, int b, int c) : val(a), id(b), cnt(c) {} }; set<struct nodeComputer>ss; priority_queue<struct nodeSocket>que; int ansSocker[maxn]; int ansComputer[maxn]; void work() {int n, m;scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i) {int val;scanf("%d", &val);ss.insert(nodeComputer(val, i));} // for (set<struct nodeComputer> :: iterator it = ss.begin(); it != ss.end(); ++it) { // printf("%d %d\n", it->val, it->id); // } // set<struct nodeComputer> :: iterator it = ss.lower_bound(nodeComputer(1, 3)); // printf("%d\n", it == ss.end());for (int i = 1; i <= m; ++i) {int val;scanf("%d", &val);que.push(nodeSocket(val, i, 0));}int ansc = 0, ansu = 0;while (!que.empty()) {struct nodeSocket t = que.top();que.pop();set<struct nodeComputer> :: iterator it = ss.lower_bound(nodeComputer(t.val, 0));if (it == ss.end() || it->val != t.val) {if (t.val == 1) continue; //再分解就沒意思了que.push(nodeSocket(t.val / 2 + (t.val & 1), t.id, t.cnt + 1));} else if (it->val == t.val) {ansSocker[t.id] = t.cnt;ansComputer[it->id] = t.id;ansc++;ansu += t.cnt;ss.erase(it);}}printf("%d %d\n", ansc, ansu);for (int i = 1; i <= m; ++i) {printf("%d ", ansSocker[i]);}printf("\n");for (int i = 1; i <= n; ++i) {printf("%d ", ansComputer[i]);}printf("\n"); }int main() { #ifdef localfreopen("data.txt","r",stdin); #endifwork();return 0; } View Code?
然而因為沒個插頭都會判斷是否要用,該是要插多少個adapters的,還是要插。用一個普通隊列來維護,能達到一樣的效果。
時間就降下來了
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL;#include <set> #include <map> #include <queue> #include <string> const int maxn = 200000 + 20; struct nodeComputer {int val, id;bool operator < (const struct nodeComputer &rhs) const {if (val != rhs.val) return val < rhs.val;else return id < rhs.id;}nodeComputer(int a, int b) : val(a), id(b) {} }; struct nodeSocket {int val, id, cnt;bool operator < (const struct nodeSocket &rhs) const {if (cnt != rhs.cnt) return cnt > rhs.cnt;else if (val != rhs.val) return val > rhs.val;else return id > rhs.id;}nodeSocket(int a, int b, int c) : val(a), id(b), cnt(c) {} }; set<struct nodeComputer>ss; queue<struct nodeSocket>que; int ansSocker[maxn]; int ansComputer[maxn]; void work() {int n, m;scanf("%d%d", &n, &m);for (int i = 1; i <= n; ++i) {int val;scanf("%d", &val);ss.insert(nodeComputer(val, i));} // for (set<struct nodeComputer> :: iterator it = ss.begin(); it != ss.end(); ++it) { // printf("%d %d\n", it->val, it->id); // } // set<struct nodeComputer> :: iterator it = ss.lower_bound(nodeComputer(1, 3)); // printf("%d\n", it == ss.end());for (int i = 1; i <= m; ++i) {int val;scanf("%d", &val);que.push(nodeSocket(val, i, 0));}int ansc = 0, ansu = 0;while (!que.empty()) {struct nodeSocket t = que.front();que.pop();set<struct nodeComputer> :: iterator it = ss.lower_bound(nodeComputer(t.val, 0));if (it == ss.end() || it->val != t.val) {if (t.val == 1) continue; //再分解就沒意思了que.push(nodeSocket(t.val / 2 + (t.val & 1), t.id, t.cnt + 1));} else if (it->val == t.val) {ansSocker[t.id] = t.cnt;ansComputer[it->id] = t.id;ansc++;ansu += t.cnt;ss.erase(it);}}printf("%d %d\n", ansc, ansu);for (int i = 1; i <= m; ++i) {printf("%d ", ansSocker[i]);}printf("\n");for (int i = 1; i <= n; ++i) {printf("%d ", ansComputer[i]);}printf("\n"); }int main() { #ifdef localfreopen("data.txt","r",stdin); #endifwork();return 0; } View Code?
轉載于:https://www.cnblogs.com/liuweimingcprogram/p/5988302.html
總結
以上是生活随笔為你收集整理的Codeforces Round #377 (Div. 2) E. Sockets的全部內容,希望文章能夠幫你解決所遇到的問題。