[NWPU][2014][TRM][8]第二次五小时组队赛,XX年金华区域赛真题 A

15 篇文章 0 订阅
A - Physical Examination
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 number is increasing! 
There are many examination subjects to do, and there is a queue for every subject. The queues are getting longer as time goes by. Choosing the queue to stand is always a problem. Please help WANGPENG to determine an exam sequence, so that he can finish all the physical examination subjects as early as possible.
 

Input

There are several test cases. Each test case starts with a positive integer n in a line, meaning the number of subjects(queues). 
Then n lines follow. The i-th line has a pair of integers (ai, bi) to describe the i-th queue: 
1. If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject. 
2. As the queue is getting longer, the waiting time will increase bi seconds every second while WANGPENG is not in the queue. 
The input ends with n = 0. 
For all test cases, 0<n≤100000, 0≤a  i,b  i<2  31.
 

Output

For each test case, output one line with an integer: the earliest time (counted by seconds) that WANGPENG can finish all exam subjects. Since WANGPENG is always confused by years, just print the seconds mod 365×24×60×60.
 

Sample Input

       
       
5 1 2 2 3 3 4 4 5 5 6 0
 

Sample Output

       
       
1419

Hint

 In the Sample Input, WANGPENG just follow the given order. He spends 1 second in the first queue, 5 seconds in the 2th queue, 27 seconds in the 3th queue, 169 seconds in the 4th queue, and 1217 seconds in the 5th queue. So the total time is 1419s. WANGPENG has computed all possible orders in his 120-core-parallel head, and decided that this is the optimal choice. 
         
 


开始时的代码

#include <iostream>
#include <algorithm>

using namespace std;
const int INF = 2100000000;
#define EPS 1e-8
const int MAXN = 100005;
double c[MAXN];
long long  n;

struct que
{
    int a, b;
    double c;
}q[MAXN];
//起初没用bool cmp(const que& x, const que& y)
//而是用的bool cmp(que x, que y)导致wa了多次
bool cmp(const que &x, const que &y)
{
    return x.c - y.c > 0;
}

int main()
{
    while(cin >> n && n)
    {

        for(int i = 1; i <= n; i++)
        {
            cin >> q[i].a >> q[i].b;
            /*这里开始以为wa是因为a, b == 0时要特判,
            后来发现除以int 0时程序会崩溃,而除以double 0时返回的是inf或-inf
            而此题正好是从大到小排序,a==0时排前面就先去那个队,b==0是排最后,正好切合题意
            所以下三行都是没用的,不过知道了“非零数除以int 0时程序会崩溃,而除以double 0时返回的是inf或-inf”*/
            //if(q[i].a == 0) { q[i].c = INF; continue; }
            //if(q[i].b == 0) {q[i].c = -INF ;continue; }
            //if(q[i].b == 0 && q[i].a == 0) {q[i].c = EPS ;continue; }
            q[i].c = (q[i].b * 1.0) / (q[i].a * 1.0);
        }
        sort(q + 1, q + n + 1, cmp);
        //cout << c[0] << " " << c[1];
        long long  sum = 0;
        for(int i = 1; i <= n; i++)
        {
            sum = (sum + (q[i].a + q[i].b * sum)) % (365*24*60*60);
            //sum %= (365*24*60*60);
        }
        cout << sum % (365*24*60*60) << endl;
    }
    return 0;
}

重写:

//@auther YangZongjun
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string.h>
#include <algorithm>
//#include <string>
//#include <cstring>

using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
const int INF = 2100000000;
const int MAXN = 100005;
const int N = 365*24*60*60;

int n;

struct que
{
    int a, b;
    double c;
}q[MAXN];

bool cmp(const que& x, const que& y)
{
    return x.c > y.c;
}

int main()
{
    //freopen("C:/Users/Admin/Desktop/input.txt", "r", stdin);
    while(cin >> n && n)
    {
        for(int i = 0; i < n; i++)
        {
            cin >> q[i].a >> q[i].b;
            q[i].c = (double)(q[i].b) / (double)(q[i].a);
        }
        sort(q, q + n, cmp);
        long long sum = 0;
        for(int i = 0; i < n; i++)
        {
            sum = (sum + q[i].a + q[i].b * sum) % N;
        }
        cout << sum << endl;
    }
    return 0;
}

这次学到了除零的情况,还是有收获的:

参见此篇博文:http://blog.sina.com.cn/s/blog_5e9e98210101ndgl.html

C++   运算结果出现 1.#IND, 1.#INF nan, inf 原因

  (2013-04-09 14:32:40)
 
  
   
   
进行浮点数编程时,如果没有注意,常常会出现输出类似 1.#IND, 1.#INF 或者 nan, inf 之类奇怪的输出。这通常隐含了浮点数操作的异常。
殊浮点数的含义
值表示“无穷大 (infinity 的缩写)”,即超出了计算机可以表示的浮点数的最大范围(或者说超过了 double 类型的最大值)。例如,当用 0 除一个整数时便会得到一个1.#INF / inf值;相应的,如果用 0 除一个负整数也会得到 -1.#INF / -inf 值。 -1.#IND / n
1.#INF / inf:这
个an:这个的情况更复杂,一般来说,它们来自于任何未定义结果(非法)的浮点数运算。"IND"是 indeterminate 的缩写,而"nan"是 not a number 的缩写。产生这个值的常见例子有:对负数开平方,对负数取对数,0.0/0.0,0.0*∞, ∞/∞ 等。举个例子,如果log()内的值是1.#INF,得到的log值就会是,1.#INF。
以简而言之,如果遇到 1.#INF / inf,就检查是否发生了运算结果溢出除零,而遇到 1.#IND / nan,就检查是否发生了非法的运算。
所 特殊浮点数的判断
判断一个浮点数是否是无穷大或 NaN。int _isnan(double x) 函数用来判断一个浮点数是否是 NaN,而 int _finite(double x) 用以判断一个浮点数是否是无穷大。 你可能已经注意到了,上面
很多 C 库都提供了一组函数用 来两个函数都是以下划线开头的,因此在可移植性上可能是存在问题的,那么如何实现一个通用的判断版本呢?首先,对于 Nan,可以用下面的代码实现: bool IsNumber(double x) { // 这里的比较操作看上去总是会得到 true
ol IsFiniteNumber(double x) { return (x
// 但有趣的是对于 IEEE 754 浮点数 NaN 来说总会得到 false! return (x == x); } 而下面的代码可以判断一个浮点数是否是有限的(finite, 即既不是 NaN 又不是 infinite): b o <= DBL_MAX && x >= -DBL_MAX); } 其中,DBL_MAX 是 中预定义的常量。
把上面两个函数结合起来,还可以实现一个浮点数是否是 Inf 的判断。
 
  
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值