uvalive5810 uva12368 Candles
题意:每组数据给出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的更多相关文章
- C - New Year Candles
Problem description Vasily the Programmer loves romance, so this year he decided to illuminate his r ...
- codeforces A. New Year Candles 解题报告
题目链接:http://codeforces.com/problemset/problem/379/A 题目意思:给定a支蜡烛(每支蜡烛可以燃烧1小时),可以在燃尽的a支蜡烛中看能组成多少组b支蜡烛, ...
- ARC 101 C - Candles
题面在这里! 显然直接枚举左端点(右端点)就OK啦. #include<cstdio> #include<cstdlib> #include<algorithm> ...
- Gym - 101635K:Blowing Candles (简单旋转卡壳,求凸包宽度)
题意:给定N个点,用矩形将所有点覆盖,要求矩形宽度最小. 思路:裸体,旋转卡壳去rotate即可. 最远距离是点到点:宽度是点到边. #include<bits/stdc++.h> #de ...
- Gym101635K Blowing Candles
题目链接:http://codeforces.com/gym/101635 题目大意: 推荐一篇文章:https://blog.csdn.net/wang_heng199/article/detail ...
- Solution -「ABC 219H」Candles
\(\mathcal{Description}\) Link. 有 \(n\) 支蜡烛,第 \(i\) 支的坐标为 \(x_i\),初始长度为 \(a_i\),每单位时间燃烧变短 \(1\) ...
- Inverted sentences
And ever has it been that love knows not its own depth until the hour of separation. 除非临到了别离的时候,爱永远 ...
- Good Bye 2013 A
A. New Year Candles time limit per test 1 second memory limit per test 256 megabytes input standard ...
- HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven ...
随机推荐
- MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具
MySQL Workbench是一款专为MySQL设计的ER/数据库建模工具.它是著名的数据库设计工具DBDesigner4的继任者.你可以用MySQL Workbench设计和创建新的数据库 ...
- easyui跨iframe属性datagrid
1.问题 如何刷新easyui父级tab页中iframe嵌套页中的datagrid? 2.解决方法 (1) parent.$("iframe[title='tabtitle']") ...
- Android stagefright与opencore对比
[转载至其它博客] http://blog.csdn.net/djy1992/article/details/9339917 1引言 Android froyo版本多媒体引擎做了变动,新添加了st ...
- java中memcached
http://www.oschina.net/code/snippet_250396_9181
- poj 1401---求N!末尾0的个数,2的个数一定比5多,观察得来,0的产生即为2*5,去找这个阶乘一行里面5的个数即可
#include<stdio.h> #include<stdlib.h> int main() { int T,N; while(scanf("%d",&a ...
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
题目来源:http://community.topcoder.com//stat?c=problem_statement&pm=12587&rd=15501 这道题目开始以为是要在无向 ...
- Spring、整合Spring+JDBC
首先引入Spring包和JDBC所使用到的包: 配置beans.xml步骤: 1.配置dataSource的属性 2.将DataSource交给DataSourceTransactionManager ...
- var_dump(php)
var_dump -- 打印变量的相关信息 描述 void var_dump ( mixed expression [, mixed expression [, ...]] ) 此函数显示关于一个或多 ...
- How to close existing connections to a DB
use master ALTER DATABASE YourDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE --do you stuff here A ...
- MySQL 中文显示乱码以及中文查询条件返回0条结果的问题解决
最近关于中文显示乱码的贴子比较多,所以也做了个总结: 可以参考一下杨涛涛版主的<各种乱码问题汇总>http://topic.csdn.net/u/20071124/08/3b7eae6 ...