比较不错的一个题,关键是理解状态转移

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 55
using namespace std;
int m,ans; int num[maxn],vis[maxn];
bool cnt[];
int scale[]; bool dfs(int cur)
{
// printf("%d %d\n",cur,ans);
if(cur==ans-)
{
for(int i=; i<m-; i++)
if(!vis[i])
return ;
return ;
} for(int i=; i<cur; i++)
{
for(int j=; j<m-; j++)
{
if(vis[j])continue;
int dd=scale[i]+num[j];
vis[j]=;
if(dd<=scale[cur-])continue;
if(dd>=num[m-])continue; scale[cur]=dd; queue<int>q;
while(!q.empty())q.pop(); for(int k=; k<cur; k++)
{
int tmp=dd-scale[k];
if(cnt[tmp]&&!vis[cnt[tmp]])
{
vis[cnt[tmp]]=;
q.push(cnt[tmp]);
}
} int tmp=num[m-]-scale[cur];
if(cnt[tmp]&&!vis[cnt[tmp]])
{
vis[cnt[tmp]]=;
q.push(cnt[tmp]);
} if(dfs(cur+))return ;
while(!q.empty())
{
vis[q.front()]=;
q.pop();
}
}
}
return ;
} int main()
{
int n,ca=;
while(scanf("%d",&n)&&n)
{
for(int i=; i<n; i++)
scanf("%d",&num[i]);
sort(num,num+n);
m=unique(num,num+n)-num; ans=;
while(ans*(ans-)/<m)
ans++;
memset(cnt,,sizeof cnt);
memset(vis,,sizeof vis);
for(int i=; i<m; i++)
cnt[num[i]]=i; scale[]=;
while(!dfs())
ans++;
scale[ans-]=num[m-];
printf("Case %d:\n",ca++);
printf("%d\n",ans); for(int i=;i<ans;i++)
printf("%d ",scale[i]);
puts("");
}
return ;
}

uva 1377的更多相关文章

  1. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  2. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  3. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  4. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  5. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  6. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  7. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  8. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. UVA - 11584 Partitioning by Palindromes[序列DP]

    UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is t ...

随机推荐

  1. html5 canvas 圆形抽奖的实例

    年底没啥,抽空学习了canvas,写了个html5抽奖的代码,造了个轮子,有用的童鞋可以拿走. 其中,canvas.onclick触发抽奖行为,概率可以在core.lottery()函数上添加,美化也 ...

  2. 20141016--for 兔子

    Console.Write("请输入月数:"); int m =int.Parse(Console.ReadLine()); ;//成兔对数ct ;//小兔对数xt ;//幼兔对数 ...

  3. CSS的浮动和清除

    CSS浮动和清除 什么是浮动? 在实现效果上,让元素浮起来(飘起来),动起来(向左或向右移动) 浮动本质:就是将两个块元素同存一行. float 取值:主要是对浮动的方向进行控制 left:让元素向左 ...

  4. java计时器

    //用于计时,要求xx秒后开始发送短信,短信之间间隔xx秒 public static Date dateOperateBySecond(Date date,int second){ Calendar ...

  5. 使用Script元素发送JSONP请求

    // 根据指定URL发送一个JSONP请求 //然后把解析得到的相应数据传递给回调函数 //在URL中添加一个名为jsonp的查询参数,用于指定该请求的回调函数的名称 function getJSON ...

  6. java学习笔记_GUI(1)

    import javax.swing.*; public class Gui { public static void main(String[] args) { JFrame frame = new ...

  7. python(四)数据持久化操作 文件存储

    1.写入 导入pickle包 然后组织一个列表my_list,保存为pkl格式,可以是任意格式 在磁盘下回出现一个保存的文件 2.读取

  8. spring-cloud-hystrix熔断

    依赖pom <dependencyManagement> <dependencies> <dependency> <groupId>org.spring ...

  9. php安全设定

    nginx: 在配置文件nginx.conf的http段里加入  server_tokens off;  HTTPD.CONFTraceEnable Off <Directory ~ " ...

  10. ARM-Linux S5PV210 UART驱动(5)----串口的open操作(tty_open、uart_open)

    串口驱动初始化后,串口作为字符驱动也已经注册到系统了,/dev目录下也有设备文件节点了. 那接下来uart的操作是如何进行的呢? 操作硬件之前都是要先open设备,先来分析下这里的open函数具体做了 ...