• Codeforces Round #229 (Div. 2) 解题报告


    开了个小号去做div2写一下解题报告。

    Problem A Inna and Alarm Clock

    简单题。直接开数组标记一下即可。代码如下:

     1 /**************************************************
     2  * Author     : xiaohao Z
     3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
     4  * Last modified : 2014-02-11 23:26
     5  * Filename     : Round_229_2_A.cpp
     6  * Description     : 
     7  * ************************************************/
     8 
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <cmath>
    14 #include <algorithm>
    15 #include <queue>
    16 #include <stack>
    17 #include <vector>
    18 #include <set>
    19 #include <map>
    20 #define MP(a, b) make_pair(a, b)
    21 #define PB(a) push_back(a)
    22 
    23 using namespace std;
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 typedef pair<unsigned int,unsigned int> puu;
    27 typedef pair<int, double> pid;
    28 typedef pair<ll, int> pli;
    29 typedef pair<int, ll> pil;
    30 
    31 const int INF = 0x3f3f3f3f;
    32 const double eps = 1E-6;
    33 const int LEN = 101;
    34 
    35 int main()
    36 {
    37 //    freopen("in.txt", "r", stdin);
    38 
    39     int n, a, b, col[LEN], row[LEN];
    40     while(scanf("%d", &n)!=EOF){
    41         memset(row, 0, sizeof row);
    42         memset(col, 0, sizeof col);
    43         for(int i=0; i<n; i++){
    44             scanf("%d%d", &a, &b);
    45             col[b] = 1;row[a] = 1;
    46         }
    47         a = b = 0;
    48         for(int i=0; i<LEN; i++){
    49             if(col[i]) a++;
    50             if(row[i]) b++;
    51         }
    52         printf("%d
    ", min(a, b));
    53     }
    54     return 0;
    55 }
    View Code

    Problem B Inna, Dima and Song

    思路:首先(b[i] > 1 && b[i] <= a[i]*2)才能满足能弹出来,否则-1.满足的话(ll)(b[i]/2)*(ll)(b[i]-(b[i]/2))即是最大之(注意我的强制类型转换否则会超或者直接用ll)

    代码如下:

     1 /**************************************************
     2  * Author     : xiaohao Z
     3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
     4  * Last modified : 2014-02-11 23:28
     5  * Filename     : Round_229_2_B.cpp
     6  * Description     : 
     7  * ************************************************/
     8 
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <cmath>
    14 #include <algorithm>
    15 #include <queue>
    16 #include <stack>
    17 #include <vector>
    18 #include <set>
    19 #include <map>
    20 #define MP(a, b) make_pair(a, b)
    21 #define PB(a) push_back(a)
    22 
    23 using namespace std;
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 typedef pair<unsigned int,unsigned int> puu;
    27 typedef pair<int, double> pid;
    28 typedef pair<ll, int> pli;
    29 typedef pair<int, ll> pil;
    30 
    31 const int INF = 0x3f3f3f3f;
    32 const double eps = 1E-6;
    33 const int LEN = 100010;
    34 int a[LEN], b[LEN];
    35 
    36 int main()
    37 {
    38 //    freopen("in.txt", "r", stdin);
    39 
    40     int n;
    41     while(scanf("%d", &n)!=EOF){
    42         for(int i=0; i<n; i++){
    43             scanf("%d", &a[i]);
    44         }
    45         for(int i=0; i<n; i++){
    46             scanf("%d", &b[i]);
    47         }
    48         ll ans = 0;
    49         for(int i=0; i<n; i++){
    50                if(b[i] > 1 && b[i] <= a[i]*2)ans+=(ll)(b[i]/2)*(ll)(b[i]-(b[i]/2));    
    51             else ans --;
    52         }
    53         printf("%I64d
    ", ans);
    54     }
    55     return 0;
    56 }
    View Code

    Problem C Inna and Candy Boxes

    题意:有一排盒子每个盒子里面两种情况有一个糖和没有。给长度n,整数k。然后m个区间查询要求把[l,r]中变成只有l+tk-1有糖需要几步操作。每一次操作能拿走一颗糖,放进一颗糖。

    思路:利用k比较小的特征把k看成一个循环节,对k的余数分别递推dp[i]表示i之前和i模k相等的有几颗糖。在更具询问统计循环节上的每一个就行了。

    代码如下:

     1 /**************************************************
     2  * Author     : xiaohao Z
     3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
     4  * Last modified : 2014-02-11 23:27
     5  * Filename     : Round_229_2_C.cpp
     6  * Description     : 
     7  * ************************************************/
     8 
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <cmath>
    14 #include <algorithm>
    15 #include <queue>
    16 #include <stack>
    17 #include <vector>
    18 #include <set>
    19 #include <map>
    20 #define MP(a, b) make_pair(a, b)
    21 #define PB(a) push_back(a)
    22 
    23 using namespace std;
    24 typedef long long ll;
    25 typedef pair<int, int> pii;
    26 typedef pair<unsigned int,unsigned int> puu;
    27 typedef pair<int, double> pid;
    28 typedef pair<ll, int> pli;
    29 typedef pair<int, ll> pil;
    30 
    31 const int INF = 0x3f3f3f3f;
    32 const double eps = 1E-6;
    33 const int LEN = 100000+10;
    34 int dp[LEN];
    35 char str[LEN];
    36 int n, k, m, l, r;
    37 
    38 int main()
    39 {
    40 //    freopen("in.txt", "r", stdin);
    41 
    42     while(scanf("%d%d%d", &n, &k, &m)!=EOF){
    43         getchar();
    44         gets(str);
    45         for(int i=0; i<k; i++) dp[i] = (str[i]=='1'?1:0);
    46         for(int i=k; i<n; i++) dp[i] = dp[i-k]+(str[i]=='1'?1:0);
    47         for(int i=0; i<m; i++){
    48             scanf("%d%d", &l, &r);
    49             l--;r--;
    50             int ans = 0;
    51             for(int i=1; i<=k; i++){
    52                 int hi, lo;
    53                 if(l-i < 0) lo = 0;
    54                 else lo = dp[l-i];
    55                 if(r+1-i < 0) hi = 0;
    56                 else hi = dp[r+1-i];
    57                 if(i==1) ans += (((r-l+1)/k) - (hi-lo));
    58                 else ans += (hi-lo);
    59             }
    60             printf("%d
    ", ans);
    61         }
    62     }
    63     return 0;
    64 }
    View Code

    Problem D Inna and Sweet Matrix

    题意:在一张地图上一开始全空。每次选一条路上没有糖的路径走过去放一颗糖。每格只能放一颗,有k颗问你放完的最小花费是多少?花费为走过的总路程。

    思路:贪心思想。按bfs顺序确定放糖位置,然后每次放最远的糖,走过去按x先增y后增。具体参照我的排序函数。

    代码如下:

      1 /**************************************************
      2  * Author     : xiaohao Z
      3  * Blog     : http://www.cnblogs.com/shu-xiaohao/
      4  * Last modified : 2014-02-11 23:29
      5  * Filename     : Round_229_2_D.cpp
      6  * Description     : 
      7  * ************************************************/
      8 
      9 #include <iostream>
     10 #include <cstdio>
     11 #include <cstring>
     12 #include <cstdlib>
     13 #include <cmath>
     14 #include <algorithm>
     15 #include <queue>
     16 #include <stack>
     17 #include <vector>
     18 #include <set>
     19 #include <map>
     20 #define MP(a, b) make_pair(a, b)
     21 #define PB(a) push_back(a)
     22 
     23 using namespace std;
     24 typedef long long ll;
     25 typedef pair<int, int> pii;
     26 typedef pair<unsigned int,unsigned int> puu;
     27 typedef pair<int, double> pid;
     28 typedef pair<ll, int> pli;
     29 typedef pair<int, ll> pil;
     30 
     31 const int INF = 0x3f3f3f3f;
     32 const double eps = 1E-6;
     33 const int LEN = 55;
     34 int Map[LEN][LEN], n, m;
     35 struct P{int x, y;};
     36 struct cmp{
     37     bool operator() (P a, P b){
     38         if(a.x+a.y!=b.x+b.y) return a.x+a.y < b.x+b.y;
     39         else return a.x > b.x; 
     40     }
     41 };
     42 priority_queue<P, vector<P>, cmp> pq;
     43 P MPP(int a, int b){
     44     P ret;
     45     ret.x = a;
     46     ret.y = b;
     47     return ret;
     48 }
     49 int xx[] = {0, 0, 1,-1};
     50 int yy[] = {1,-1, 0, 0};
     51 
     52 
     53 int put(int num){
     54     int ret = 1;
     55     queue<pii> q;
     56     q.push(MP(0,0));
     57     Map[0][0] = 1;
     58     pq.push(MPP(0,0));
     59     num--; if(num == 0) return ret;
     60     while(!q.empty()){
     61         pii nv = q.front(); q.pop();
     62         for(int i=0; i<4; i++){
     63             int tx = nv.first+xx[i];
     64             int ty = nv.second+yy[i];
     65             if(tx >= 0 && tx < n && ty >= 0 && ty < m && !Map[tx][ty]){
     66                 Map[tx][ty] = 1;
     67                 q.push(MP(tx, ty));
     68                 pq.push(MPP(tx, ty));
     69                 num--;
     70                 ret += (tx+ty+1);
     71                 if(num == 0) return ret;
     72             }
     73         }
     74     }
     75 }
     76 
     77 int main()
     78 {
     79 //    freopen("in.txt", "r", stdin);
     80     
     81     int st;
     82     while(scanf("%d%d%d", &n, &m, &st)!=EOF){
     83         memset(Map, 0, sizeof Map);
     84         while(!pq.empty())pq.pop();
     85         int ans = put(st);
     86         printf("%d
    ", ans);
     87         while(!pq.empty()){
     88             P nv = pq.top();pq.pop();
     89             nv.x++,nv.y++;
     90             int x = 1, y = 1;
     91             while(1){
     92                 if(x == nv.x && y == nv.y){
     93                     printf("(%d,%d)
    ", nv.x, nv.y);
     94                     break;
     95                 }
     96                 printf("(%d,%d) ", x, y);
     97                 if(x < nv.x) x++;
     98                 else if(y < nv.y) y++;
     99             }
    100         }
    101     }
    102     return 0;
    103 }
    View Code
    奔跑吧!少年!趁着你还年轻
  • 相关阅读:
    jsonp的实现原理和缺点
    vue $set的使用初了解
    js将多维数组转换为一维数组
    微信小程序之页面打开数量限制
    ZOJ 1463 Brackets Sequence
    CSUOJ 1248 非变性聚丙烯酰胺凝胶电泳
    ZOJ 1524 Supermarket
    POJ 1012 Joseph
    ZOJ 1276 Optimal Array Multiplication Sequence
    POJ 1742 Coins
  • 原文地址:https://www.cnblogs.com/shu-xiaohao/p/3545266.html
Copyright © 2020-2023  润新知