题意:每组数据给出n个数,每个数在1-100,问组成这些数的蜡烛的权值的最小值。权值=把选的蜡烛从大到小排列组成的数

组成方式:比如有1 3两个蜡烛 可以组成13(1和3)或4(1+3) 只有一个加号可以用

解:位运算记录状态,can[i][j]表示在i的二进制所记录的状态下能不能组成j

can直接预处理出来,分类讨论 用一个数表示 还是用两个数表示 check的时候都在100以内 直接枚举

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std; int n;
bool can[][];
int a[];
int cas=; bool check1(int x,int y,int z){
int cnt[];
memset(cnt,,sizeof(cnt));
int tmp=y;
while (tmp!=){
int now=tmp%;
tmp=tmp/;
if (((x&(<<now))==)||(cnt[now]>)) return false;
cnt[now]++;
}
tmp=z;
while (tmp!=){
int now=tmp%;
tmp=tmp/;
if (((x&(<<now))==)||(cnt[now]>)) return false;
cnt[now]++;
}
return true;
} bool check(int x,int y){
if ((y<)&&(x&(<<y))) return true;
for (int i=;i<=y/;i++){
if (i!=(y-i)){
if (check1(x,i,y-i)) return true;
}
}
if (y==) return false;
int tmpy1=y/;
int tmpy2=y%;
if (tmpy1==tmpy2) return false;
if ((x&(<<tmpy1))==) return false;
if ((x&(<<tmpy2))==) return false;
return true;
} int main(){
memset(can,,sizeof(can));
for (int i=;i<;i++){
for (int j=;j<=;j++) can[i][j]=check(i,j);
}
while (){
scanf("%d",&n);
if (n==) return ;
cas++;
bool flag;
int ans=;
for (int i=;i<=n;i++) scanf("%d",&a[i]);
for (int i=;i<;i++){
flag=true;
for (int j=;j<=n;j++) if (!can[i][a[j]]) flag=false;
if (flag){
int tmp=;
for (int j=;j>=;j--) if (i&(<<j)) tmp=tmp*+j;
if ((ans==)||(ans>tmp)) ans=tmp;
}
}
printf("Case %d: %d\n",cas,ans);
}
return ;
}
/*
2 10 11
1 30
0
*/
  

uvalive5810 uva12368 Candles的更多相关文章

  1. C - New Year Candles

    Problem description Vasily the Programmer loves romance, so this year he decided to illuminate his r ...

  2. codeforces A. New Year Candles 解题报告

    题目链接:http://codeforces.com/problemset/problem/379/A 题目意思:给定a支蜡烛(每支蜡烛可以燃烧1小时),可以在燃尽的a支蜡烛中看能组成多少组b支蜡烛, ...

  3. ARC 101 C - Candles

    题面在这里! 显然直接枚举左端点(右端点)就OK啦. #include<cstdio> #include<cstdlib> #include<algorithm> ...

  4. Gym - 101635K:Blowing Candles (简单旋转卡壳,求凸包宽度)

    题意:给定N个点,用矩形将所有点覆盖,要求矩形宽度最小. 思路:裸体,旋转卡壳去rotate即可. 最远距离是点到点:宽度是点到边. #include<bits/stdc++.h> #de ...

  5. Gym101635K Blowing Candles

    题目链接:http://codeforces.com/gym/101635 题目大意: 推荐一篇文章:https://blog.csdn.net/wang_heng199/article/detail ...

  6. Solution -「ABC 219H」Candles

    \(\mathcal{Description}\)   Link.   有 \(n\) 支蜡烛,第 \(i\) 支的坐标为 \(x_i\),初始长度为 \(a_i\),每单位时间燃烧变短 \(1\) ...

  7. Inverted sentences

    And ever has it been that love knows not its own depth until the hour of separation.  除非临到了别离的时候,爱永远 ...

  8. Good Bye 2013 A

    A. New Year Candles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

随机推荐

  1. Android_高仿建行圆形菜单

    转:http://blog.csdn.net/lmj623565791/article/details/43131133 1.概述 今天打开建行看存款,一看伤心欲绝,再看:我擦,这个圆形菜单挺炫.于是 ...

  2. smarty的简单介绍

    我们可以看到,smarty的程序部分实际就是符合php语言规范的一组代码,我们依次来解释一下:1./**/语句: 包含的部分为程序篇头注释.主要的内容应该为对程序的作用,版权与作者及编写时间做一个简单 ...

  3. Javascript禁止父元素滚动条滚动, pc、移动端均有效

    在网页中经常会遇到这样的场景, 网页比较长有滚动条, 然后网页内的某个内容块里面的内容也比较长, 也具有滚动条.当鼠标移到内容块中使用滚动条来滚动查看内容到达底部或头部的时候,父元素的滚动条也就开始滚 ...

  4. 练习3.20 a 将中缀表达式转换为后缀表达式

    //将中缀表达式转换为后缀表达式 int main() { ; ]={,,,,,,,}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); ) { ...

  5. Spring、Hello Spring

    1.引入Spring jar包 2.新建一个Person 接口和Person Bean public interface PersonIService { public void helloSprin ...

  6. MySQL- 锁机制及MyISAM表锁

    锁是计算机协调多个进程或线程并发访问某一资源的机制.在数据库中,除传统的计算资源(如CPU.RAM.I/O等)的争用以外,数据也是一种供许 多用户 共享的资源.如何保证数据并发访问的一致性.有效性是所 ...

  7. List的方法和属性 方法或属性 作用

    List的方法和属性 方法或属性 作用 Capacity 用于获取或设置List可容纳元素的数量.当数量超过容量时,这个值会自动增长.您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以 ...

  8. CSS Unicode 编码

    CSS 中文字体 Unicode 编码表 在 CSS 中设置字体名称,直接写中文是可以的.但是在文件编码(GB2312.UTF-8 等)不匹配时会产生乱码的错误. 为此,在 CSS 直接使用 Unic ...

  9. SqlServer mssql 按月统计所有部门

    以订单统计为例,前端展示柱状图(Jquery统计): 表及主要字段描述如下:表名:Orders1.日期CreateTime2.金额Amount3.用户UserID 情况一:根据部门统计某一年每月销量( ...

  10. NavigationBar--修改返回按钮的标题

    UIBarButtonItem *backItem = [[[UIBarButtonItem alloc] init] autorelease]; backItem.title = @"返回 ...