CF 1138A Sushi for Two

https://codeforces.com/problemset/problem/1138/A

题意:

  一排寿司,有金枪鱼和鳗鱼两种,找到最大的连续序列,一半全是金枪鱼,另一半全是鳗鱼。

思路:

  遍历一遍,整合成一个序列,代表每种寿司连续出现多少次;

  然后找到相邻对中,较小数最大的那对。

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <math.h>
  5 #include <vector>
  6 #include <algorithm>
  7 using namespace std;
  8 class status
  9 {
 10 public:
 11     int cnt;
 12     int _type;
 13 public:
 14     void print_self(int i)
 15     {
 16         printf("[%d]: type = [%d], cnt = [%d]\n",i, _type, cnt);
 17     }
 18 };
 19 unsigned int sushi[100010];
 20 int main()
 21 {
 22     int cnt = 0;
 23     while(scanf("%d",&cnt)!=EOF)
 24     {
 25         // input    
 26         int ipt;
 27  
 28         memset(sushi,0,sizeof(sushi));
 29         for(int i=0; i<cnt; i++)
 30             scanf("%d",&sushi[i]);
 31         
 32         // calculate
 33         auto kind = sushi[0];
 34         int tmp_cnt = 1;
 35         vector<status> vec;
 36  
 37         for(int i=1; i<cnt;)
 38         {
 39             if(sushi[i] == kind)
 40             {
 41                 tmp_cnt++;
 42                 i++;
 43             }
 44             else
 45             {
 46                 status p;
 47                 p.cnt = tmp_cnt;
 48                 p._type = kind;
 49                 vec.push_back(p);
 50                 tmp_cnt = 1;
 51                 kind = sushi[i];
 52                 i++;
 53             }
 54         }
 55         status p;
 56         p.cnt = tmp_cnt;
 57         p._type = kind;
 58         vec.push_back(p);
 59         int max_cnt = 0;
 60         auto vec_size = vec.size();
 61         
 62         if(vec_size == 1)
 63         {
 64             max_cnt = 0;
 65         }
 66         else if (vec_size == 2)    
 67         {
 68             max_cnt = 2 * min(vec[0].cnt, vec[1].cnt);
 69         }
 70         else
 71         {
 72             for(int i=0; i<vec_size; i++)
 73             {
 74                 if(vec[i]._type != 1)
 75                     continue;
 76                 tmp_cnt = 0;
 77                 if(i == 0)
 78                 {
 79                     tmp_cnt = 2 * min(vec[i].cnt, vec[i+1].cnt);
 80                 }
 81                 else if (i == vec_size-1)
 82                 {
 83                     tmp_cnt = 2 * min(vec[i].cnt, vec[i-1].cnt);
 84                 }
 85                 else
 86                 {
 87                     tmp_cnt = max(vec[i-1].cnt, vec[i+1].cnt);
 88                     tmp_cnt = 2 * min(vec[i].cnt, tmp_cnt);
 89                 }
 90                 max_cnt = max(tmp_cnt, max_cnt);    
 91             }
 92         }
 93         
 94  
 95         // output
 96         printf("%d\n", max_cnt);
 97  
 98     }    
 99     return 0;
100 }

 

posted @ 2019-03-25 16:48  dutbyy  阅读(114)  评论(0)    收藏  举报