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-;
const int INF=;
const int maxn=+;
const LL mod=***;
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)==&&n)
{
ans=;
for(int i=;i<n;i++)
{
scanf("%I64d%I64d",&que[i].a,&que[i].b);
}
sort(que,que+n,cmp);
for(int i=;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 ;
}
hdu4442 Physical Examination(贪心)的更多相关文章
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- hdu 4442 Physical Examination 贪心排序
Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4442 Physical Examination
Physical Examination Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
- HDU 4442 Physical Examination(关于贪心排序)
这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...
- hdu 4442 Physical Examination (2012年金华赛区现场赛A题)
昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...
- 2012 Asia JinHua Regional Contest
Draw Something http://acm.hdu.edu.cn/showproblem.php?pid=4450 o(n)统计输入每个数的平方和. #include<cstdio> ...
- 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)
Walking Race Description flymouse's sister wc is very capable at sports and her favorite event is ...
- [讨论] Window XP 安装msxml6后,load xml时提示schema验证失败
现象:在windows XP x64下,使用用户安装的msxml6库加载xml文件时失败. 进一步说明: 该xml文档使用了W3C的名称空间 xmlns:xsi= "http://www.w ...
- poj3162(树形dp+优先队列)
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 5409 Accepted: 1371 Ca ...
随机推荐
- Eclipse 变量高亮显示设置
A:Window-> preferences->java->Editor->Mark Occurences Local variables就是变量的高亮显示
- RabbitMQ队列/Redis缓存
一.RabbitMQ队列 RabbitMQ安装(Centos7安装):1.安装依赖:yum install socat (不安装会报错)2.下载rpm包:wget http://www.rabbitm ...
- OpenGL学习进程(7)第五课:点、边和图形(二)边
本节是OpenGL学习的第五个课时,下面介绍OpenGL边的相关知识: (1)边的概念: 数学上的直线没有宽度,但OpenGL的直线则是有宽度的.同时,OpenGL的直线必须是有限长度,而不是像数学概 ...
- Python字符串格式转换
转换类型 转换类型 说明 d, i 带符号十进制 b 无符号二进制 o 无符号八进制 u 无符号十进制 x 无符号十六进制(小写) X 无符号十六进制(大写) e 科学计数法表示的浮点数(小写) E ...
- C#多线程学习之Thread
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- iOS_SDWebImage框架分析
SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的 category,方便使用.使用SDWebImage首先了解它加载图片的流程. 入口 setIma ...
- 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动
今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...
- dll和lib
lib:里面包含了很多源代码,工程会将这些源代码加入自己的项目中编译: dll:动态编译库,允许可执行文件在运行中加载里面的资源. 使用lib需注意两个文件:(1).h头文件,包含lib中说明输出的类 ...
- INSPIRED启示录 读书笔记 - 第38章 打造企业级产品的经验
十大要点 1.可用性:很少有企业开发这类软件时会进行交互设计.视觉设计.可用性测试,因此产品才会表现得如此糟糕 2.产品正常工作:多数企业级产品根本没法使用,或者还需花大量的时间和资金开发临时补丁,产 ...
- c++ boost库学习一:时间和日期
timer类 #include <boost\timer.hpp> #include "iostream" using namespace std; int _tmai ...