codeforces 463A Caisa and Sugar 解题报告

题目链接:http://codeforces.com/problemset/problem/463/A

题目意思:某个人有 s dollar的钱,有 n 种类型的糖果,第 i 种糖果的价值为 xi dollar + yi cent。这个商场比较古怪,如果收了顾客的cent,它不会找回相应的cent,而是用对应的sweet来替代。这个人只可以买一种类型的糖果,前提是 s dollar 大于等于糖果的价值,问最多可以得到的sweet是多少。

   有几个细节需要注意:

   (1)如果够钱买,但是糖果的cent 为 0,这个人是得不到任何sweet!

   (2)判断够不够钱买糖果的时候,不能只单纯看dollar,而要看埋对应的cent。例如这组数据:

    1  10

    10  10

     这个人是买不起这种糖果的!

   (3)sweet 数 = 100 - yi(前提是买得起)


 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 100 + 5;
 9 
10 struct node
11 {
12     int x, y;
13     bool operator < (const node& a) const
14     {
15         return x < a.x;
16     }
17 
18 }sugar[maxn];
19 
20 int main()
21 {
22     int s, n;
23     while (scanf("%d%d", &n, &s) != EOF)
24     {
25         for (int i = 0; i < n; i++)
26             scanf("%d%d", &sugar[i].x, &sugar[i].y);
27         sort(sugar, sugar+n);
28         int ans = 0;
29         int flag = 0;
30         for (int i = 0; i < n; i++)
31         {
32             if (sugar[i].x > s)   
33                 break;
34             else if (sugar[i].x == s && sugar[i].y > 0)   // 对应情况2
35                 continue;
36             else
37             {
38                 if (sugar[i].y == 0)  // 对应第一种情况
39                     ans = max(ans, 0);
40                 else
41                     ans = max(ans, 100-sugar[i].y);
42                 flag = 1;
43             }
44         }
45         printf("%d
", flag ? ans : -1);
46     }
47     return 0;
48 }
View Code

    

原文地址:https://www.cnblogs.com/windysai/p/3946962.html