HDOJ 5742 It's All In The Mind(数学变形)

It's All In The Mind

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1179    Accepted Submission(s): 586


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
 

Author
zimpha
 

Source


思路:
这题的意思就是说,它是一个递减数列,然后可能会给定你几个位置的数,让你求(a1+a2)/Σai 的最大值。对表达式进行变形,其实就是1/(1+(a3+…+an)/(a1+a2))的最大值。所以只需要a1+a2最大,a3+…+ai最小就好了。那样的话就是把给定位置之前的数都赋值成给定位置的数值,最后一个给定位置之后的全都变为0。对a1、a2是不是给定位置进行分类讨论就好,很水的一题。

代码:
#include<iostream>
using namespace std;

int a[200];
int num[200];

int gcd(int a, int b)
{
    int r=a%b;
    if(r==0) return b;
    else return gcd(b,a%b);
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        int sum=0;
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            int xi,yi;
            cin>>xi>>yi;
            num[i]=xi;
            a[xi]=yi;
        }
        //后面的值都最小 
        
    
        
        if(num[0]==2) 
        {            
            for(int i=0;i<m-1;i++)//中间的 
            {
                for(int j=num[i]+1;j<num[i+1];j++)
                {
                     a[j]=a[num[i+1]];
                }
             }
             for(int j=num[m-1]+1;j<=n;j++)//最后的 
            {
                a[j]=0;
            }
            for(int i=3;i<num[0];i++) //3-中间 
            {
                a[i]=a[num[0]];
            }
            a[1]=100;     
        }
        
        else if(num[0]==1&&num[1]!=2) 
        {            
            for(int i=0;i<m-1;i++)//中间的 
            {
                for(int j=num[i]+1;j<num[i+1];j++)
                {
                     a[j]=a[num[i+1]];
                }
             }
             for(int j=num[m-1]+1;j<=n;j++)//最后的 
            {
                a[j]=0;
            }
            for(int i=3;i<num[0];i++) //3-中间 
            {
                a[i]=a[num[0]];
            }
            a[2]=a[1]; 
        }
        
        else if(num[0]==1&&num[1]==2) 
        {            
            for(int i=0;i<m-1;i++)//中间的 
            {
                for(int j=num[i]+1;j<num[i+1];j++)
                {
                     a[j]=a[num[i+1]];
                }
             }
             for(int j=num[m-1]+1;j<=n;j++)//最后的 
            {
                a[j]=0;
            }
            for(int i=3;i<num[0];i++) //3-中间 
            {
                a[i]=a[num[0]];
            }     
        }
        
        
        else
        {            
            for(int i=0;i<m-1;i++)//中间的 
            {
                for(int j=num[i]+1;j<num[i+1];j++)
                {
                     a[j]=a[num[i+1]];
                }
             }
             for(int j=num[m-1]+1;j<=n;j++)//最后的 
            {
                a[j]=0;
            }
            for(int i=3;i<num[0];i++) //3-中间 
            {
                a[i]=a[num[0]];
            }
            a[1]=a[2]=100;     
        }//前两个数都没有输入
        
        for(int i=1;i<=n;i++)
        {
            sum=sum+a[i];
        }
        
        int ll=a[1]+a[2];
        int l=gcd(ll,sum);
       // cout<<ll<<" "<<sum<<" "<<l<<endl;
       
        if(l-1==0)cout<<ll<<"/"<<sum<<endl; 
        else   cout<<(ll/l)<<"/"<<(sum/l)<<endl;
        
    }
} 



全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
运营你豪哥:简历改改吧-非本、求职意向技术岗、无实习经历、内容空洞 如果简历不爆改的话,应该是会持续崩溃了 1.把你教育经历放最下面去 2.蓝底照片很奇怪哈,感觉还在高中时代,建议白底重新拍一下 3.校园经历没啥必要,收集和反馈同学们对产品的意见,解决学生和老师之间的沟通,企业招聘不看这些哈 好好思考一下简历的设计和你要表达的重点,再去投简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务