zoj3888 找第二大

 题目简化后最终要求的就是第二大的数。但是由于数据较大,不能直接求。可以先预处理,求出所有情况。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int from;
    int to;
};
struct Node
{
    int max1;
    int max2;//di er ge
};
node a[50002];
int n,m,p,ans[50002];
bool cmp(node aa,node bb)
{
    if(aa.from!=bb.from)
    return aa.from>bb.from;
    return aa.to>bb.to;
}
int main()
{
    int i,j;
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        Node ret;
        memset(ans,0,sizeof(ans));
        for(i=0;i<m;i++)
            scanf("%d%d",&a[i].from,&a[i].to);
        sort(a,a+m,cmp);
        int count;
        ret.max1=ret.max2=9999999;
        count=0;
        j=0;
        for(i=n;i>=1;i--)//bian li nian
        {
            for(;j<m;j++)
            {
                if(a[j].from<i)
                    break;
                count++;
                int temp=a[j].to;
                if(temp<ret.max2)
                {
                    ret.max2=temp;
                    if(ret.max2<ret.max1)
                    {
                        int t=ret.max2;
                        ret.max2=ret.max1;
                        ret.max1=t;
                    }
                }
            }
            if(count>=2)
            {
                if(i-ret.max2<0)
                    ans[i]=0;
                else
                ans[i]=i-ret.max2;
            }
        }
        while(p--)
        {
            int x;
            scanf("%d",&x);
            printf("%d\n",ans[x]);
        }
    }
}

 

posted @ 2015-08-01 09:05  sweat123  阅读(184)  评论(0编辑  收藏  举报