HDU 5742 It's All In The Mind(思维水)

来源:互联网 发布:dede免费源码站 编辑:程序博客网 时间:2024/05/22 06:48
Problem Description
Professor Zhang has a number sequence a1,a2,...,an. However, the sequence is not complete and some elements are missing. Fortunately, Professor Zhang remembers some properties of the sequence:

1. For every i{1,2,...,n}0ai100.
2. The sequence is non-increasing, i.e. a1a2...an.
3. The sum of all elements in the sequence is not zero.

Professor Zhang wants to know the maximum value of a1+a2ni=1ai among all the possible sequences.
 

Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first contains two integers n and m (2n100,0mn) -- the length of the sequence and the number of known elements.

In the next m lines, each contains two integers xi and yi (1xin,0yi100,xi<xi+1,yiyi+1), indicating that axi=yi.
 

Output
For each test case, output the answer as an irreducible fraction "p/q", where pq are integers, q>0.
 

Sample Input
22 03 13 1
 

Sample Output
1/1200/201
 


题意:一个长度n的非升序的未知序列a1,a2,a3...an,元素的值范围为[0,100],题目会给定一些已确定元素的值,除给定元素随意构造其他元素的值,使得a1+a2ni=1ai的值最大,求这个最大值,用最简分数表示,不必去分母。


设A = a1 + a2,B = a3 + a4 + ... + an,则题中公式可以写成

A
 A + B


的样子,那么要使得这个值最大,那么我们分子分母同除以A转换一下就发现让A尽可能大,B尽可能小,整个式子的值就越大,由于序列是非升序,我们只需在保证序列非升序和不破坏已知元素的前提下,a1~a2部分尽量填大数,a3~an部分尽量填小数即可,代码如下:


#include <bits/stdc++.h>using namespace std;int s[110];int main(){int t,n,m,x,y;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);memset(s,0,sizeof(s));s[1] = s[2] = 100;for(int i = 0 ; i < m ; i++){scanf("%d %d",&x,&y);s[x] = y;}s[2] = s[2] == 100 ? s[1] : s[2];int a = s[1] + s[2];int b = a, pre = 0;for(int i = n ; i >= 3 ; i--){if(!s[i]) s[i] = pre;else pre = s[i];b += s[i];}int g = __gcd(a,b);printf("%d/%d\n",a / g,b / g);}    return 0;}


ai



0 0
原创粉丝点击