UVA11895 Honorary Tickets

传送


因为每个人是最优策略,所以他会选中奖概率最高的箱子。

要知道当前哪一个箱子中奖概率最高,就应该求出一个人抽完奖后这个箱子期望有几个信封获奖,即(l_i-frac{l_i}{t_i})个。

如果这道题不要输出分数的话,用单调队列模拟(k)次就好了。

加上分数的条件,就是把(l_i)看成(frac{a_i}{b_i}),则抽完后,就是(a_i'=a_i(t_i-1),b_i'=b_it_i)。所以单调队列里面我们维护(t_i,a_i,b_i)就可以了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
//const int maxn = ;
In ll read()
{
	ll ans = 0;
	char ch = getchar(), las = ' ';
	while(!isdigit(ch)) las = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(las == '-') ans = -ans;
	return ans;
}
In void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}

int n, K;
struct Node
{
	ll t, a, b;
	In bool operator < (const Node& oth)const
	{
		return a * oth.b * oth.t < oth.a * b * t;
	}
};
priority_queue<Node> q;

In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}

int main()
{
//	MYFILE();
	int T = read();
	while(T--)
	{
		while(!q.empty()) q.pop();
		n = read(), K = read();
		for(int i = 1; i <= n; ++i)
		{
			Node tp; tp.t = read(), tp.a = read(), tp.b = 1;
			q.push(tp);
		}
		for(int i = 1; i < K; ++i)
		{
			Node tp = q.top(); q.pop();
			ll a = tp.a * (tp.t - 1), b = tp.b * tp.t, g = gcd(a, b);
			q.push((Node){tp.t, a / g, b / g});
		}
		Node tp = q.top();
		ll a = tp.a, b = tp.b * tp.t, g = gcd(a, b);
		write(a / g), putchar('/'), write(b / g), enter;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/mrclr/p/14601749.html