Codeforces Gym 100418K Cards 组合数学

本文介绍了一道名为CardsTimeLimit的问题,通过暴力打表的方法寻找最优策略中的参数X,以最大化赢得游戏的概率。该文提供了详细的算法实现,并附上了完整的代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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\n", Bel[i], Num[i], Prod[i]);
    }
    for (int i = 1; i <= n; i++)
    {
        int pos = Get(a[i]);
     //   printf("%d\n", 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\n", 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\n", ans);
    }
    printf("%I64d\n", ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/HarryGuo2012/p/4711980.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值