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 local
    freopen("data.txt","r",stdin);
#endif
    work();
    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 local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}
View Code

 

posted on 2016-10-22 20:34  stupid_one  阅读(208)  评论(0编辑  收藏  举报

导航