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

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  p q  are integers,  q>0 .
 

Sample Input
  
  
2 2 0 3 1 3 1
 

Sample Output
  
  
1/1 200/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;
}


a i



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值