• Codeforces Round #536 (Div. 2)


    A. Lunar New Year and Cross Counting

    签.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define N 510
     5 int n;
     6 char G[N][N];
     7 
     8 int main()
     9 {
    10     while (scanf("%d", &n) != EOF)
    11     {
    12         for (int i = 1; i <= n; ++i) scanf("%s", G[i] + 1);
    13         int res = 0;
    14         for (int i = 2; i < n; ++i) for (int j = 2; j < n; ++j) if (G[i][j] == 'X')
    15         {
    16             if (G[i - 1][j + 1] == 'X' && G[i - 1][j - 1] == 'X' && G[i + 1][j - 1] == 'X' && G[i + 1][j + 1] == 'X')
    17                 ++res;
    18         }
    19         printf("%d
    ", res);
    20     }
    21     return 0;
    22 }
    View Code

    B. Lunar New Year and Food Ordering

    签.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define ll long long
     5 #define N 100010
     6 int n, m;
     7 struct qnode
     8 {
     9     int a, c, id;
    10     void scan(int id) { scanf("%d%d", &a, &c); this->id = id; }
    11     bool operator < (const qnode &other) const
    12     {
    13         if (c != other.c) return c < other.c;
    14         return id < other.id;
    15     }
    16 }q[N];
    17 
    18 
    19 int main()
    20 {
    21     while (scanf("%d%d", &n, &m) != EOF)
    22     {
    23         for (int i = 1; i <= n; ++i) scanf("%d", &q[i].a);
    24         for (int i = 1; i <= n; ++i) scanf("%d", &q[i].c);
    25         for (int i = 1; i <= n; ++i) q[i].id = i;
    26         queue <int> que; 
    27         sort(q + 1, q + 1 + n);
    28         for (int i = 1; i <= n; ++i) que.push(q[i].id);
    29         sort(q + 1, q + 1 + n, [](qnode a, qnode b) { return a.id < b.id; } );
    30         for (int i = 1, d, t; i <= m; ++i)
    31         {
    32             scanf("%d%d", &t, &d);
    33             ll res = 0;
    34             if (q[t].a >= d)
    35             {
    36                 res += 1ll * d * q[t].c;
    37                 q[t].a -= d;
    38                 d = 0;
    39             }
    40             else
    41             {
    42                 res += 1ll * q[t].a * q[t].c;
    43                 d -= q[t].a;
    44                 q[t].a = 0;
    45                 while (!que.empty() && d)   
    46                 {
    47                     int top = que.front(); 
    48                     if (q[top].a >= d)
    49                     {
    50                         res += 1ll * d * q[top].c;
    51                         q[top].a -= d;
    52                         d = 0; 
    53                         break;
    54                     }
    55                     else
    56                     {
    57                         res += 1ll * q[top].c * q[top].a;
    58                         d -= q[top].a;
    59                         q[top].a = 0;
    60                         que.pop();
    61                     }
    62                 }
    63             }
    64             if (d > 0) res = 0; 
    65             printf("%lld
    ", res);
    66         }
    67     }
    68     return 0;
    69 }
    View Code

    C. Lunar New Year and Number Division

    签.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define ll long long
     5 #define N 300010
     6 int n;
     7 ll a[N];
     8 
     9 int main()
    10 {
    11     while (scanf("%d", &n) != EOF)
    12     {
    13         for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
    14         sort(a + 1, a + 1 + n);
    15         ll res = 0;
    16         for (int i = 1; i <= n >> 1; ++i)
    17         {
    18             res += 1ll * (a[i] + a[n - i + 1]) * (a[i] + a[n - i + 1]);
    19         }
    20         printf("%lld
    ", res);
    21     }
    22     return 0;
    23 }
    View Code

    D. Lunar New Year and a Wander

    签.

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 #define N 100010
     5 int n, m;
     6 vector <int> G[N];
     7 vector <int> res;
     8 
     9 int used[N];
    10 void BFS()
    11 {
    12     priority_queue <int, vector <int>, greater <int> > pq; pq.push(1);
    13     res.clear();
    14     memset(used, 0, sizeof used); 
    15     used[1] = 1;
    16     while (!pq.empty())
    17     {
    18         res.push_back(pq.top());
    19         int u = pq.top(); pq.pop();
    20         for (auto v : G[u]) if (!used[v])
    21         {
    22             used[v] = 1;
    23             pq.push(v);
    24         }
    25     }
    26 }
    27 
    28 int main()
    29 {
    30     while (scanf("%d%d", &n, &m) != EOF)
    31     {
    32         for (int i = 1; i <= n; ++i) G[i].clear();
    33         for (int i = 1, u, v; i <= m; ++i)
    34         {
    35             scanf("%d%d", &u, &v);
    36             G[u].push_back(v);
    37             G[v].push_back(u);
    38         }
    39         BFS();
    40         for (int i = 0; i < n; ++i) printf("%d%c", res[i], " 
    "[i == n - 1]);
    41     }
    42     return 0;
    43 }
    View Code

    E. Lunar New Year and Red Envelopes

    Solved.

    题意:

    有$n个红包,每个红包可以在[s_i, t_i]的时间内拾起,拾起后获得w_i元,但是到d_i这个时刻都不能再获取其他红包$

    $a非常贪心,他在每一时刻,如果这个时刻可以拾起红包,它会拾起金额最大的,如果同时有多个金额最大的$

    $他会拾起d_i最大的$

    $他的女儿可以有m次干扰他的机会,每次干扰他使得他在某个时刻不能拾起红包$

    $如果他的女红采用最优操作,他最少获得多少金钱$

    思路:

    我们发现在某个时刻如果可以拾起红包,那么这个红包已经确定,可以用线段树预处理

    再考虑$dp[i][j] 表示在第i个时刻,用了j次机会的最少金币$

    $有两个转移$

    $一个是 这个时刻如果没有红包可以拿 那么$

    $dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])$

    如果有红包可以拿

    $dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j])$

    $dp[d_i + 1][j] = min(dp[d_i + 1][j], dp[i][j] + w_i)$

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 
      4 #define ll long long
      5 #define INFLL 0x3f3f3f3f3f3f3f3f
      6 #define N 100010
      7 #define M 210
      8 int n, m, k;
      9 ll f[N][M];
     10 
     11 namespace SEG
     12 {
     13     struct node
     14     {
     15         int w, d;
     16         void init()
     17         {
     18             w = d = 0;
     19         }
     20         void add(int ww, int dd)
     21         {
     22             if (ww > w)
     23             {
     24                 w = ww;
     25                 d = dd;
     26             }    
     27             else if (ww == w && dd > d)
     28             {
     29                 d = dd;
     30             }
     31         }
     32     }a[N << 2], res;
     33     void init()
     34     {
     35         memset(a, 0, sizeof a);
     36     }
     37     void pushdown(int id)
     38     {
     39         a[id << 1].add(a[id].w, a[id].d);
     40         a[id << 1 | 1].add(a[id].w, a[id].d);
     41     }
     42     void update(int id, int l, int r, int ql, int qr, int w, int d)
     43     {
     44         if (l >= ql && r <= qr)
     45         {
     46             a[id].add(w, d);
     47             return;    
     48         }
     49         int mid = (l + r) >> 1;
     50         pushdown(id);
     51         if (ql <= mid) update(id << 1, l, mid, ql, qr, w, d);
     52         if (qr > mid) update(id << 1 | 1, mid + 1, r, ql, qr, w, d);
     53     }
     54     void query(int id, int l, int r, int pos)
     55     {
     56         if (l == r) 
     57         {
     58             res = a[id];
     59             return;
     60         }
     61         int mid = (l + r) >> 1;
     62         pushdown(id);
     63         if (pos <= mid) query(id << 1, l, mid, pos);
     64         else query(id << 1 | 1, mid + 1, r, pos);
     65     }
     66 
     67 }
     68 
     69 int main()
     70 {
     71     while (scanf("%d%d%d", &n, &m, &k) != EOF)
     72     {
     73         SEG::init();
     74         memset(f, 0x3f, sizeof f);
     75         int last = 0;
     76         for (int i =1, s, t, d, w; i <= k; ++i)
     77         {
     78             scanf("%d%d%d%d", &s, &t, &d, &w);
     79             last = max(last, d); 
     80             SEG::update(1, 1, n, s, t, w, d); 
     81         }
     82         for (int i = 0; i <= m; ++i) f[1][i] = 0; 
     83         for (int i = 1; i <= n; ++i)
     84         {
     85             SEG::res.init();
     86             SEG::query(1, 1, n, i);
     87             int w = SEG::res.w, d = SEG::res.d;
     88             for (int j = 0; j <= m; ++j) 
     89             {
     90                 if (d)
     91                 {
     92                     f[d + 1][j] = min(f[d + 1][j], f[i][j] + w);
     93                     f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j]); 
     94                 }
     95                 else
     96                     f[i + 1][j] = min(f[i + 1][j], f[i][j]);
     97             }
     98         }
     99         ll res = INFLL;
    100         for (int i = 0; i <= m; ++i) res = min(res, f[n + 1][i]);
    101         printf("%lld
    ", res); 
    102     }
    103     return 0;
    104 }
    View Code
  • 相关阅读:
    Django(七)缓存、信号、Form
    Django(六)Session、CSRF、中间件
    Django(五)母版继承、Cookie、视图装饰器等
    Django(四) ORM 外键操作及初识Ajax
    Django(三) ORM 数据库操作
    Django(二)路由系统、视图、模板
    wc命令
    df命令
    rm命令
    mv命令
  • 原文地址:https://www.cnblogs.com/Dup4/p/10344733.html
Copyright © 2020-2023  润新知