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而已(如果想不通也可以写出数学表达式消项观察)。那么我两两之间全比完以后就选那个当前最小的就是当前最优解。为了方便,反正每次选最小,所以事先把n个队排好序然后直接算就行了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const int eps=1e-8;
const int INF=1000000000;
const int maxn=100000+10;
const LL mod=365*24*60*60;
int n;
struct node
{
    LL a,b;
}que[maxn];
bool cmp(node x,node y)
{
    return x.a*y.b<x.b*y.a;
}
LL ans;
int main()
{
    //freopen("in8.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)==1&&n)
    {
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&que[i].a,&que[i].b);
        }
        sort(que,que+n,cmp);
        for(int i=0;i<n;i++)
        {
            //cout<<que[i].a<<‘ ‘<<que[i].b<<endl;
            ans+=(que[i].a+(ans*que[i].b)%mod)%mod;
        }
        printf("%I64d\n",ans%mod);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}
时间: 2024-10-10 08:14:25

hdu4442 Physical Examination(贪心)的相关文章

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

hdu 4442 Physical Examination (排序)

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

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

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

HDU 4442 Physical Examination

水题...但是研究了半天 最后发现是这样排序的,两个项目进行排序,哪一个代价小,哪一个放前面 #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include &l

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

HDU-4442-Physical Examination (2012年金华赛区现场赛A题)

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

肺炎-维基百科

http://en.wikipedia.org/wiki/Pneumonia the number of words: 3110word           count---------------------pneumoniae        18respiratory       17streptococcus      15aspiration        11pulmonary          9interstitial       9sputum             6infl

高血压_翻译

网页: From Wikipedia, the free encyclopedia http://en.wikipedia.org/wiki/Hypertension 单词 word           count---------------------hypertension   [ha?p?'ten?(?)n]高血压  134 Hypertension is a medical condition in which a person has very high blood pressure