Codeforces Gym 100418K Cards 组合数学

Cards
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86686#problem/K

Description

You have N cards with different numbers on them. Your goal is to find a card with a maximal number. At the beginning all cards are put into the hat. You start getting them one by one and look at the numbers on them. After each card you can select it and stop the process. If it is really the card with the maximal number you win otherwise you lose. Also you can skip the current card and continue process. Fortunately you have a friend who helps with a good strategy: you pull X cards and memorize their values. Then you continue the process and select as answer the first card with value greater than the maximal value you memorized. Unfortunately you don't know the value of Xthat maximizes you chances of winning. Your task is to find X.

Input

Single line containing one number: N (5 ≤ N ≤ 100).

Output

Single line containing one number: value of X that maximizes you chances of winning.

Sample Input

5

Sample Output

2

题解

 这道题可以暴力打表,而且貌似效果比较好。。我的做法是枚举X,再枚举X中最大的数a,那么X对应的概率应当是sum(C(a-1,x-1)/(C(n,x)*(n-a), x<=a&&a<n),这样统计好后,取最大的X即可,详见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 1000000005
#define eps 1e-10
#define PI acos(-1.0)
#define K (0.017453292519943295769236907684886l)
#define LL long long
#define ULL unsigned long long

using namespace std;

const int maxn = 100005;

int a[maxn], n, m;

struct Node
{
    int val, pos;
}B[maxn];

bool cmp(const Node &x, const Node &y)
{
    return x.val < y.val;
}

LL Bel[maxn], Num[maxn], Prod[maxn];

int Get(int x)
{
    if (x > B[m].val) return m;
    int l = 1, r = m, pos = 0;
    while(l <= r)
    {
        int mid = (l + r) / 2;
        if (x >= B[mid].val)
        {
            pos = mid; l = mid + 1;
        }
        else r = mid - 1;
    }
    return pos;
}

int main()
{
    LL ans = 0;
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for (int i = 1; i <= m; i++)
        {
            scanf("%d", &B[i].val);
            B[i].pos = i;
        }
    sort(B + 1, B + 1 + m, cmp);
    for (int i = 1; i <= m; i++)
    {
        Bel[i] = Bel[i - 1] + B[i].pos;
        Num[i] = Num[i - 1] + B[i].val;
        Prod[i] = Prod[i - 1] + 1LL * B[i].pos * B[i].val;
      //  printf("%I64d %I64d %I64d
", Bel[i], Num[i], Prod[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        int pos = Get(a[i]);
     //   printf("%d
", pos);
        ans = ans + 1LL * pos * i * a[i] + Prod[pos];
        ans = ans - 1LL * a[i] * Bel[pos] - 1LL * i * Num[pos];
        //
      // if (i == 1) printf("%I64d
", ans);
        LL temp = 1LL * (m - pos) * i * a[i] + Prod[m] - Prod[pos];
        temp = temp - 1LL * a[i] * (Bel[m] - Bel[pos]) - 1LL * i * (Num[m] - Num[pos]);
        ans -= temp;
      //  if (i == 1) printf("%I64d
", ans);
    }
    printf("%I64d
", ans);
    return 0;
}

 

原文地址:https://www.cnblogs.com/HarryGuo2012/p/4711980.html