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

总是超时 求大牛帮忙看看.........

Posted by skq at 2012-05-18 11:30:40 on Problem 3260
#include <cstdlib>
#include<deque>
#include <iostream>
using namespace std;
deque<int> vo(1,0),p(0,0);
int n,t,vi[101],m,mx=0,minn=100000 ;
int dp[25000]={0},dp1[25000]={0};
inline int min(int a,int b)
{
	return a<b?a:b;
}

void zeroonepack(int cost,int max)
{
	for(int i=max*max+t;i>=cost;i--)
		dp[i]=min(dp[i],dp[i-cost]+1);
}

void compack(int cost,int max)
{
	for(int i=cost;i<=max*max;i++)
       dp1[i]=min(dp1[i],dp[i-cost]+1);
}
int main()
{

	cin>>n>>t;
	for(int i=0;i<n;i++)
	{
		cin>>vi[i+1];
	}
	for(int j=1;j<=n;j++)
	{
		cin>>m;
		for(int h=1;h<=m;h++)
			vo.push_back(vi[j]);
	}
	for(deque<int>::iterator it=vo.begin();it!=vo.end();it++)
		if(*it>mx)
			mx=*it;
    for(int ii=1;ii<25000;ii++)
    {
            dp1[ii]=dp[ii]=100000;
    }
	for(int i1=1;i1<vo.size();i1++)
	       zeroonepack(vo[i1],mx);
	for(int i2=1;i2<=n;i2++)
	      compack(vi[i2],mx);
	for(int x=t;x<=t+mx*mx;x++)
		 if (dp[x] >= 0 && dp1[x-t] >= 0)
		     minn=min(minn,dp[x]+dp1[x-t]);
	if(minn!=100000)
        cout<<minn<<endl;
	else
		cout<<-1<<endl;
	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