zoukankan      html  css  js  c++  java
  • 洛谷P1080 国王游戏

    两个难点。

    • 怎么想到的贪心?

    首先确定算法:

    显然不是数据结构题。转成图论也不太可能。

    考虑DP:f[i][j]表示前i个人取j状态的最小最大值......2^1000,直接放弃。

    因为出现了“最大值最小”,考虑二分答案:如果我们有一个ans是最大值,我们怎么判断是否可行?

    要确保每一个数都不会超过ans,这很困难。

    思路戛然而止。

    然后思考:本题要求排序。zbtrs:排序不就是贪心吗?

    然后想到邻位互换法,得出贪心策略。

    显然很难想出贪心来......

    • 高精度

    这个没什么好说的,写个高精度乘单精度,除单精度,比较大小,输出,就行了。


    跑的十分之快,136ms十个点
      1 #include <cstdio>
      2 #include <algorithm>
      3 #include <string>
      4 #include <cstring>
      5 using namespace std;
      6 const int N = 1010;
      7 struct LL {
      8     string num;
      9     void out() {
     10         for(int i = 0; i < num.size(); i++) {
     11             putchar(num[i]);
     12         }
     13         return;
     14     }
     15     LL operator * (const int& x) const {
     16         int len = num.size();
     17         int a[len + 50];
     18         memset(a, 0, sizeof(a));
     19         for(int i = 0; i < len; i++) {
     20             a[i] = num[len - i - 1] - '0';
     21             a[i] *= x;
     22         }
     23         for(int i = 0; i < len; i++) {
     24             if(a[i] > 9) {
     25                 a[i + 1] += a[i] / 10;
     26                 a[i] %= 10;
     27                 if(i + 1 == len) {
     28                     len++;
     29                 }
     30             }
     31         }
     32         string f = "";
     33         for(int i = len - 1; i >= 0; i--) {
     34             f += (a[i] + '0');
     35         }
     36         LL t;
     37         t.num = f;
     38         return t;
     39     }
     40     LL operator / (const int x) const {
     41         int len = num.size();
     42         int a[len];
     43         for(int i = 0; i < len; i++) {
     44             a[i] = num[len - i - 1] - '0';
     45         }
     46         for(int i = len - 1; i > 0; i--) {
     47             a[i - 1] += 10 * (a[i] % x);
     48             a[i] /= x;
     49         }
     50         a[0] /= x;
     51         while(len >= 1 && a[len - 1] == 0) {
     52             len--;
     53         }
     54         string f = "";
     55         for(int i = len - 1; i >= 0; i--) {
     56             f += (a[i] + '0');
     57         }
     58         LL t;
     59         t.num = f;
     60         return t;
     61     }
     62     bool operator < (const LL& x) const {
     63         if(num.size() != x.num.size()) {
     64             return num.size() < x.num.size();
     65         }
     66         for(int i = 0; i < num.size(); i++) {
     67             if(num[i] != x.num[i]) {
     68                 return num[i] < x.num[i];
     69             }
     70         }
     71         return 1;
     72     }
     73 };
     74 struct Man {
     75     int a, b, c;
     76     bool operator < (const Man &f) const{
     77         return this->c < f.c;
     78     }
     79 }man[N];
     80 int main() {
     81     int n;
     82     scanf("%d", &n);
     83     for(int i = 0; i <= n; i++) {
     84         scanf("%d%d", &man[i].a, &man[i].b);
     85         man[i].c = man[i].a * man[i].b;
     86     }
     87 
     88     sort(man + 1, man + n + 1);
     89     /*
     90     for(int i = 1; i <= n; i++) {
     91         printf("%d %d %d
    ", man[i].a, man[i].b, man[i].c);
     92     }
     93     printf("
    ");
     94     */
     95     LL ans, now;
     96     now.num = "1";
     97     ans.num = "0";
     98     for(int i = 0; i < n; i++) {
     99         now = now * man[i].a;
    100         LL t = now / man[i + 1].b;
    101         if(ans < t) {
    102             ans = t;
    103         }
    104     }
    105     ans.out();
    106     return 0;
    107 }
    108 /**
    109 3
    110 1 7
    111 6 1
    112 2 3
    113 2 3
    114 ans:4
    115 */
    AC代码

  • 相关阅读:
    「AHOI / HNOI2017」单旋
    【CQOI2012】局部极小值
    【CQOI2011】放棋子
    【gdoi2018 day2】第二题 滑稽子图
    【JSOI2018】潜入行动
    在vue中获取不到canvas对象? 两种解决办法。
    数据可视化echart
    在vue中使用高德地图开发,以及AMap的引入?
    mac 常使用的一些小技巧
    form编码方式application/x-www-form-urlencoded和multipart/form-data的区别?
  • 原文地址:https://www.cnblogs.com/huyufeifei/p/9013684.html
Copyright © 2011-2022 走看看