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. javascript的offset、client、scroll使用方法

    offsetTop 指元素距离上方或上层控件的位置,整型,单位像素. offsetLeft 指元素距离左方或上层控件的位置,整型,单位像素. offsetWidth 指元素控件自身的宽度,整型,单位像 ...

  2. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card

    巧妙的贪心 Description Bessie the cow is a huge fan of card games, which is quite surprising, given her l ...

  3. Web安全XSS、CSRF和SQL注入

    SQL注入 SQL注入是以用户的输入作为sql语句的一部分,如后端接收到用户的请求数据后,不经过数据转义,就把数据拼接到SQL中执行,容易导致SQL的语义被篡改,即受到攻击了. 解决办法是对接收的数据 ...

  4. verdi知识点

    引用:http://blog.csdn.net/naclkcl9/article/details/5425936 1. verdi 加强了active anotation, active trace和 ...

  5. python 列表(增删改查)

    列表 :(列表可以嵌套,列表的中的元素可以为任意) 列表的创建:1.   a = [1, 2, 3] 2.   a = list([1, 2, 3]) 1.查: 索引(下标),都是从0开始 切片 .c ...

  6. ARM MMU

    关于MMU,以下几篇文章写得通俗易懂: s3c6410_MMU地址映射过程详述 追求卓越之--arm MMU详解 基于S3C6410的ARM11学习(十五) MMU来了 这里总结MMU三大作用: 1. ...

  7. 20130829ios cocos2d下拉列表的向上弹出实现(ios开发遇到的frame的问题)

    前几天仔细区分了ios中frame,bounds,center之间的关系. Frame:边框矩形,是视图相对于其父坐标的位置和大小 Bounds:边界矩形,是本地坐标系统(一般较少使用) Center ...

  8. Codeforces Round #439 (Div. 2) E. The Untended Antiquity

    E. The Untended Antiquity 题目链接http://codeforces.com/contest/869/problem/E 解题心得: 1.1,x1,y1,x2,y2 以(x1 ...

  9. 高性能MySQL(第三版)

    一.MySQL架构与历史 1.2.2 锁粒度 表锁:写锁的优先级高于读锁:写锁的请求可以插入到读锁的前面,但读锁的请求却不能插入到写锁的前面: 行级锁:行级锁只在存储引擎层实现,在服务器层没有实现: ...

  10. java foreach的实现原理

    http://blog.csdn.net/moqihao/article/details/51078464 本质是通过集合的iterator方式实现,所以再使用foreach集合,要强制判断集合的是否 ...