bnu_4060 Plants vs. Zombies

原题链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=4060

分析:

        解法一:注意到这里只有一个数据的单起来的,其他都两两配对,有进有出(被杀死)。那么我们用sum表示他们的和,进则加,出则减。最好剩下的sum就是单着的那个数。

        解法二:对于这种两两配对,就一个数单着的题,可以用‘^’(异或)运算去掉相同的,留下不同的。

              异或运算:  0^a=a;   a^a^b=b;  a^a=0;

                a^c^b^a^b=c;(相同的^后都变成0了,最后只剩下单个的和0异或);

我的代码(解一):

 

#include<stdio.h>
typedef long long LL;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        LL sum=0;
        for(int i=1;i<=2*n-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(!a) sum+=b;
            else   sum-=b;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

 

我的代码(解二):

 

#include<stdio.h>
typedef long long LL;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int ans=0;
        for(int i=1;i<=2*n-1;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            ans^=b;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

总结:

        一开始用二分,果断超时。接着用STL中的map容器,果断超内存。看别人的代码后,我和我的小心肝都惊呆了。。原来可以这样。(想了下,以前其实在HDOJ上做过这类题的 o(>﹏<)o).

        杭电的原题戳这里


posted on 2013-08-20 20:40  bbsno  阅读(156)  评论(0编辑  收藏  举报

导航