uvalive5810 uva12368 Candles

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>

using namespace std;

int n;
bool can[1024][101];
int a[11];
int cas=0;

bool check1(int x,int y,int z){
    int cnt[11];
    memset(cnt,0,sizeof(cnt));
    int tmp=y;
    while (tmp!=0){
            int now=tmp%10;
            tmp=tmp/10;
            if (((x&(1<<now))==0)||(cnt[now]>0)) return false;
            cnt[now]++;
    }
    tmp=z;
    while (tmp!=0){
            int now=tmp%10;
            tmp=tmp/10;
            if (((x&(1<<now))==0)||(cnt[now]>0)) return false;
            cnt[now]++;
    }
    return true;
}

bool check(int x,int y){
    if ((y<10)&&(x&(1<<y))) return true;
    for (int i=1;i<=y/2;i++){
            if (i!=(y-i)){
                    if (check1(x,i,y-i)) return true;
            }
    }
    if (y==100) return false;
    int tmpy1=y/10;
    int tmpy2=y%10;
    if (tmpy1==tmpy2) return false;
    if ((x&(1<<tmpy1))==0) return false;
    if ((x&(1<<tmpy2))==0) return false;
    return true;
}

int main(){
    memset(can,0,sizeof(can));
    for (int i=1;i<1024;i++){
            for (int j=1;j<=100;j++) can[i][j]=check(i,j);
    }
    while (1){
            scanf("%d",&n);
            if (n==0) return 0;
            cas++;
            bool flag;
            int ans=0;
            for (int i=1;i<=n;i++) scanf("%d",&a[i]);
            for (int i=1;i<1024;i++){
                    flag=true;
                    for (int j=1;j<=n;j++) if (!can[i][a[j]]) flag=false;
                    if (flag){
                            int tmp=0;
                            for (int j=9;j>=0;j--) if (i&(1<<j)) tmp=tmp*10+j;
                            if ((ans==0)||(ans>tmp)) ans=tmp;
                    }
            }
            printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}
/*
2 10 11
1 30
0
*/

  

时间: 2024-10-21 03:01:33

uvalive5810 uva12368 Candles的相关文章

codechef Birthday Candles 题解

Birthday Candles The chef is preparing a birthday cake for one of his guests, and his decided to write the age of the guest in candles on the cake. There are 10 types of candles, one for each of the digits '0' through '9'. The chef has forgotten the

HackerRank - &quot;Candles Counting&quot; - DIFFICULT

It should be categorized as 'Advanced' I think ... Anyway, Fenwick tree is the key. Editorial: https://www.hackerrank.com/challenges/candles-2/editorial. Editorial provided a very smart, Inclusion-Exclusion Principle based algorithm. However Editoria

Codeforces Gym - 101635K Blowing Candles [旋转卡壳]

题意:给你平面上一些点,求一个凸包的最短直径 思路:旋转卡壳,然后搞一下就行了可旋转卡壳求最远点差不多,cur带表的是求出的对锺点,然后与当前的直线p[i],p[i+1],求一下距离 代码: #include <bits/stdc++.h> using namespace std; const double eps = 1e-16; int sgn(double x) { if(fabs(x) < eps)return 0; if(x < 0)return -1; else ret

K - Blowing Candles

凸包 + 旋转卡壳 模本 c++ code: #include <bits/stdc++.h> using namespace std; const int N=3e5+7; struct point{ double x, y; point(){} point(double x, double y) : x(x), y(y) {} friend point operator + (const point &a, const point &b){ return point(a.x

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

题意:给定N个点,用矩形将所有点覆盖,要求矩形宽度最小. 思路:裸体,旋转卡壳去rotate即可. 最远距离是点到点:宽度是点到边. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define RC rotating_calipers using namespace std; const int maxn=400010; struct point{ ll x,y

2016/02/14 codes

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, i

Inverted sentences

And ever has it been that love knows not its own depth until the hour of separation. 除非临到了别离的时候,爱永远不会知道自己的深浅. 这是个倒装句,因为ever位于句首,句子要倒装,it是形式主语,has been是谓语,that从句是真正的主语从句,until后为状语 倒装句 在英语中,主语和谓语的语序通常是主语在前,谓语在后.这类语序被称为“自然语序”.但有时为了强调句子的某一部分,或由于其它诸如语法结构或

HDOJ 4430 Yukari&#39;s Birthday

C++高精度问题太蛋疼了.... Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2891    Accepted Submission(s): 604 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a

hdu4430之枚举+二分

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2549    Accepted Submission(s): 522 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party