zoukankan      html  css  js  c++  java
  • [csu/coj 1083]贪心

    题意:给定n个线段,问能不能把x,y,z个长度为1,2,3的线段不重合地放进去。

    思路:首先如果n个线段长度比要放的长度之和小,则无解,否则先考虑放2和3,如果2和3放下了1肯定可以放下(这是显然的)。于是我们贪心先把n个线段放满长度为3的线段,然后再考虑删去长度为3的线段来放长度为2的线段,删的时候要选择删去以后空闲的线段长度最多的删,比如某个线段本身有1的空闲线段,这时如果删去一条放在上面的长度为3的线段,则空闲线段变为4,这种情况优先删,其它情况次之。实现上采用优先队列,保存每个线段的长度为3的线段个数和空闲线段长度(只有0和1两种可能),然后按照前面说的优先级重载小于号就ok了,非常方便。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <stack>
     16 #include <set>
     17 #include <bitset>
     18 #include <functional>
     19 #include <numeric>
     20 #include <stdexcept>
     21 #include <utility>
     22 
     23 using namespace std;
     24 
     25 #define mem0(a) memset(a, 0, sizeof(a))
     26 #define mem_1(a) memset(a, -1, sizeof(a))
     27 #define lson l, m, rt << 1
     28 #define rson m + 1, r, rt << 1 | 1
     29 #define define_m int m = (l + r) >> 1
     30 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     31 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     32 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     33 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     34 #define all(a) (a).begin(), (a).end()
     35 #define lowbit(x) ((x) & (-(x)))
     36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     39 #define pchr(a) putchar(a)
     40 #define pstr(a) printf("%s", a)
     41 #define sstr(a) scanf("%s", a)
     42 #define sint(a) scanf("%d", &a)
     43 #define sint2(a, b) scanf("%d%d", &a, &b)
     44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     45 #define pint(a) printf("%d
    ", a)
     46 #define test_print1(a) cout << "var1 = " << a << endl
     47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     49 #define mp(a, b) make_pair(a, b)
     50 #define pb(a) push_back(a)
     51 
     52 typedef long long LL;
     53 typedef pair<int, int> pii;
     54 typedef vector<int> vi;
     55 
     56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     58 const int maxn = 4e5 + 7;
     59 const int md = 1e9 + 7;
     60 const int inf = 1e9 + 7;
     61 const LL inf_L = 1e18 + 7;
     62 const double pi = acos(-1.0);
     63 const double eps = 1e-6;
     64 
     65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     68 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     70 int make_id(int x, int y, int n) { return x * n + y; }
     71 
     72 struct Node {
     73     int cnt3, rest;
     74     bool operator < (const Node &that) const {
     75         return that.rest && that.cnt3 || that.cnt3 > cnt3 && !(rest && cnt3);
     76     }
     77     constructInt2(Node, cnt3, rest);
     78 };
     79 
     80 pii node[maxn];
     81 int a[maxn], tot, s, t, x, y, z, m;
     82 int Left[maxn], Right[maxn];
     83 int main() {
     84     //freopen("in.txt", "r", stdin);
     85     while (cin >> s >> t) {
     86         cin >> x >> y >> z;
     87         cin >> m;
     88         rep_up0(i, m) {
     89             sint2(node[i].first, node[i].second);
     90         }
     91         sort(node, node + m);
     92         tot = 0;
     93         int R = -1;
     94         int cs = 0;
     95         rep_up0(i, m) {
     96             if (node[i].first - R > 1) {
     97                 Left[cs] = R + 1;
     98                 Right[cs ++] = node[i].first - 1;
     99             }
    100             max_update(R, node[i].second);
    101         }
    102         if (R < 1e9) {
    103             Left[cs] = R + 1;
    104             Right[cs ++] = 1e9;
    105         }
    106         rep_up0(i, cs) {
    107             int L = max(s, Left[i]), R = min(t, Right[i]);
    108             if (R >= L) a[tot ++] = R - L + 1;
    109         }
    110         sort(a, a + tot);
    111         //rep_up0(i, tot) cout << a[i] << " ";
    112         int sum = 0;
    113         rep_up0(i, tot) sum += a[i];
    114         if (sum < x + 2 * y + 3 * z) {
    115             puts("NO");
    116             continue;
    117         }
    118         priority_queue<Node> Q;
    119         int cnt2 = 0;
    120         rep_up0(i, tot) {
    121             Q.push(Node(a[i] / 3, a[i] % 3 % 2));
    122             cnt2 += a[i] % 3 / 2;
    123         }
    124 
    125         while (!Q.empty()) {
    126             if (cnt2 >= y) break;
    127             Node Hnode = Q.top(); Q.pop();
    128             if (Hnode.rest) {
    129                 if (Hnode.cnt3) {
    130                     cnt2 += 2;
    131                     Q.push(Node(Hnode.cnt3 - 1, 0));
    132                 }
    133             }
    134             else {
    135                 if (Hnode.cnt3) {
    136                     cnt2 ++;
    137                     Q.push(Node(Hnode.cnt3 - 1, 1));
    138                 }
    139             }
    140         }
    141 
    142         int cnt3 = 0;
    143         while (!Q.empty()) {
    144             Node Hnode = Q.top(); Q.pop();
    145             cnt3 += Hnode.cnt3;
    146         }
    147 
    148         puts(cnt2 >= y && cnt3 >= z? "YES" : "NO") ;
    149     }
    150     return 0;
    151 }
    View Code
  • 相关阅读:
    关于DataGrid最后一页只有一行记录时,删除此记录出错的问题
    数据库开发个人总结(ADO.NET小结)
    MSSQL server数据库开发精典技巧
    愈强愈勇(奥运六星)
    中国与日本的生活对比 转)
    古龙妙语录
    DataGrid单击行时改变颜色
    VS.NET 2003 控件命名规范
    qt国际化
    Histogram matching using python, opencv
  • 原文地址:https://www.cnblogs.com/jklongint/p/4509081.html
Copyright © 2011-2022 走看看