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;
}
总结:有一个清晰的思路和好的算法,加上正确的代码,就是要好多了
思路: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;
}
相关文章推荐
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2305 大数的b进制数 取模
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2409 polya或者burnside
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2891 欧几里德线性同余方程
- http://acm.pku.edu.cn/JudgeOnline/problem?id=3468 成段更新,区间求和(要用long long)
- http://acm.pku.edu.cn/JudgeOnline/problem?id=3667 区间更新+求满足长短的最左区间
- http://acm.pku.edu.cn/JudgeOnline/
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2528 成段更新,区间统计颜色的种类(离散化处理线段)
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2480 欧拉
- Problems by Topic @ acm.pku.edu.cn
- http://acm.pku.edu.cn/JudgeOnline/problem?id=1808 二次剩余式
- http://acm.pku.edu.cn/JudgeOnline/problem?id=1284 (原根)
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2407 很水的欧拉
- http://acm.hdu.edu.cn/showproblem.php?pid=1010
- http://acm.pku.edu.cn/JudgeOnline/problem?id=2777 成段更新,区间统计颜色的种类 (用的位运算, 可是写的很搓)
- http://acm.pku.edu.cn/JudgeOnline/problem?id=1131 八进小数转十进制小数
- http://acm.hdu.edu.cn/showproblem.php?pid=3336
- Max Sum http://acm.hdu.edu.cn/showproblem.php?pid=1003
- http://acm.hdu.edu.cn/showproblem.php?pid=1203(背包问题)
- http://acm.hdu.edu.cn/showproblem.php?pid=1238
- nefu 84 http://acm.nefu.edu.cn/test/problemshow.php?problem_id=84