Benelux Algorithm Programming Contest 2019 H. Historic Exhibition(思維)

The Benelux Artistic Pottery Consortium is preparing for an exhibit of its most prized urns and vases at a gallery in Nijmegen. Due to the sheer number of vases to be put on display the gallery has trouble finding a pedestal of the right size for every single vase. They have pedestals available that can either be placed normally or upside down and can be characterised by the diameter of their top and bottom surface. Moreover, the diameter of the top and bottom varies by at most one unit length.For artistic reasons, it is important that the diameter of the base of a vase matches the diameter of the surface of the pedestal it is placed on. You have been asked to find a way to place all the vases on available pedestals. In order to make this work, you might need to turn some of the pedestals upside down. For example, Figure H.1 shows a possible assignment of pedestals to vases for sample input 1. Assist the gallery by writing a program to compute such an assignment.ff0b1c063e.png

  • input 

The first line contains two integers 0 \le p, v \le 10^40≤p,v≤104 the number of pedestals and the number of vases.Then follow p lines, the i_{th}ith​ of which contains two integers 1 \le a_i, b_i \le 10^41≤ai​,bi​≤104 denoting the diameters of the different sides of pedestal i. It is given that \vert a_i-b_i\vert \le 1∣ai​−bi​∣≤1.Then follows a single line containing v integers 1 \le c_1, c_2, ..., c_v \le 10^41≤c1​,c2​,...,cv​≤104, where ci denotes the diameter of vase i.

  • output

Output v distinct integers 1 \le x_1, x_2, ... x_v \le p1≤x1​,x2​,...xv​≤p such that vase i can stand on pedestal x_ixi​, or print impossible if no assignment of vases to pedestals exists.If there are multiple possible solutions, you may output any one of them.

本題答案不唯一,符合要求的答案均正確

樣例輸入1複製

4 3
1 2
4 5
2 3
2 2
1 2 3

樣例輸出1複製

1
4
3

樣例輸入2複製

2 2
1 1
2 3
1 1

樣例輸出2複製

impossible

樣例輸入3複製

2 3
9 8
4 5
4 9 5

樣例輸出3複製

impossible

題意:

將花瓶與其大小相同的底座配對,底座可以倒置,給出花瓶底的大小和底座上下底的大小,底座的兩個底大小最多差1。

思路:

使用unordered_map<int, stack<int> >mp1, mp2; 將上底與下底相同的底座的下標存入mp1中,將上底與下底不同的底座按較小的大小存入mp2中。再將花瓶按大小排序,對於每個花瓶大小 w ,先查詢 w 是否在mp1中,否的話查詢 w - 1 是否在mp2中(因爲花瓶是從小到大排序的,若較大底都不滿足條件,這個底座就沒有用了),否的話再查詢 w 是否在mp2中,都不在的話impossible。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4 + 10;

int ans[N];

struct node
{
    int id, w;
}s[N];

bool cmp(node x, node y)
{
    return x.w < y.w;
}

int main()
{
    int p, v, aa, bb;
    while(~scanf("%d%d", &p, &v))
    {
        unordered_map<int, stack<int> >mp1;
        unordered_map<int, stack<int> >mp2;
        for(int i = 1; i <= p; ++i)
        {
            scanf("%d%d", &aa, &bb);
            if(aa > bb)
                swap(aa, bb);
            if(aa == bb)
            {
                mp1[aa].push(i);
            }
            else
            {
                mp2[aa].push(i);
            }
        }
        for(int i = 1; i <= v; ++i)
        {
            scanf("%d", &s[i].w);
            s[i].id = i;
        }
        sort(s + 1, s + v + 1, cmp);
        bool flag = 1;
        for(int i = 1; i <= v; ++i)
        {
            if(mp1[s[i].w].size())
            {
                ans[s[i].id] = mp1[s[i].w].top();
                mp1[s[i].w].pop();
            }
            else if(mp2[s[i].w - 1].size())
            {
                ans[s[i].id] = mp2[s[i].w - 1].top();
                mp2[s[i].w - 1].pop();
            }
            else if(mp2[s[i].w].size())
            {
                ans[s[i].id] = mp2[s[i].w].top();
                mp2[s[i].w].pop();
            }
            else
            {
                flag = 0;
                break;
            }
        }
        if(!flag)
        {
            cout<<"impossible"<<'\n';
        }
        else
        {
            for(int i = 1; i <= v; ++i)
            {
                cout<<ans[i]<<'\n';
            }
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章