您的位置:首页 > 其它

codeforces round #253 div1

2014-07-10 17:04 239 查看
A. Borya and Hanabi

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game.

Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding n cards. The game is somewhat complicated by the fact
that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many
cards of this type he has).

The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints.

A color hint goes like that: a player names some color and points at all the cards of this color.

Similarly goes the value hint. A player names some value and points at all the cards that contain the value.

Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value.

Input

The first line contains integer n (1 ≤ n ≤ 100) —
the number of Borya's cards. The next line contains the descriptions of n cards. The description of each card consists of exactly two characters. The first
character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in.

Output

Print a single integer — the minimum number of hints that the other players should make.

Sample test(s)

input
2
G3 G3


output
0


input
4
G4 R4 R3 B3


output
2


input
5
B1 Y1 W1 G1 R1


output
4


Note

In the first sample Borya already knows for each card that it is a green three.

In the second sample we can show all fours and all red cards.

In the third sample you need to make hints about any four colors.

题意:有25种类型的卡片,5种颜色5种价值组成。问给定n张牌,问经过几次提示可以知道牌的堆叠情况。每次提示只能指出一种颜色的所有牌或者具有某种价值的所有牌。

思路:我们发现提示最多10次就可以知道牌的堆叠情况。那么我们给提示编码,状压枚举给出哪些指令看能不能区分所有的牌,求出最小值。

具体点说,我们首先将每种牌留下一张,用mark数组表示提示的值,高两位为颜色,低两位为价值,只有当所有的牌mark数组的值不同才能区分出所有,详见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int MAXN=100+50;
int n,cnt,num;
int vis[MAXN],mp[MAXN*MAXN],card[MAXN],mark[MAXN];
void init()
{
    cnt=num=0;
    memset(vis,0,sizeof(vis));
    memset(mp,0,sizeof(mp));
    memset(mark,0,sizeof(mark));
}
int main()
{
    //freopen("text.txt","r",stdin);
    scanf("%d",&n);
    init();
    int Min=50;char s[5];
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        if(!vis[s[0]])
            vis[s[0]]=++cnt;
        if(!vis[s[1]])
            vis[s[1]]=++cnt;
        int x=vis[s[0]]*100+vis[s[1]];
        if(!mp[x])
            mp[x]=1,card[++num]=x;
    }
    for(int i=0;i<(1<<cnt);i++)
    {
        int res=0;
        for(int j=0;j<=cnt;j++)
            if((1<<j) & i)
            {
                res++;
                for(int k=1;k<=num;k++)
                {
                    if(card[k]/100 == j+1) mark[k]+=100*(j+1);
                    if(card[k]%100 == j+1) mark[k]+=j+1;
                }

            }
        sort(mark+1,mark+1+num);
        int flag=1;
        for(int l=2;l<=num;l++)
            if(mark[l] == mark[l-1])
            {
                flag=0;
                break;
            }
        memset(mark,0,sizeof(mark));
        if(flag)
            Min=min(Min,res);
    }
    printf("%d\n",Min);
}


B. Andrey and Problem

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Andrey needs one more problem to conduct a programming contest. He has n friends who are always willing to help. He can ask some of them to come up with
a contest problem. Andrey knows one value for each of his fiends — the probability that this friend will come up with a problem if Andrey asks him.

Help Andrey choose people to ask. As he needs only one problem, Andrey is going to be really upset if no one comes up with a problem or if he gets more than one problem from his friends. You need to choose such a set of people that maximizes the chances of
Andrey not getting upset.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) —
the number of Andrey's friends. The second line contains n real numbers pi(0.0 ≤ pi ≤ 1.0) —
the probability that the i-th friend can come up with a problem. The probabilities are given with at most 6 digits after decimal point.

Output

Print a single real number — the probability that Andrey won't get upset at the optimal choice of friends. The answer will be considered valid if it differs from the correct one by at most 10 - 9.

Sample test(s)

input
40.1 0.2 0.3 0.8


output
0.800000000000


input
20.1 0.2


output
0.260000000000


Note

In the first sample the best strategy for Andrey is to ask only one of his friends, the most reliable one.

In the second sample the best strategy for Andrey is to ask all of his friends to come up with a problem. Then the probability that he will get exactly one problem is 0.1·0.8 + 0.9·0.2 = 0.26.



#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100+50;
int n;
double a[MAXN];
int cmp(double a,double b)
{
    return a>b;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%lf",&a[i]);
    sort(a+1,a+n+1,cmp);
    double Max=0;
    double p0=1,p1=0;
    for(int i=1;i<=n;i++)
    {
        p1*=(1-a[i]);
        p1+=p0*a[i];
        p0*=(1-a[i]);
        Max=max(Max,p1);
    }
    printf("%.12f\n",Max);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: