zoukankan      html  css  js  c++  java
  • codeforces_732E-Sockets

    题目链接:http://codeforces.com/contest/732/problem/E

    第一次用multimap,只能说并不会用STL。

    将sockets排序,枚举每个socket然后找对应的computer,找不到就减半直到变成1(1要计算在内)

    使用multimap每次找到之后erase对应元素。

    multimap内部为pair结构体,可以用find()返回对应迭代器,然后就跟pair指针一样了。

    代码:

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <iostream>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <cstring>
     7 #include <map>
     8 #include <string>
     9 using namespace std;
    10 #define maxn 200005
    11 typedef pair<int, int> P;
    12 
    13 struct node{
    14     int v, c;
    15     bool operator < (const node & t) const {
    16         return v < t.v;
    17     }
    18     bool operator == (const node & t) const {
    19         return v == t.v;
    20     }
    21 }s[maxn];
    22 
    23 
    24 int n, m , p[maxn];
    25 int a[maxn], b[maxn], vis[maxn];
    26 multimap<int, int > mp;
    27 
    28 int main(){
    29 
    30     scanf("%d %d", &n, &m);
    31     for(int i = 1; i <= n; i++){
    32         scanf("%d", &p[i]);
    33         mp.insert(P(p[i], i));
    34     }
    35     for(int i = 0 ; i < m ; i++){
    36         scanf("%d", &s[i].v);
    37         s[i].c = i;
    38     }
    39 
    40     int c = 0, u = 0;
    41     sort(s, s + m);
    42     memset(a, 0, sizeof(a));
    43     memset(b, -1, sizeof(b));
    44     for(int i = 0; i < m; i++){
    45         multimap<int, int>::iterator mpit = mp.find(s[i].v);
    46         P tmp = *mpit;
    47         if(tmp.second != 0){
    48             c++;
    49             a[s[i].c] = 0;
    50             b[tmp.second] = s[i].c;
    51             mp.erase(mpit);
    52         }
    53         else{
    54             int cnt = 0, tms = s[i].v;
    55             while(tms){
    56                 tms = (int)(tms / 2.0 + 0.5);
    57                 cnt++;
    58                 multimap<int, int>::iterator mppit = mp.find(tms);
    59                 P tmmp = *mppit;
    60                 if(tmmp.second != 0){
    61                     u+= cnt;
    62                     c++;
    63                     a[s[i].c] = cnt;
    64                     b[tmmp.second] = s[i].c;
    65                     mp.erase(mppit);
    66                     break;
    67                 }
    68                 if(tms == 1)
    69                     break;
    70             }
    71         }
    72     }
    73 
    74     printf("%d %d
    ", c, u);
    75     for(int i = 0; i < m; i++)
    76         printf("%d ", a[i]);
    77     puts("");
    78     for(int i = 1; i <= n; i++)
    79         printf("%d ", b[i] + 1);
    80     puts("");
    81 }

    题目:

    E. Sockets
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The ICM ACPC World Finals is coming! Unfortunately, the organizers of the competition were so busy preparing tasks that totally missed an important technical point — the organization of electricity supplement for all the participants workstations.

    There are n computers for participants, the i-th of which has power equal to positive integer pi. At the same time there are m sockets available, the j-th of which has power euqal to positive integersj. It is possible to connect the i-th computer to the j-th socket if and only if their powers are the same: pi = sj. It is allowed to connect no more than one computer to one socket. Thus, if the powers of all computers and sockets are distinct, then no computer can be connected to any of the sockets.

    In order to fix the situation professor Puch Williams urgently ordered a wagon of adapters — power splitters. Each adapter has one plug and one socket with a voltage divider between them. After plugging an adapter to a socket with power x, the power on the adapter's socket becomes equal to , it means that it is equal to the socket's power divided by two with rounding up, for example  and .

    Each adapter can be used only once. It is possible to connect several adapters in a chain plugging the first to a socket. For example, if two adapters are plugged one after enother to a socket with power 10, it becomes possible to connect one computer with power 3 to this socket.

    The organizers should install adapters so that it will be possible to supply with electricity the maximum number of computers c at the same time. If there are several possible connection configurations, they want to find the one that uses the minimum number of adapters u to connect ccomputers.

    Help organizers calculate the maximum number of connected computers c and the minimum number of adapters u needed for this.

    The wagon of adapters contains enough of them to do the task. It is guaranteed that it's possible to connect at least one computer.

    Input

    The first line contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of computers and the number of sockets.

    The second line contains n integers p1, p2, ..., pn (1 ≤ pi ≤ 109) — the powers of the computers.

    The third line contains m integers s1, s2, ..., sm (1 ≤ si ≤ 109) — the power of the sockets.

    Output

    In the first line print two numbers c and u — the maximum number of computers which can at the same time be connected to electricity and the minimum number of adapters needed to connect ccomputers.

    In the second line print m integers a1, a2, ..., am (0 ≤ ai ≤ 109), where ai equals the number of adapters orginizers need to plug into the i-th socket. The sum of all ai should be equal to u.

    In third line print n integers b1, b2, ..., bn (0 ≤ bi ≤ m), where the bj-th equals the number of the socket which the j-th computer should be connected to. bj = 0 means that the j-th computer should not be connected to any socket. All bj that are different from 0 should be distinct. The power of the j-th computer should be equal to the power of the socket bj after plugging in abjadapters. The number of non-zero bj should be equal to c.

    If there are multiple answers, print any of them.

    Examples
    input
    2 2
    1 1
    2 2
    output
    2 2
    1 1
    1 2
    input
    2 1
    2 100
    99
    output
    1 6
    6
    1 0
  • 相关阅读:
    查看端口被占用
    Eclipse导入包
    Eclipse中构造方法自动生成
    Eclipse中get/set方法自动生成
    Eclipse改字体大小
    设计六原则
    类的关系
    JAVA实现多线程下载
    try...catch的前世今生
    447. 回旋镖的数量
  • 原文地址:https://www.cnblogs.com/bolderic/p/6824449.html
Copyright © 2011-2022 走看看