• CF967D Resource Distribution


    思路:

    在一堆服务器中,资源最少的那一个是“瓶颈”,由此想到贪心思路。

    首先对所有服务器按照资源数量c排序,再从头到尾扫描。对每个位置,根据x1和x2计算出两段连续的服务器集合分别分配给A任务和B任务(还需要枚举分配A和B的先后顺序),如果够用,则有解。所有位置都不行,则无解。

    实现:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef pair<int, int> pii;
     4 pii c[300005];
     5 int n, x1, x2;
     6 vector<int> check(int a, int b)
     7 {
     8     bool flg = false;
     9     int tmp, tmp2, l, l2, i = 0;
    10     for ( ; i < n; i++)
    11     {
    12         tmp = c[i].first; l = (a + tmp - 1) / tmp;
    13         if (i + l >= n) continue;
    14         tmp2 = c[i + l].first; l2 = (b + tmp2 - 1) / tmp2;
    15         if (i + l + l2 > n) continue;
    16         flg = true; break;
    17     }
    18     vector<int> ans;
    19     if (flg) { ans.push_back(i); ans.push_back(l); ans.push_back(l2); }
    20     return ans;
    21 }
    22 void show(vector<int> & v, bool rev)
    23 {
    24     vector<int> v1, v2;
    25     for (int i = v[0]; i < v[0] + v[1]; i++) v1.push_back(c[i].second);
    26     for (int i = v[0] + v[1]; i < v[0] + v[1] + v[2]; i++) v2.push_back(c[i].second);
    27     cout << "Yes" << endl;
    28     if (rev)
    29     {
    30         cout << v[2] << " " << v[1] << endl;
    31         for (auto it: v2) cout << it << " ";
    32         cout << endl;
    33         for (auto it: v1) cout << it << " ";
    34         cout << endl;
    35     }
    36     else
    37     {
    38         cout << v[1] << " " << v[2] << endl;
    39         for (auto it: v1) cout << it << " ";
    40         cout << endl;
    41         for (auto it: v2) cout << it << " ";
    42         cout << endl;
    43     }
    44 }
    45 int main()
    46 {
    47     while (cin >> n >> x1 >> x2)
    48     {
    49         for (int i = 0; i < n; i++) { cin >> c[i].first; c[i].second = i + 1; }
    50         sort(c, c + n);
    51         vector<int> ans = check(x1, x2);
    52         if (ans.size()) { show(ans, false); continue; }
    53         ans = check(x2, x1);
    54         if (ans.size()) { show(ans, true); continue; }
    55         cout << "No" << endl;
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    协程方法的开启、关闭以及传参
    五种访问修饰符
    多态之虚方法、抽象类、接口
    递归算法
    继承之构造方法
    鼠标相关操作(Cursor类及相关API)
    遇到的问题(七)
    遇见的问题(六)
    遇见的问题(五)
    函数VS对象
  • 原文地址:https://www.cnblogs.com/wangyiming/p/8973860.html
Copyright © 2020-2023  润新知