首页 > 代码库 > HDU5742 It's All In The Mind(思维题,水题)
HDU5742 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}, 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
Sample Input 2 2 0 3 1 3 1 Sample Output 1/1 200/201
题意:
有一个数字序列,给出数字序列的性质是:
1.每个数最大为100,最小为0
2.序列非递增
3.序列总和不为0
求出这个数列的最大值
样例:第一行是T,表示T组测试样例,第二行是n,m。表示序列的长度和已知数的个数。下m行为xy,表示axi=yi
第一组 长度为2,已知0个。则最大值为(100+100)/(100+100)或者(1+1)/(1+1)最大值为1。
第二组长度为3,已知1个。已知的这一个为a3=1 。则最大值为(100+100)/(100+100+1)
思路:
想求(a1+a2)/(a1+a2+...+an) 的最大值,只要让分母尽可能大,分子尽可能小即可。
有两种情况,第一种为已知a1,a2。让不知道的数取最小值(不是0,因为要保证序列非递增)。
第二种情况,不知道a1或者a2。让a1和a2取最大值(也要注意序列非递增)。
最后用GCD求出a1+a2与序列和的最大公因数,然后取分数。
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int gcd(int a,int b)//求最大公约数 { if(b==0) return a; return gcd(b,a%b); } int main() { int a[105],t,m,n; scanf("%d",&t); while(t--) { int i,j,x,y,sum1=0,sum2=0,ans=0; int w=0; scanf("%d%d",&n,&m); for(i=1; i<=n; i++) a[i]=-1;//初始化数组为-1,因为数组从0-100,所以可以用-1来表示 for(i=1; i<=m; i++) { scanf("%d%d",&x,&y); a[x]=y;//将已知条件赋值 } for(i=n; i>=3; i--)//从3-n尽量取最小 { if(a[i]==-1)//如果后面没有值,说明没有非递增限制,直接赋值为w,w初始为0 a[i]=w; else//如果已经有值,则值前面的赋值为这个值。 { w=a[i]; } } if(a[1]==-1)/*如果a[1]没有值,则赋值为100(最大)*/ a[1]=100; if(a[2]==-1)/*如果a[2]没有值,则与a[1]相同*/ a[2]=a[1]; for(i=1; i<=n; i++) sum1+=a[i]; sum2=a[1]+a[2]; ans=gcd(sum2,sum1); printf("%d/%d\n",(sum2/ans),(sum1/ans)); } }
HDU5742 It's All In The Mind(思维题,水题)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。