zoukankan      html  css  js  c++  java
  • hiho1269 优化延迟 ([Offer收割]编程练习赛1)

    一道中文题,就不用翻译了。

    大意是讲,一串数字,可以按照输入的先后顺序扔到一个固定大小的缓冲池子里,这个池子里的数输出顺序随意。然后计算——

    SP=1*Pi1+2*Pi2+3*Pi3+...+N*PiN(其中i1, i2, ... iN是1, 2, 3, ... N的一个排列)

    问在满足sp <= q的情况下,缓冲池最小可以是多少?

    很水的一道题,然而还是花了我好久。

    首先得出一个结论,将缓冲池里的数字按照从大到小的的顺序输出,可以得到当前缓冲池大小的最优解。那么就找一个可以在将数字扔到池子里的时候就排好序的数据结构就好了。我选的是优先队列。

    然后二分+模拟就可以了,二分枚举每次缓冲池的大小。刚开始的范围是1——n。

    在进行二分前先计算出大小为1和为n的情况下是否能够满足条件,如果为1可以就直接输出结果,如果为n也不行也直接输出结果。

    就是这么水,但是因为我的二分写的不怎么样,所以浪费了好多时间,导致下一题已经来不及做了……

    代码——

      1 #include <cstdio>
      2 #include <cmath>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <queue>
      6 #include <iostream>
      7 using namespace std;
      8 #define LL long long
      9 
     10 struct Node
     11 {
     12     int a;
     13 
     14     bool operator < (const Node& x) const
     15     {
     16         return x.a > a;
     17     }
     18 
     19 };
     20 
     21 int n;
     22 LL q;
     23 int s[100010];
     24 priority_queue <Node> que;
     25 
     26 bool Max()
     27 {
     28     LL maxn = 0;
     29     for(int i = 0; i < n; i++)
     30     {
     31         maxn += s[i]*(i+1);
     32     }
     33     if(maxn <= q) return 1;
     34     return 0;
     35 }
     36 
     37 bool Min()
     38 {
     39     while(!que.empty()) que.pop();
     40     LL minn = 0;
     41     for(int i = 0; i < n; i++)
     42     {
     43         Node p;
     44         p.a = s[i];
     45         que.push(p);
     46     }
     47     for(int i = 0; i < n; i++)
     48     {
     49         Node p = que.top();
     50         que.pop();
     51         minn += p.a*(i+1);
     52     }
     53     if(minn > q) return 1;
     54     return 0;
     55 }
     56 
     57 void ErFen()
     58 {
     59     int l = 1;
     60     int r = n;
     61     while(l <= r)
     62     {
     63         int lr = (l+r)/2;
     64         int sum = 0;
     65         LL ans = 0;
     66         int ii = 0;
     67         for(int i = 0; i < n; i++)
     68         {
     69             if(sum == lr)
     70             {
     71                 Node p = que.top();
     72                 ans += p.a*(++ii);
     73                 que.pop();
     74                 sum--;
     75             }
     76             Node p;
     77             p.a = s[i];
     78             que.push(p);
     79             sum++;
     80         }
     81         while(!que.empty())
     82         {
     83             Node p = que.top();
     84             ans += p.a*(++ii);
     85             que.pop();
     86         }
     87         if(ans == q) {l = lr; r = lr-1;}
     88         else if(ans > q) l = lr+1;
     89         else r = lr-1;
     90     }
     91     printf("%d
    ", l);
     92 }
     93 
     94 int main()
     95 {
     96     //freopen("test.in", "r", stdin);
     97     while(~scanf("%d", &n))
     98     {
     99         cin >> q;
    100         for(int i = 0; i < n; i++) scanf("%d", &s[i]);
    101         if(Max())
    102         {
    103             printf("1
    ");
    104             continue;
    105         }
    106         if(Min())
    107         {
    108             printf("-1
    ");
    109             continue;
    110         }
    111         ErFen();
    112     }
    113 }
    View Code
  • 相关阅读:
    eclipse中springsource-tool-suite(sts)插件安装教程
    maven的安装配置超详细教程【含nexus】
    取消文件与svn服务器的关联
    An internal error occurred during: "Initializing Java Tooling". Eclipse启动发生的错误
    jQuery validate 设置失去焦点就校验和失去焦点就表单校验是否通过
    CSS禁止滚动条
    六十:Flask.Cookie之flask设置cookie的有效域名
    五十九:Flask.Cookie之flask设置cookie过期时间
    五十八:Flask.Cookie之flask设置和删除cookie
    五十七:flask文件上传之使用flask-wtf验证上传的文件
  • 原文地址:https://www.cnblogs.com/mypride/p/5269051.html
Copyright © 2011-2022 走看看