URAL 2019 Pair: normal and paranormal (贪心) -GDUT联合第七场

比赛题目链接

题意:n个人每人拿着一把枪想要杀死n个怪兽,大写字母代表人,小写字母代表怪兽。A只能杀死aB只能杀死b,如题目中的图所示,枪的弹道不能交叉。人和怪兽的编号分别是1n,问是否存在能全部杀死的情况,如果存在则输出编号1n的每个人杀死的怪兽的编号,如果不能输出"Impossible"。

题解:贪心,用递归实现,判断相邻的是否能构成一对,优先构成相邻的,如果不能就递归到前面看是否能构成一对即可。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,e;
char data[5005*2];
int Next[5005*2];
bool eq(char a,char b) {
    bool ta=0,tb=0;
    if(a>=‘A‘&&a<=‘Z‘) {
        a-=‘A‘;
        ta=1;
    }
    if(a>=‘a‘&&a<=‘z‘) a-=‘a‘;
    if(b>=‘A‘&&b<=‘Z‘) {
        b-=‘A‘;
        tb=1;
    }
    if(b>=‘a‘&&b<=‘z‘) b-=‘a‘;
    if(ta&&tb) return false;
    if(ta==false && tb==false) return false;
    return a==b;
}
void solve(int s) {
    //printf("%d %d\n",s,e);
    e++;
    if(e>=n) return;
    while(e<n&&!eq(data[s],data[e])) {
        solve(e);
    }
    if(e>=n) return;
    Next[s]=e;
    Next[e]=s;
    e++;
}
int main() {
    scanf("%d",&n);
    scanf("%s",data);
    n*=2;
    memset(Next,-1,sizeof(Next));
    e=0;
    while(e<=n) solve(e);
    for(int i=0;i<n;i++) {
        //printf("%d ",Next[i]);
        if(Next[i]==-1) {
            puts("Impossible");
            return 0;
        }
    }
    //puts("");
    int gid[5005*2],g=1;
    for(int i=0;i<n;i++) {
        if(data[i]>=‘a‘&&data[i]<=‘z‘) {
            gid[i] = g++;
        }
    }
    for(int i=0;i<n;i++) {
        if(data[i]>=‘A‘&&data[i]<=‘Z‘) {
            printf("%d ",gid[Next[i]]);
        }
    }
    puts("");
}
时间: 2024-12-17 21:12:35

URAL 2019 Pair: normal and paranormal (贪心) -GDUT联合第七场的相关文章

ural 2019 Pair: normal and paranormal

2019. Pair: normal and paranormal Time limit: 1.0 secondMemory limit: 64 MB 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 ve

URAL 2019 Pair: normal and paranormal (STL栈)

题意:在一个半圆内,有2*n个点,其中有大写字母和小写字母.其中你需要连接大写字母到小写字母,其中需要保证这些连接的线段之间没有相交. 如果能够实现,将大写字母对应的小写字母的序号按序输出. 析:我把它看成一个括号序列,然后用栈解决即可. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cs

Gym 100507H - Pair: normal and paranormal

题目链接:http://codeforces.com/gym/100507/attachments ---------------------------------------------------------------------------- 刚看这题的时候感觉是区间$DP$ 然而复杂度一直停留在$O(n^3)$优化不下来 后来又瞎试了一些贪心 都在较大的数据上挂掉了 反复琢磨着大写字母和相应小写字母匹配 便想到了以前做过的括号匹配 只不过此题大写字母和小写字母的左右关系是不限制的 因

2014-2015 ACM-ICPC, NEERC, Eastern Subregional Contest Problem H. Pair: normal and paranormal

题目链接:http://codeforces.com/group/aUVPeyEnI2/contest/229669 时间限制:1s 空间限制:64MB 题目大意:给定一个长度为2n,由n个大写字母和n小写字母组成的字符串,将对应的字母两两连接,且不相交,按顺序输出没个大写字母对应的小写字母的位置,如果不存在则输出"Impossible" 样例: /home/destr/Desktop/深度截图_选择区域_20181006175058.png /home/destr/Desktop/深

URAL 1025. Democracy in Danger (贪心)

1025. Democracy in Danger Time limit: 1.0 second Memory limit: 64 MB Background In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citizens (fortunately, there were no lots

hdu5360||多校联合第6场1008 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5360 Problem Description There are n soda conveniently labeled by 1,2,-,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hi

hdu5353||2015多校联合第六场1001 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=5353 Problem Description There are n soda sitting around a round table. soda are numbered from 1 to n and i-th soda is adjacent to (i+1)-th soda, 1-st soda is adjacent to n-th soda. Each soda has some candies

AtCoder NIKKEI Programming Contest 2019 C. Different Strokes (贪心)

题目链接:https://nikkei2019-qual.contest.atcoder.jp/tasks/nikkei2019_qual_C 题意:给出 n 种食物,Takahashi 吃下获得 ai 快乐值,Aoki 吃下获得 bi 快乐值,两人轮流吃,他们的目标是最大化自己获得的快乐值减去她人获得的快乐值吗,问最后该值是多少. 题解:易知 Takahashi 要最大化答案而 Aoki 要最小化答案,考虑全部食物由 Aoki 吃下,则ans = -(b1 + b2 + .... + bn),

URAL 1787 Turn for MEGA (贪心 + 模拟)

Turn for MEGA Time limit: 1.0 second Memory limit: 64 MB A traffic light at the turn for the "MEGA" shopping center from the Novomoskovskiy highway works in such a way that k cars are able to take a turn in one minute. At weekends all the reside