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分析: 令 x=a1+a2,y=a3+a4+⋯+an , 那么 a1+a2+⋯+ana1+a2=x+yx=1−x+yy . 对于定值 y , 显然 x 越大越好, 对于定值 x , 显然 y 越小越好. 于是按照 a1 和 a2 尽量大, 其他元素尽量小的策略填数就好了.注意该数列为递减数列,所以要控制各个元素的值,因此引入 赋值区间(例如,输入测试样例 1 6 3 1 1 3 3 6 1,给a2,a4,a5赋值,a2=1,a4=1,a5=1,此处赋值区间为[1,1],[4,5]);AC代码如下:<pre name="code" class="cpp">#include "iostream" using namespace std; int gcd(int a, int b); int main(int argc, char* argv[]) { int t,i,j; int n,m; int son,mon; //分子,分母 int flag,ff,L; //L为赋值区间的左界 int a[110],b[110],c[110]; cin>>t; while(t--) { flag=0;//标记a1是否为定值 ff=0; //标记a2是否为定值 L=3;//赋值区间左界初始值为3,区间【1,2】的元素可直接确定 son=mon=0; cin>>n>>m; for(i=1;i<=m;i++) { cin>>c[i]>>b[i]; if (c[1]==1) { flag=1;//a1为定值b[1] a[1]=b[1]; } if (c[i]==2) { ff=1;//a2为定值b[i] a[2]=b[i]; } if (c[i]>2) { mon+=b[i]; for(j=L;j<c[i];j++)//对赋值区间的元素赋值 { a[j]=b[i]; mon+=a[j]; } L=c[i]+1; } } if (flag==0) a[1]=100;//直接确定a1,a2 if (ff==0) a[2]=a[1]; son+=a[1]+a[2]; mon+=a[1]+a[2]; int s=gcd(son,mon);//gcd函数求两个数的最大公因数 if (s==0) //约分 { cout<<son<<"/"<<mon<<endl; } else { cout<<son/s<<"/"<<mon/s<<endl; } } return 0; } int gcd(int a, int b) { if (a<b) { int temp=a; a=b; b=temp; } if (b==0) { return b; } int sum=a%b; while(sum!=0) { a=b; b=sum; sum=a%b; } return b; }