这种样式的最优解问题一看就是贪心。如果一下不好看,那么可以按照由特殊到一般的思维方式,先看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(贪心)的更多相关文章

  1. HDU 4442 Physical Examination(贪心)

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  2. hdu 4442 Physical Examination 贪心排序

    Physical Examination Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 4442 Physical Examination

    Physical Examination Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

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

    这个题目用贪心来做,关键是怎么贪心最小,那就是排序的问题了. 加入给定两个数a1, b1, a2, b2.那么如果先选1再选2的话,总的耗费就是a1 + a1 * b2 + a2; 如果先选2再选1, ...

  5. hdu 4442 Physical Examination (2012年金华赛区现场赛A题)

    昨天模拟赛的时候坑了好久,刚开始感觉是dp,仔细一看数据范围太大. 题目大意:一个人要参加考试,一共有n个科目,每个科目都有一个相应的队列,完成这门科目的总时间为a+b*(前面已完成科目所花的总时间) ...

  6. 2012 Asia JinHua Regional Contest

    Draw Something http://acm.hdu.edu.cn/showproblem.php?pid=4450 o(n)统计输入每个数的平方和. #include<cstdio> ...

  7. 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)

    Walking Race   Description flymouse's sister wc is very capable at sports and her favorite event is ...

  8. [讨论] Window XP 安装msxml6后,load xml时提示schema验证失败

    现象:在windows XP x64下,使用用户安装的msxml6库加载xml文件时失败. 进一步说明: 该xml文档使用了W3C的名称空间 xmlns:xsi= "http://www.w ...

  9. poj3162(树形dp+优先队列)

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 5409   Accepted: 1371 Ca ...

随机推荐

  1. 剑指offer 面试21题

    面试21题: 题目:调整数组的顺序使奇数位于偶数前面 题一:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 解题思路:使用两个指针 ...

  2. python之路 正则表达式,模块导入的方法,hashlib加密

    一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...

  3. centos 6.5 设置屏幕保护

    设置屏幕保护:System -> Preferences -> Screensaver.如果需要取消屏幕保护的锁定功能,将Lock screen when screensaver is a ...

  4. SOA宣言和微服务特点

    如果从概念层来看,我更喜欢把SOA归为企业架构的范畴,从企业架构出发把业务分解为不同业务域的服务,关注系统间的服务互联互通的规范,并不关心如何实现.也就是说在企业架构上使用SOA支撑业务,而在方案架构 ...

  5. C语言中auto,register,static,const,volatile的区别

    1)auto 这个关键字用于声明变量的生存期为自动,即将不在任何类.结构.枚举.联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量.这个关键字不怎么多写,因为所有的变量默认就是aut ...

  6. React routerV4 笔记

    React routerV4 笔记     一.基础路由示例 import React from 'react' import { BrowserRouter as Router, Route, Li ...

  7. Java中ArrayList和LinkedList区别、ArrayList和Vector的区别

    一般大家都知道ArrayList和LinkedList的大致区别: 1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,Ar ...

  8. 对vector,list的操作函数

    向量只能接受同一类型的数据:list可以接受不同的数据. 1.添加元素 vector:> b=c(1,2,3) > b=c(b,"four") #直接在后面添加添加 & ...

  9. Cisco学习笔记

    目录 1. 路由 1.1 静态路由 1.2 动态路由 2. 访问控制列表 2.1 标准访问控制列表 2.2 扩展访问控制列表 2.3 命名访问控制列表 3. VLAN 3.1 基础知识 3.2 配置实 ...

  10. One 的使用(1)

    方法一:使用命令提示符 第一步:打开d盘  C:Users\dcf>d; 第二步:打开工作空间  D:\>Cd workspace 第三步:打开the one  D:\workspace& ...