HDUPhysical Examination(贪心)

题目链接

题目大意:给N个队列,每一个队列在0时刻体检的时候完毕时间是ai。假设超过t(s),那么就是ai + t?bi.问如何组合才干用最短的时间完毕体检(每一个队列都要去一趟)。

结果要取模一个给定的数。

解题思路:相邻交换法。将这N个队列排下先后体检的顺序。然后在计算要花费的时间就能够了,要用long Long,ai?bi会int溢出。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;
const int maxn = 1e5 + 5;
const int MOD = 31536000;

struct Queue {

    ll ai, bi;
} q[maxn];

bool cmp (const Queue A, const Queue B) {

    if (A.ai * B.bi < A.bi * B.ai)
        return true;
    return false;
}

int main () {

    int n;
    while (scanf ("%d", &n) && n) {

        for (int i = 0; i < n; i++)
            scanf ("%I64d%I64d", &q[i].ai, &q[i].bi);
        sort (q, q + n, cmp);

        ll ans = q[0].ai, t = q[0].ai;
        for (int i = 1; i < n; i++) {
            ans = (ans + q[i].ai + (t * q[i].bi) %MOD)%MOD;
            t = (t + q[i].ai + (t * q[i].bi) % MOD) % MOD;
        }

        printf ("%I64d\n", ans);
    }
    return 0;
}
时间: 2025-01-17 10:17:30

HDUPhysical Examination(贪心)的相关文章

CSU1603: Scheduling the final examination(贪心)

Description For the most of the university students,what they most want is that they can obtain 60 points from the final examination of every subject. Now, final examination is coming. As an excellent programmer,you are asked for help. The full mark

hdu4442 Physical Examination(贪心)

这种样式的最优解问题一看就是贪心.如果一下不好看,那么可以按照由特殊到一般的思维方式,先看n==2时怎么选顺序(这种由特殊到一般的思维方式是思考很多问题的入口): 有两个队时,若先选第一个,则ans=a1+a2+b2*a1;若先选第二个,则ans=a2+a1+b1*a2;所以选择顺序就比b2*a1和b1*a2就好了. 那么当有n>2个队时,能不能也这么搞?当然可以,每次剩下那几个队没选,我就两两比,两两之间比较时,之前用的时间都要加上是和顺序无关的,顺序影响的只是b2*a1和b1*a2而已(如果

HDU 4442 Physical Examination (贪心+排序)

Physical Examination Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5985    Accepted Submission(s): 1682 Problem Description WANGPENG is a freshman. He is requested to have a physical examinati

CSU 1603 Scheduling the final examination(贪心)

1603: Scheduling the final examination Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 58  Solved: 18 [Submit][Status][Web Board] Description For the most of the university students,what they most want is that they can obtain 60 points from the fina

HDU 4442 Physical Examination(关于贪心排序)

这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1,总的耗费就是a2 + a2 * b1 + a1.这时比较两个数的大小,发现两边都有a1+a2,所以只是比较a1*b2和a2 * b1的大小. #include <cstdio> #include <cstring> #include <algorithm> using na

WAP 2014 Examination 1

有两个考试,选了第1个. 题目的意思是W*H的方格,从S点到G点,中间要经过所有的n个checkpoint,求最短路.其中有的点不能走. 如果没有checkpoint的话,一个BFS就可以了.最开始想到贪心,从S开始,每次选择最近的那个点,但很容易找出反例来. 后来想,题目无非就是在S和G中安排n个点,以一定的次序使得依次连起来之后路径长度最短,因此一次添加一个点,假设之前已经安排好k个点,新加的点一共有k+1个位置可选.从这些可选点中选择最优解.但又找到了反例. 于是想到了动态规划,dp[i]

CSUOJ 1603 Scheduling the final examination

1603: Scheduling the final examination Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 15 Description For the most of the university students,what they most want is that they can obtain 60 points from the final examination of every subjec

hdu 4442 贪心

http://acm.hdu.edu.cn/showproblem.php?pid=4442 Problem Description WANGPENG is a freshman. He is requested to have a physical examination when entering the university. Now WANGPENG arrives at the hospital. Er-.. There are so many students, and the nu

【uva 1615】Highway(算法效率--贪心 区间选点问题)

题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到一个个区间,这样就是选最小的点覆盖所有的区间的问题了.我之前的一篇博文有较详细的解释:关于贪心算法的经典问题(算法效率 or 动态规划).代码实现我先空着.挖坑~