2016 Multi-University Training Contest 2(2016多校训练第二场)1009

题目:

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

 

题目大意:有一个数列a,但是很多数字都遗失了,只记得其中一些数字,给出数列的长度,并给出记得的数字信息(数列的第几个是多少),要求求出(a1+a2)/(a1+a2+........an)的最大值(约分后的结果)

解题思路:因为(a1+a2)<(a1+a2+........an),因此该数值一定是一个小于1的真分数,要使分数尽可能大则应该使分子尽可能大同时分母尽可能小,又根据题目给出的限定条件发现,0<=ai<=100,因此a1和a2应该尽可能从100往下取,而其他值则应该尽可能从0往上取,题目给出的另外一个条件是该数列是一个单调递减的数列,ai>=ai+1,因此可以从数列后部往前遍历,某位置的元素等于后方的不为零的值,这样可以得到满足要求的数组,然后求和,非常重要的一点是要记得进行约分,最后输出约分后的结果

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
    int t;
    int n,m;
    int x,y;
    int ansnum=0;
    int ansden=0;
    int num[105];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(num,0,sizeof(num));
        num[0]=100;
        num[1]=100;
        while(m--)
        {
            scanf("%d%d",&x,&y);
            num[x-1] = y;
        }
        if(num[0]<num[1])num[1] = num[0];
        for(int i=n-2;i>=2;i--)
        {
            if((num[i]==0)&&(num[i+1]!=0))num[i] = num[i+1];
        }
        ansnum=ansden=0;
        ansnum += (num[0]+num[1]);
        for(int i=0;i<n;i++)
        {
            ansden+=num[i];
        }
      /*  for(int i=0;i<n;i++)
        {
            cout<<num[i]<<" ";
        }
        cout<<endl;*/
        for (int i = 2; i <= ansnum; i++)
        {
            if (ansnum % i == 0 && ansden % i == 0)
            {
                ansnum /= i;
                ansden /= i;
                i--;
                continue;
            }
        }
        printf("%d/%d\n",ansnum,ansden);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值