本文主要是介绍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);}
}
这篇关于poj2549的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!