(HDU 5742) It's All In The Mind <思维水题> 2016 Multi-University Training Contest 2

41 篇文章 0 订阅
14 篇文章 0 订阅

It’s All In The Mind
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1608 Accepted Submission(s): 749

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.

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.

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
2016 Multi-University Training Contest 2

题意:
有一个含有n个元素的非递增序列ai(0=< ai <= 100),现在告诉你m个数的信息x,v(第x个数的值为v),sum为所有元素的和,求(a1+a2)/sum的最大值?

分析:
要想(a1+a2)/sum 值最大,那么只需要使a1+a2尽可能的大,而 a3+ .. an尽可能的小即可。
所以当a1 ,a2尽可能的向100 靠近, 而后面未知的数等于他后面第一个已知的位置上的数即可。
最后求出分子和分母后再分别除以他们的gcd即可

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 110;
int ind[maxn],val[maxn];

int gcd(int a,int b)
{
    return b == 0 ? a : gcd(b,a%b);
}

int main()
{
    int t,n,m,x,v,p,q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int f1 = 0, f2 = 0; //记录a1,a2是否已知
        p = q = 0;
        ind[0] = 1; ind[1] = 2; 
        int e = 2;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&x,&v);
            if(x == 1)
            {
                f1 = 1;
                val[1] = v;
            }
            else if(x == 2)
            {
                f2 = 1;
                val[2] = v;
            }
            else
            {
                ind[e++] = x;
                val[x] = v;
            }
        }
        if(!f1 && !f2) val[1] = val[2] = 100;
        else if(!f1 && f2) val[1] = 100;
        else if(f1 && !f2) val[2] = val[1];
        p += val[1] + val[2];
        for(int i=e-2;ind[i]>=2;i--) q += (ind[i+1] - ind[i])*val[ind[i+1]];
        q += p;
        int tmp = gcd(p,q);
        printf("%d/%d\n",p/tmp,q/tmp);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值