UVa_Live 3664(精度坑)

Posted on 2015-08-27 19:06  LLGemini  阅读(231)  评论(0编辑  收藏  举报

题意很好理解的贪心题,然而却卡疯了的精度坑。

再次理解一下double小数运算时可能导致的精度问题,本题为避免该问题可以将小数乘以100化为整数进行比较,输出的时候再除以100就ok;

 

思路也很好想,数据也不大,直接贴代码吧。

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <sstream>
  4 #include <cmath>
  5 #include <cstring>
  6 #include <cstdlib>
  7 #include <string>
  8 #include <vector>
  9 #include <map>
 10 #include <set>
 11 #include <queue>
 12 #include <stack>
 13 #include <algorithm>
 14 using namespace std;
 15 #define ll long long
 16 #define _cle(m, a) memset(m, a, sizeof(m))
 17 #define repu(i, a, b) for(int i = a; i < b; i++)
 18 #define repd(i, a, b) for(int i = b; i >= a; i--)
 19 #define sfi(n) scanf("%d", &n)
 20 #define pfi(n) printf("%d\n", n)
 21 #define MAXN 100010
 22 const int maxn = 16400;
 23 struct Player
 24 {
 25     double a, b, c;
 26     int max_;
 27     set<int> s;
 28 } p[maxn];
 29 int r[maxn], vis[maxn];
 30 int main()
 31 {
 32     int n;
 33     int T = 1;
 34     while(~scanf("%d", &n) && n)
 35     {
 36         for(int i = 0; i <= n; i++)
 37             p[i].s.clear();
 38         for(int i = 1; i <= n; i++)
 39         {
 40             scanf("%lf%lf%lf", &p[i].a, &p[i].b, &p[i].c);
 41             int a = (p[i].a+0.005)*100; //注意此处控制精度,其实就是为了四舍五入
 42             int b = (p[i].b+0.005)*100;
 43             int c = (p[i].c+0.005)*100;
 44 
 45             p[i].s.insert(a);
 46             p[i].s.insert(b);
 47             p[i].s.insert(c);
 48             p[i].s.insert(a+b);
 49             p[i].s.insert(a+c);
 50             p[i].s.insert(b+c);
 51             p[i].s.insert(a+b+c);
 52             p[i].s.insert(0);
 53             p[i].max_ = a+b+c;
 54         }
 55         bool ok = true;
 56         for(int i = 0; i < n; i++)
 57         {
 58             scanf("%d", &r[i]);
 59         }
 60         int ans = p[r[0]].max_;
 61         for(int i = 1; i < n; i++)
 62         {
 63             int last = r[i-1], now = r[i];
 64             if(last > now)
 65             {
 66                 if(*p[now].s.begin() >= ans)
 67                 {
 68                     ok = false;
 69                     break;
 70                 }
 71                 set<int>::iterator it = p[now].s.lower_bound(ans);
 72                 it--;
 73                 ans = *it;
 74             }
 75             else
 76             {
 77                 if(*p[now].s.begin() > ans)
 78                 {
 79                     ok = false;
 80                     break;
 81                 }
 82                 set<int>::iterator it = p[now].s.lower_bound(ans);
 83                 if(it == p[now].s.end() || *it > ans)
 84                 {
 85                     --it;
 86                     ans = *it;
 87                 }
 88             }
 89         }
 90         if(ok)
 91         {
 92             printf("Case %d: %.2lf\n", T++, (double)ans/100.00);
 93         }
 94         else
 95         {
 96             printf("Case %d: No solution\n", T++);
 97         }
 98     }
 99     return 0;
100 }
View Code