题目:
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}, 0≤ai≤100.
2. The sequence is non-increasing, i.e. a1≥a2≥...≥an.
3. The sum of all elements in the sequence is not zero.
Professor Zhang wants to know the maximum value of a1+a2∑ni=1ai among all the possible sequences.
1. For every i∈{1,2,...,n}, 0≤ai≤100.
2. The sequence is non-increasing, i.e. a1≥a2≥...≥an.
3. The sum of all elements in the sequence is not zero.
Professor Zhang wants to know the maximum value of a1+a2∑ni=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 (2≤n≤100,0≤m≤n) -- the length of the sequence and the number of known elements.
In the next m lines, each contains two integers xi and yi (1≤xi≤n,0≤yi≤100,xi<xi+1,yi≥yi+1), indicating that axi=yi.
The first contains two integers n and m (2≤n≤100,0≤m≤n) -- the length of the sequence and the number of known elements.
In the next m lines, each contains two integers xi and yi (1≤xi≤n,0≤yi≤100,xi<xi+1,yi≥yi+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;
}