Gym 100507H Pair: normal and paranormal (贪心)

Pair: normal and paranormal

题目链接:

http://acm.hust.edu.cn/vjudge/contest/126546#problem/H

Description

If you find yourself in Nevada at an abandoned nuclear range during Halloween time, you’ll become a witness of an unusual show. Here Ghostbusters hold annual tests for new versions of their proton packs. There are 𝑛 Ghostbusters and 𝑛 portable traps with ghosts, all are located on a semicircle. Each trap contains exactly one ghost. The ghosts may be of different types, but each Ghostbuster can neutralize with his weapon only one type of the evil spirits. On the count of three all ghost traps open at once and all Ghostbusters start to fire. Of course, each Ghostbuster shoots at the ghost, which his proton gun is able to neutralize. The most important thing here is not to cross proton beams of the guns. You know positions of all Ghostbusters and all the traps in this year’s tests. For each Ghostbuster determine which ghost he should shoot at, so that all the ghosts are neutralized and no two gun beams cross. You can assume that all proton beams are in the same horizontal plane and they don’t shoot ghosts through in case of a hit.

Input

In the first line there is an integer 𝑛 that is the number of Ghostbusters (1 ≤ 𝑛 ≤ 5 000). In the second line the sequence of 2𝑛 Latin letters is written, describing the allocation of the Ghostbusters and the traps on the semicircle. Uppercase letters correspond to the Ghostbusters and lowercase letters correspond to the traps. For example, “a” stands for a trap with the ghost of type “a”, while “A” stands for the Ghostbuster with the gun neutralizing ghosts of type “a”. The sequence has exactly 𝑛 lowercase letters and exactly 𝑛 uppercase letters.

Output

If the problem has a solution, output 𝑛 space-separated integers 𝑔1, 𝑔2, . . ., 𝑔𝑛, where 𝑔𝑖 is the number of the ghost 𝑖-th Ghostbuster should shoot at. Both Ghostbusters and ghosts are numbered with integers from 1 to 𝑛 in the order of their positions along the semicircle. All 𝑔𝑖 must be pairwise different. If the problem has several solutions, output any of them. If the problem has no solution, output “Impossible”.

Examples

2 AbBa 2 1 2 AbaB Impossible 1 Ab Impossible
##题意: 把题中的大写字母看成枪,小写字母看成目标点. (同一字母才能匹配射击) 所有点都在一个半圆上,给出所有点的相对位置. 求是否能打掉所有的目标点,且所有的射击路线(直线)不交叉. 如果可以,输出每个枪应该瞄准第几个目标点. (这套题真tm难读).
##题解: 由于给出的是相对位置,所以可以把问题抽象到横轴上. 使得横轴上所有相同字符大小写互相匹配且不出现交叉.(可以覆盖,不能交叉) 再考虑满足条件的两个区间的关系:只有两种关系: "两区间互不关联"或"一区间完全覆盖另一区间" 在纸上画一下图很容易看出来: 先处理相邻的匹配字符,并将这对字符删掉;再对剩下的考虑相邻匹配. 这样处理出来的匹配就一定满足上述条件. 注意考虑impossible的情况即可.
##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define eps 1e-8 #define maxn 11000 #define mod 100000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

int n;
char str[maxn];
bool vis[maxn];
int ans[maxn];

void add_ans(int a, int b) {
if(str[a]+32 == str[b]) ans[a] = b;
else ans[b] = a;
}

int main(int argc, char const *argv[])
{
//IN;

while(scanf("%d", &n) != EOF)
{
    scanf("%s", str);

    memset(vis, 0, sizeof(vis));
    int pairs = 0;
    int anss = 0;
    while(1) {
        int flag = 0;
        for(int i=0; i<2*n; i++) {
            if(vis[i]) continue;
            int j = i+1;
            while(j < 2*n) {
                if(vis[j]) {j++; continue;}
                if(abs(str[j]-str[i]) == 32) { /*找到相邻的匹配并标记它们*/
                    flag = 1;
                    vis[i] = vis[j] = 1;
                    add_ans(i, j);
                    anss++;
                    break;
                }
                else break;
            }
            i = j-1;
        }

        /*如果找不到匹配 或 已经找出所有匹配*/
        if(anss==n || !flag) break;
    }

    if(anss!=n) puts("Impossible");
    else {
        int cnt = 0;
        for(int i=0; i<2*n; i++) {
            if(str[i]>='a' && str[i]<='z')
                ans[i] = ++cnt;
        }
        cnt = 0;
        for(int i=0; i<2*n; i++) {
            if(str[i]>='a' && str[i]<='z') continue;
            cnt++;
            printf("%d%c", ans[ans[i]], cnt==n?'\n':' ');
        }
    }
}

return 0;

}

posted @ 2016-08-07 16:44  Sunshine_tcf  阅读(234)  评论(0编辑  收藏  举报