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

背包水题 萌新的2进制优化模板 552K 47MS 536B

Posted by 416221843 at 2016-07-18 15:31:52 on Problem 1276
不优化貌似会tle 数据范围好像也不太对贡献了几个re 加一个判断条件就好

#include<string.h>
#include<stdio.h>
int s,n,max, a[111],x,y,z, i,k, j,v[100005];
int main() {
	while (scanf("%d", &s) != EOF)
	{
		memset(v, 0, sizeof(v)); v[0] = 1; z = 1;
		scanf("%d",&n);
		for (i = 1; i <= n; i++)
		{
			scanf("%d%d", &x, &y);
			k = 1;
			while (x>0)
			{if(x>=k) a[z++] = k*y, x = x - k, k = k<<1;
			else {
				a[z++] = x*y; break;
	}}}
		for (i = 1; i < z; i++)
			for (j = s; j >= 0; j--)
				if (v[j]&& j + a[i] <= s)
				 v[j + a[i]] = 1;
		for (i = s; i!=0; i--)
			if (v[i]) break;
		printf("%d\n",i);
	}}

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