poj2549

睡觉 睡觉 睡觉今天好心塞!!!先是
//TLE了 0.0
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
using namespace std;

long long a[1010];

int main()
{
    int n;
    //freopen("2549.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        map<long long,long long> sum;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        sort(a,a+n);
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                sum[a[i]+a[j]]=a[i];

            }
        }
        long long result=-10000000000;
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                map<long long,long long>::iterator ite=sum.find(a[j]-a[i]);
                if(ite!=sum.end())
                {
                    if((ite->first-ite->second!=a[i])&&(ite->first-ite->second!=a[j])&&(ite->second!=a[i])&&(ite->second!=a[j]))
                        result=max(result,a[j]);
                }
                ite=sum.find(a[i]-a[j]);
                if(ite!=sum.end())
                {
                    if((ite->first-ite->second!=a[i])&&(ite->first-ite->second!=a[j])&&(ite->second!=a[i])&&(ite->second!=a[j]))
                        result=max(result,a[i]);
                }
            }
        }
        if(result==-10000000000)printf("no solution\n");
        else printf("%lld\n",result);
    }
}


后是各种TLE,编译错误。。。。。。。。。。最后还必须靠别人的代码来AC啊!!!!!

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<map>
#include<vector>
using namespace std;

long long a[1010];
struct node
{
    long long v;
    int i, j;
    node(long long v, int i, int j) : v(v), i(i), j(j){}
    bool operator < (const node& other) const
    {
        return v < other.v;
    }
    bool operator > (const node& other) const
    {
        return v > other.v;
    }
    bool operator != (const node& other) const
    {
        return i != other.i && j != other.j && i != other.j && j != other.i;
    }
};

bool cmp(node m,node n)
{
    return m.v<n.v;
}


int main()
{
    int n;
    //freopen("2549.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        vector<node>sum;
        vector<node>sum2;
        for(int i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        sort(a,a+n);
        for(int i=0;i<n-1;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                //dan.v=a[i]+a[j];
                //dan.from=i;
                //dan.to=j;
                sum.push_back(node(a[i]+a[j],i,j));
                //sum2[a[i]-a[j]]=a[j];
                //sum2[a[j]-a[i]]=a[i];
                //dan.v=a[i]-a[j];
                //dan.from=i;
                //dan.to=j;
                //node(a[i]-a[j],i,j);
                sum2.push_back(node(a[i]-a[j],i,j));
                //dan.v=a[j]-a[i];
                //dan.from=j;
                //dan.to=i;
                //node(a[j]-a[i],j,i);
                sum2.push_back(node(a[j]-a[i],j,i));
            }
        }
        long long result=-536870912;
        sort(sum.begin(),sum.end(),greater<node>());
        sort(sum2.begin(),sum2.end());
        vector<node>::iterator ite;
        for(ite=sum.begin();ite!=sum.end();ite++)
        {
            vector<node>::iterator lb=lower_bound(sum2.begin(),sum2.end(),*ite);
            vector<node>::iterator ub=upper_bound(lb,sum2.end(),*ite);
            /*for(;lb!=ub;lb++)
            {
                if(lb->v==ite->v)
                {
                    if((ite->i!=lb->j)&&(ite->j!=lb->j))
                        result=max(result,a[lb->i]);
                }
            }*/
            for (; lb != ub; ++lb)
            {
                if (*lb != *ite)
                {
                    result = max(result, ite->v + a[lb->j]);
                }
            }

        }
        if(result==-536870912)printf("no solution\n");
        else printf("%lld\n",result);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值