Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为什么runtime error,大神请解——同有一程序runtime error大牛帮帮忙

Posted by 18726135518 at 2016-06-15 13:31:12 on Problem 3977
In Reply To:为什么runtime error,大神请解 Posted by:18726135518 at 2016-06-15 13:30:26
#include<cstdio>
#include<algorithm>
#include<map>
#include<vector>
#define maxn 50
using namespace std;
typedef long long LL;
LL Abs(const LL& x){
	return x>=0?x:-x;
}
LL a[maxn];
vector<LL> c;
int n;
void Min(LL &res,int &ans,LL sum,int ct){
	if (sum>res)return;
	if (res>sum){
		res=sum;
		ans=ct;
		return;
	}else ans=min(ans,ct);
}
int main(){
	while (scanf("%d",&n)==1 && n){
		map<LL,int>b;
		for (int i=0;i<n;i++)scanf("%lld",&a[i]);
		LL res=a[0];
		int ans=1;
		int half=n/2;
		for (int i=1;i<1<<half;i++){
			LL sum=0;
			int ct=0;
			for (int j=0;j<half;j++)
				if (i>>j &1){
					sum+=a[i];
					ct++;
				}
			Min(res,ans,Abs(sum),ct);
			c.push_back(sum);
			b[sum]=min(b[sum],ct);	
		}
		sort(c.begin(),c.end());
		for (int i=1;i<1<<(n-half);i++){
			LL sum=0;
			int ct=0;
			for (int j=0;j<n-half;j++)
				if (i>>j &1){
					sum+=a[half+j];
					ct++;
				}
			Min(res,ans,Abs(sum),ct);
			int loc=lower_bound(c.begin(),c.end(),-sum)-c.begin();
			if (loc!=c.size())Min(res,ans,Abs(c[loc]+sum),b[c[loc]]+ct);
			if (loc!=0)Min(res,ans,Abs(c[loc-1]+sum),b[c[loc-1]]+ct);	
		}
		printf("%lld %d\n",Abs(res),ans);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator