# 浙大acm2059,我用贪心算法但是老wa,哪为高手能帮我解决

HelpMeACM 2004-08-09 04:46:59
//http://acm.zju.edu.cn/show_problem.php?pid=2059
#include<stdio.h>
#include<math.h>
#define N 200

int main()
{
int i,j,k,t,w[N];
char n;
//如果使用unsigned int类型,就WA
while(scanf("%d",&n),n!=-1)
{
int s1=0,s2=0;

for(i=0,k=(n>>1);i<k;i++)
{scanf("%d",&w[i]);s1+=w[i];}

for(;i<n;i++)
{scanf("%d",&w[i]);s2+=w[i];}

if(((s1+s2)&1)) {printf("Sorry\n");continue;}

bool s;
while(1)
{
s=true;
for(i=0;i<k;i++)
for(j=k;j<n;j++)
{
if(fabs((s1-w[i]+w[j])-(s2+w[i]-w[j]))<fabs(s1-s2))
{
s=false;
s1=s1-w[i]+w[j];
s2=s2+w[i]-w[j];
t=w[i];w[i]=w[j];w[j]=t;
}//if
}

if(s)
{
//if(s1>s2)
//{t=s1;s1=s2;s2=t;}
//printf("%d %d\n",s1,s2);
if(s1==s2) printf("%d\n",s1);
else printf("Sorry\n");
break;
}
}//while
}//while
return 1;
}
...全文
173 9 点赞 打赏 收藏 举报

9 条回复

HelpMeACM 2004-08-12

• 打赏
• 举报

xiaojun_wu 2004-08-12
#include<stdio.h> //AC table[i]表示两边差i时短边最大高度
#include<string.h>

#define MAX 2000

int table[MAX];
int attach[MAX];
int sum;
{
int loop;
int last;
int temp;
memcpy(attach,table,MAX*4);
for(loop=0; loop <= sum ; loop++)
if( table[loop] != -1 ){
last=table[loop];
temp= table[height+loop] > last ? table[height+loop]:last;
attach[height+loop] = attach[height+loop] > temp ? attach[height+loop]:temp;

if(height >= loop){
temp = table[height-loop] > last+loop ? table[height-loop]:last+loop;
attach[height-loop] = attach[height-loop] >temp ?table[height-loop] :temp;
}
else{
temp = table[loop-height] > last+height ? table[loop-height]:last+height;
attach[loop-height] = attach[loop-height] > temp ? attach[loop-height] :temp;
}
}
memcpy( table, attach , MAX*4);
}
void init()
{
table[0]=0;
sum=0;
for(int loop=1;loop<MAX;loop++)
table[loop]=-1;
}

void PR()
{
for(int loop=0;loop<=11;loop++)
printf("%d ",table[loop]);
printf("\n");
}
int main()
{
int num;
int loop;
int temp;

while(scanf("%d",&num)!=EOF && num >=0 )
{
init();
for(loop = 0;loop < num;loop++){
scanf("%d",&temp);
sum+=temp;
//PR();
}

if(table[0] == 0)
printf("Sorry\n");
else
printf("%d\n",table[0]);
}
return 0;
}
• 打赏
• 举报

xiaojun_wu 2004-08-11
HelpMeACM，感觉你对DP的理解不对吧，1711是DP出来的？？不明白。1711明显是一个计数的DFS啊，哪位大哥DP出来能不能给小弟看看？还有，这道题从两位同学的代码来看，该是对题意的理解有误：

• 打赏
• 举报

xiaojun_wu 2004-08-10

• 打赏
• 举报

ZhangJingZJU 2004-08-10

• 打赏
• 举报

HelpMeACM 2004-08-10

#include <iostream.h>

#define MAXSIZE 300
int numberOfSolutions, listlength, list[MAXSIZE];
int used[MAXSIZE]; // used[i] is 1 if list[i] is used in the sum

int DivSum;

void findSum (int sum, int iEnd, int numSkipped)
{
if (iEnd == listlength) return;
int newsum = sum - list[iEnd];
if ((numSkipped != list[iEnd])&&(newsum >= 0))
{
used[iEnd] = 1;
if (newsum == 0)
{
numberOfSolutions++;
//for (int i = 0; i<iEnd; i++)
//if (used[i]) cout << list[i] << '+';
//cout << list[iEnd] << endl;
cout<<DivSum<<endl;
}
else
findSum(newsum, iEnd+1, -1);
}

used[iEnd] = 0;
findSum(sum, iEnd+1, list[iEnd]);
}

int main()
{
int sum,i;
while (cin >> listlength,listlength!=-1)
{
for (i=0,sum=0; i < listlength; i++)
{cin >> list[i];sum+=list[i];}

if(sum&1) {cout << "Sorry" << endl;continue;}

numberOfSolutions = 0;
sum=(sum>>1);DivSum=sum;
findSum(sum, 0, -1);
if (numberOfSolutions == 0) cout << "Sorry" << endl;
}
return 0;
}
• 打赏
• 举报

Leaveye 2004-08-10

• 打赏
• 举报

HelpMeACM 2004-08-10
1711是用这个DP解决

• 打赏
• 举报

gnefuil 2004-08-10

• 打赏
• 举报

3.2w+

2004-08-09 04:46