zoukankan      html  css  js  c++  java
  • bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1034

    原来觉得和 bzoj4977跳伞求生 有点像(虽然还没做)。

    所以对于a[ ]从小到大来看,如果能赢b[ ]的最小的,就赢,不然就算了。

    经Zinn看TJ后发现a[ ]如果不能匹配的话,应该把b[ ]的最大的匹配掉。就像田忌赛马一样。还可以从大到小地看。

    如果较大的a[ ] <= b[ ]的话,看看较小的a[ ],如果小a[ ] > b[ ],本次先让小a[ ]匹配;如果小a[ ] == b[ ],让大a[ ]和小b[ ]匹配;如果小a[ ] < b[ ],就让小a[ ]和大b[ ]匹配。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1e5+5;
    int n,a[N],b[N],p0,p1,tp0,tp1,ans;
    int rdn()
    {
        int ret=0;char ch=getchar();
        while(ch>'9'||ch<'0')ch=getchar();
        while(ch>='0'&&ch<='9')(ret*=10)+=ch-'0',ch=getchar();
        return ret;
    }
    int main()
    {
        n=rdn();
        for(int i=1;i<=n;i++)a[i]=rdn();
        for(int i=1;i<=n;i++)b[i]=rdn();
        sort(a+1,a+n+1);sort(b+1,b+n+1);
        p0=tp0=n;p1=tp1=1;
        while(p1<=p0)
        {
            if(a[p0]>b[tp0])p0--,tp0--,ans+=2;
            else if(a[p1]>b[tp1])p1++,tp1++,ans+=2;
            else {if(a[p1]==b[tp0])ans++;tp0--;p1++;}
        }
        printf("%d ",ans);ans=0;
        p0=tp0=n;p1=tp1=1;
        while(p1<=p0)
        {
            if(b[p0]>a[tp0])p0--,tp0--;
            else if(b[p1]>a[tp1])p1++,tp1++;
            else {if(b[p1]==a[tp0])ans--;tp0--;p1++;ans+=2;}
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    申论1
    why factory pattern and when to use factory pattern
    jvm的字符串池
    is assembler instruction and machine instuction atomic
    jvm本身的多线程机制
    final
    java类的加载
    path和classpath的用途
    jar -cmf file1 file2 file3命令
    MANIFEST.MF中的MF是什么意思
  • 原文地址:https://www.cnblogs.com/Narh/p/9373368.html
Copyright © 2011-2022 走看看