hdu5742 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): 857 Accepted Submission(s): 401

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

题意:给你 n 个数,已知了m个数,求 a1+a2ni=1ai 的最大值。

思路:我们可以将式子转化一下得:求 1ni=3aini=1ai 的最大值,由式子得要保证 ni=3ai 取最小值,这样 a1+a2 就要取最大值, a1 a2 我们可以通过检查是否赋过值来进行定义值, a2 后面的数,根据条件 a1a2...an ,我们可以从 an 开始从后向前扫,初始化为0,如果遇到赋过值的,改变数值,最后求和就好了,具体看代码。

ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 1010000
#define LL long long
#define ll __int64
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
int a[MAXN];
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int n,m;scanf("%d%d",&n,&m);
        memset(a,-1,sizeof(a));
        for(int i=1;i<=m;i++)
        {
            int b,c;scanf("%d%d",&b,&c);
            a[b]=c;
        }
        if(a[1]==-1)
        {
            a[1]=100;
            if(a[2]==-1) a[2]=100;
        }
        else if(a[2]==-1)
            a[2]=a[1];
        int k=0;
        for(int i=n;i>=3;i--)
        {
            if(a[i]==-1)
                a[i]=k;
            else
                k=a[i];
        }
        ll down=0,up=a[1]+a[2];
        for(int i=1;i<=n;i++)
            down+=a[i];
        ll gc=gcd(up,down);
        printf("%I64d/%I64d\n",up/gc,down/gc);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值