HDU5742:It's All In The Mind(模拟+贪心 )

题意:

给出n和m,表示n个数,之后会给出m个下标xi和值yi,a[xi]=yi,n个数是不下降的,且总和>0,要使得(x1+x2)/∑(xi)最大。


分析:

尽可能使得前两个数最大,其他数尽可能小即可。


代码:


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, a, b)    for (int i(a); i <= (b); ++i)
int a[110];
int T;
int n, m, x1,x2, y1,y2;

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

int main(){   
    scanf("%d ", &T);
  loop:  while (T--){
        scanf("%d %d ", &n, &m);
        memset(a, 0, sizeof a);
        x1=0;
        scanf("%d %d",&x2,&y2);
        if(x2==1) 
        {
            a[1]=a[2]=y2;x1=x2;
        }
        else
        {
            a[x2]=y2;
            if(x2<=3) rep(i,1,x2-1) a[i]=100;
            else{a[1]=a[2]=100;rep(i,3,x2-1) a[i]=y2;} x1=x2;
        }
        rep(i,2,m)
            {
            scanf("%d %d", &x2, &y2);
            a[x2]=y2;
            if(i<=m)
            {
                if(x1+1==2) rep(j,3,x2-1) a[j]=y2;
                else rep(j,x1+1,x2-1) a[j]=y2; 
            }
            x1=x2;
            }
        rep(j,x1+1,n) a[j]=0;
        //rep(i,1,n) printf("a[%d]=%d\n",i,a[i]);
        if (n == 2){ puts("1/1"); goto loop;}    
        int sum = 0;
        rep(i, 1, n) sum += a[i];
        //printf("a[1]+a[2]=%d sum=%d\n",a[1]+a[2],sum);
        int g = gcd(a[1]+a[2], sum);
        printf("%d/%d\n", (a[1]+a[2])/ g, sum / g);      
    }  
       return 0;
}
 

 

posted @ 2016-07-21 23:27  遗风忘语  阅读(269)  评论(0编辑  收藏  举报