您的位置:首页 > 其它

1010(stamps),acm.pku.edu.cn

2010-09-16 00:55 316 查看
http://acm.pku.edu.cn/JudgeOnline/problem?id=1010

总结:有一个清晰的思路和好的算法,加上正确的代码,就是要好多了

思路:1.、优化:同种面额的邮票可以缩减到四张,我是把邮票按面额递增排列。

2.、构建数组时,第一个位置赋0,方便后面的搜索

哎,主要还是参考了别人的讲义,不然哪有楞个轻松哦。

代码如下:

#include <iostream>

using namespace std;

const int MAX = 100;

int stamp[MAX],lenStamp,cur,solution[4];

void qSort(int l,int h)

{

if(h < l+ 2) return;

int e = h, p = l;

while(l < h){

while(l < e && stamp[l] <= stamp[p]) l++;

while(h > p && stamp[h] >= stamp[p]) h--;

if(l < h)

swap(stamp[l],stamp[h]);

}

swap(stamp[h],stamp[p]);

qSort(p,h);

qSort(l,e);

}

void del()

{

int i,j, num = 1;

for(i = 1,j=1; i < lenStamp; i++)

if(stamp[i] == stamp[i-1] && num < 4){

stamp[j++] = stamp[i];

num++;

}

else if(stamp[i] != stamp[i-1]){

stamp[j++] = stamp[i];

num = 1;

}

lenStamp = j;

// printf("%d/n",lenStamp);

}

void ask()

{

int i,j,k,m,site[4];

int type,total,val;

bool statu = false;

for(i = 0; i < lenStamp; i++)

for(j = i; j < lenStamp; j++)

for(k = j; k < lenStamp; k++)

for(m = k; m < lenStamp; m++){

if(stamp[i] + stamp[j] + stamp[k] + stamp[m] != cur)

continue;

type = total = val = 0;

if(i){total++;type++;val=stamp[i];}

if(j){

total++;

if(j != i)

type++;

if(stamp[j] > val)

val = stamp[j];

}

if(k){

total++;

if(k != j)

type++;

if(stamp[k] > val)

val = stamp[k];

}

if(m){

total++;

if(m != k)

type++;

if(stamp[m] > stamp[k])

val = stamp[m];

}

if(type > solution[0]){

solution[0] = type;

solution[1] = total;

solution[2] = val;

solution[3] = 0;

statu = true;

}

else if(type == solution[0] && total < solution[1]){

solution[1] = total;

solution[2] = val;

solution[3] = 0;

statu = true;

}

else if(type == solution[0] && total == solution[1] && val > solution[2]){

solution[2] = val;

solution[3] = 0;

statu = true;

}

else if(type == solution[0] && total == solution[1] && val == solution[2])

solution[3] = 1;

if(statu){

site[0] = i;

site[1] = j;

site[2] = k;

site[3] = m;

statu = false;

}

}

if(solution[0] == 0)

printf("%d ---- none/n",cur);

else if(solution[3])

printf("%d (%d): tie/n",cur,solution[0]);

else{

printf("%d (%d):",cur,solution[0]);

for(i=0; i < 4; i++)

if(site[i])

printf(" %d",stamp[site[i]]);

printf("/n");

}

}

int main()

{

int temp;

while(scanf("%d",&temp) != EOF){

memset(stamp,0,sizeof(int));

lenStamp = 1;

while(temp){

stamp[lenStamp++] = temp;

scanf("%d",&temp);

}

qSort(1,lenStamp - 1);

del();

scanf("%d",&cur);

while(cur){

solution[0] = 0; // the number of different stamp types

solution[1] = 4; // the total stamps

solution[2] = 0; // the highest single-value

solution[3] = 0; // tie = false;

ask();

scanf("%d",&cur);

}

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: