zoukankan      html  css  js  c++  java
  • 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
    ",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
    ",a[1]+a[2],sum);
            int g = gcd(a[1]+a[2], sum);
            printf("%d/%d
    ", (a[1]+a[2])/ g, sum / g);      
        }  
           return 0;
    }
     
  • 相关阅读:
    leetcode 18 4Sum
    leetcode 71 Simplify Path
    leetcode 10 Regular Expression Matching
    leetcode 30 Substring with Concatenation of All Words
    leetcode 355 Design Twitte
    leetcode LRU Cache
    leetcode 3Sum
    leetcode Letter Combinations of a Phone Number
    leetcode Remove Nth Node From End of List
    leetcode Valid Parentheses
  • 原文地址:https://www.cnblogs.com/chendl111/p/5693563.html
Copyright © 2011-2022 走看看