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;
}

Codeforces Gym 100418K Cards 组合数学的更多相关文章

  1. Codeforces Gym 100418K Cards 暴力打表

    CardsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  2. codeforce Gym 100418K Cards (概率,数学)

    题意:麦田的故事,n张牌,取x张牌,记住前x张牌最大的值m,继续往后取,遇到第一张比m大的牌就停下来.求一个x使得最后的牌在整副牌里是最大的期望最大. 假设最大的牌是A,A在各种位置出现的概率就是相等 ...

  3. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  4. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  5. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  6. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  7. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  8. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

  9. CodeForces Gym 100213F Counterfeit Money

    CodeForces Gym题目页面传送门 有\(1\)个\(n1\times m1\)的字符矩阵\(a\)和\(1\)个\(n2\times m2\)的字符矩阵\(b\),求\(a,b\)的最大公共 ...

随机推荐

  1. shell脚本,如何监控mysql数据库。

    [root@localhost wyb]# cat jkmysql #!/bin/bash status=`/etc/init.d/mysqld status|grep running|wc -l` ...

  2. iterator方法和for方法 遍历数据库user表结果集ResultSet

    首先,把连接数据库的语句做成工具类,因为会一直用到这几句 代码如下: package com.swift.jdbc; import java.sql.Connection; import java.s ...

  3. 优化mysql查询

    mysql提供了一个特别的explain语句,用来分析查询语句的性能 : explain select ... 1.在所有用于where,order by,group by的列上添加索引 创建索引 添 ...

  4. (1) zabbix进程构成

    进程介绍 zabbix_agentd客户端守护进程,此进程收集客户端数据,例如cpu负载.内存.硬盘使用情况等 zabbix_getzabbix工具,单独使用的命令,通常在server或者proxy端 ...

  5. perl学习笔记之:正则表达式

     Perl 中的正则表达式 正则表达式的三种形式  正则表达式中的常用模式  正则表达式的 8 大原则          正则表达式是 Perl 语言的一大特色,也是 Perl 程序中的一点难点,不过 ...

  6. centos7.2安装redis与配置(史上最全)

    学习了php已经快三年了,一直是在盲目的忙,也没整理下笔记,今天整理一下 分享下安装redis的方法 #首先去redis官网去下载   http://www.redis.cn/download.htm ...

  7. Codeforces Round #439 (Div. 2) B. The Eternal Immortality

    B. The Eternal Immortality 题目链接http://codeforces.com/contest/869/problem/B 解题心得:题意就是给出a,b,问(a!)/(b!) ...

  8. Python自动发送邮件(可带附件)

    Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件,smtplib负责发送邮件 ...

  9. 给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

    /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x ...

  10. wordpress无法登录的解决方法

    使用wordpress建站的朋友可能会遇到wordpress管理密码,有时甚至是正确的密码,但是多次尝试输入都无法登录,并且输入用户名和输入电子邮件都无法获取密码,遇到这种情况怎么办,本文教你如何处理 ...