It's All In The Mind
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 505 Accepted Submission(s):
225
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
Author
zimpha
题意:求a1+a2/a1+a2+..+an的最大值,其中只给出部分的值,ax=y;
水题,保证a1,a2最大,后面的尽量小就好,需要注意的是,这是非递增数列,后面有数赋值,会影响前面的数的取值。
附上代码:
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int a[105]; 5 int _gcd(int x,int y) ///求最大公约数 6 { 7 int z; 8 if(x<y) z=x,x=y,y=z; 9 while(y) 10 { 11 z=x%y; 12 x=y; 13 y=z; 14 } 15 return x; 16 } 17 int main() 18 { 19 int T,i,j,n,m; 20 scanf("%d",&T); 21 while(T--) 22 { 23 int x,y,t=0; 24 scanf("%d%d",&n,&m); 25 for(i=1; i<=n; i++) ///初始化全为-1 26 a[i]=-1; 27 for(i=0; i<m; i++) 28 { 29 scanf("%d%d",&x,&y); 30 a[x]=y; 31 } 32 if(n==2||m==0) ///若只有两个数,或者不给任何数赋值,都能输出最大的1/1 33 { 34 printf("1/1 "); 35 continue; 36 } 37 int nn=0,mm=0,fail=0; 38 for(i=n; i>=3; i--) ///因为前面的小于等于后面的,因此从后面向前面循环 39 { 40 if(a[i]!=-1) ///如果a[i]被赋值,记录此时的a[i],并标记已出现赋值的数 41 { 42 t=a[i]; 43 fail=1; 44 mm+=t; 45 } 46 else 47 { 48 if(!fail) ///若没有出现已赋值的数,可以定义为后面的数始终为0 49 { 50 mm+=0; 51 } 52 else ///若已出现,则只能小于等于这个数 53 { 54 mm+=t; 55 } 56 } 57 } 58 if(a[1]==-1) ///a1和a2期望越大越好,若都无赋值,就都取100,若有赋值,则去赋值的数,且a2小于a1 59 { 60 nn+=100,mm+=100; 61 if(a[2]==-1) 62 nn+=100,mm+=100; 63 else 64 nn+=a[2],mm+=a[2]; 65 } 66 else 67 { 68 nn+=a[1],mm+=a[1]; 69 if(a[2]==-1) 70 nn+=a[1],mm+=a[1]; 71 else 72 nn+=a[2],mm+=a[2]; 73 } 74 int dd=_gcd(nn,mm); 75 printf("%d/%d ",nn/dd,mm/dd); 76 } 77 return 0; 78 }