首页 > ACM题库 > HDU-杭电 > hdu 2620 Ice Rain[解题报告]C++
2014
02-12

hdu 2620 Ice Rain[解题报告]C++

Ice Rain

问题描述 :

Ice Rain——I was waiting for a girl, or waiting for been addicted to the bitter sea. Love for irrigation in silence. No one considered whether the flowers came out or wither. Love which I am not sure swing left and right. I had no choice but to put my sadness into my heart deeply.

   Yifenfei was waiting for a girl come out, but never.
His love is caught by Lemon Demon. So yifenfei ’s heart is “Da Xue Fen Fei” like his name.
The weather is cold. Ice as quickly as rain dropped. Lemon said to yifenfei, if he can solve his problem which is to calculate the value of , he will release his love.
Unluckily, yifenfei was bored with Number Theory problem, now you, with intelligent, please help him to find yifenfei’s First Love.

输入:

Given two integers n, k(1 <= n, k <= 109).

输出:

Given two integers n, k(1 <= n, k <= 109).

样例输入:

5 4
5 3

样例输出:

5
7

http://acm.hit.edu.cn/hoj/problem/view?id=2620

给出龙头个数和骑士人数

骑士身高大于龙头直径时可以砍下龙头

排序完扫一遍

#include <stdio.h>
#include <stdlib.h>

int compar(const void *a, const void *b);

int main()
{
    int n, m;
    int diameter[20001], height[20001];
    int i, j, k, sum, head;

    while (scanf("%d %d", &n, &m) != EOF)
    {
        if (n == 0 &&m == 0)
            break;

        for (i = 0; i < n; i++)
            scanf("%d", &diameter[i]);
        for (i = 0; i < m; i++)
            scanf("%d", &height[i]);

        if (n > m)
        {
            printf("Loowater is doomed!\n");
            continue;
        }

        qsort(diameter, n, sizeof(diameter[0]), compar);
        qsort(height, m, sizeof(height[0]), compar);

        head = sum = k = 0;
        for (i = 0; i < n; i++)
        {
            for (j = k; j < m; j++)
            {
                if (diameter[i] <= height[j])
                {
                    head++;
                    sum += height[j];
                    k = ++j;
                    break;
                }
            }
        }

        if (head == n)
            printf("%d\n", sum);
        else
            printf("Loowater is doomed!\n");
    }

    return 0;
}

int compar(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

 

解题转自:http://blog.csdn.net/epk_lee/article/details/8232330