hdu-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)


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 pq are integers, q>0.
 
Sample Input
 
2
2 0
3 1
3 1
 
Sample Output
 
1/1
200/201
 
题意:
 
给一个单调序列的一部分,让你求这个式子的最大值;
 
思路:
 
(a1+a2)/(∑ai)的最小值;就是∑ai/(a1+a2)的最大值;就是1+(a3+a4+...an)/(a1+a2)的最大值;然后就是分子上的尽量小,分母上的尽量大了,相当于不等式的放缩;
 
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;

template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+10;
const int maxn=500+10;
const double eps=1e-6;

int a[maxn];

 int gcd(int x,int y)
 {
     if(y==0)return x;
     return gcd(y,x%y);
 }
int main()
{
        int t;
        read(t);
        while(t--)
        {
            int n,m;
            read(n);read(m);
            mst(a,-1);
            int x,y;
            For(i,1,m)
            {
                read(x);read(y);
                a[x]=y;
            }
            int sum=0;
            if(a[n]==-1)a[n]=0;
            sum+=a[n];
            for(int i=n-1;i>2;i--)
            {
                if(a[i]==-1)a[i]=a[i+1];
                sum+=a[i];
            }
            if(a[1]==-1)a[1]=100;
            if(a[2]==-1)a[2]=a[1];
            int p,q;
            p=a[1]+a[2];
            q=sum+p;
            //if(q==0)
            int g=gcd(p,q);
            cout<<p/g<<"/"<<q/g<<endl;
        }
        return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5692898.html