HDU 5742 It's All In The Mind (贪心)

It's All In The Mind

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5742

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

Source

2016 Multi-University Training Contest 2


##题意: 对于一个数组a1 - an,部分元素已知,部分未知. 数组满足性质:0≤ai≤100, 非严格递减, 所有数之和非0. 求所有满足情况的数组中,a1+a2/sum(ai) 的最大值.
##题解: 贪心的想法: a1和a2应该尽量大; 其余数尽量小. WA点:a1已知但a2未知,注意不要把a2直接赋成100; WA了一个下午....弱的不行
##代码: ``` cpp #include #include #include #include #include #include #include #include #include #define LL long long #define double LL #define eps 1e-8 #define maxn 150 #define mod 1000000007 #define inf 0x3f3f3f3f #define IN freopen("in.txt","r",stdin); using namespace std;

int n,m;
int num[maxn];

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

int main(void)
{
//IN;

int t; cin >> t;
while(t--)
{
    memset(num, -1, sizeof(num));
    cin >> n >> m;
    for(int i=1; i<=m; i++) {
        int x,y; scanf("%d %d", &x,&y);
        num[x] = y;
    }

    int mimi = 0;
    for(int i=n; i>=3; i--) {
        if(num[i] == -1) {
            num[i] = mimi;
        } else {
            mimi = num[i];
        }
    }

    if(num[1] == -1) num[1] = 100;
    if(num[2] == -1) num[2] = min(100, num[1]);

    int sum1 = num[1] + num[2];
    int sum2 = sum1;
    for(int i=3; i<=n; i++) sum2 += num[i];
    if(sum2 == 0) sum2 = 1;

    int gcds = gcd(sum1, sum2);
    printf("%d/%d
", sum1/gcds, sum2/gcds);
}

return 0;

}

原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5709488.html