zoukankan      html  css  js  c++  java
  • SRM551

    万年没做过topcoder了,离不做ACM也一年多了。

    每个月都会收到tc,cf这些比赛的邮件,但是都懒得去做。

    昨晚又一次开始做。

    不得不说真心退步了好多好多。

    本来就很菜,所以就DIV II了T_T~~~

    250就不说了,纯水。

    竟然坑爹的DIV II的500都卡了下。。。导致1000都没开。。。

    伤心爆了啊。。。

    Beaver Bindu has some chocolates arranged in a row. The wrapping of each chocolate has a single color. Multiple chocolates can share the same color. In this problem, each of the possible colors is represented by an uppercase letter. You are given a string chocolates. For each i, the i-th chocolate (0-based index) in the row has the color chocolates[i].  The spread of a row of chocolates is the maximum number of adjacent chocolates that all share the same color. Formally, the spread can be defined as the maximum value of (j-i+1), where i <= j and all the chocolates in the positions between i and j, inclusive, have the same color.  You are also given an int maxSwaps. Bindu can swap any two adjacent chocolates. She has decided to make at most maxSwaps such swaps.  Return the maximum spread she can obtain. 

    500题意思如上。

    就是有n个东西,有不同的颜色,每次可以交换两个相邻的位置。

    问在不大于maxSwaps的步骤内,相同颜色的连续的能有多长。

    好吧,就是一个暴力题,n最大是50。

    枚举以每个为中心,然后maxSwaps步内最长有多长就行。

    具体操作就是:

    1、枚举n个格子

    2、以当前的i个格子为中心,然后记录两边相同颜色的格子移动过来需要的步数。

    3、将步数从小到大排序。

    4、从小步数选起,满足总步数不超过maxSwaps

     1 #include <vector>
     2 #include <list>
     3 #include <map>
     4 #include <set>
     5 #include <deque>
     6 #include <stack>
     7 #include <bitset>
     8 #include <algorithm>
     9 #include <functional>
    10 #include <numeric>
    11 #include <utility>
    12 #include <sstream>
    13 #include <iostream>
    14 #include <iomanip>
    15 #include <cstdio>
    16 #include <cmath>
    17 #include <cstdlib>
    18 #include <ctime>
    19 
    20 using namespace std;
    21 
    22 class ColorfulChocolates {
    23 public:
    24     int maximumSpread(string, int);
    25 };
    26 
    27 int ColorfulChocolates::maximumSpread(string s, int maxSwaps) {
    28     int ans = 0;
    29     for(int i = 0 ; i < s.size() ; i++){
    30         int cnt = 0;
    31         int n = 0;
    32         int path[2000] = {0};
    33         for(int j = i + 1 ; j < s.size() ; j++)
    34             if(s[j] == s[i]){
    35                 path[n++] = j - i - 1 - cnt;
    36                 cnt++;
    37             }
    38         cnt = 0;
    39         for(int j = i -1 ; j >= 0 ; j--)
    40             if(s[j] == s[i]){
    41                 path[n++] = i - j - 1 - cnt;
    42                 cnt++;
    43             }
    44         sort(path , path+n);
    45         
    46         int tmp = 0;
    47         int x = 1;
    48         for(int j = 0 ; j < n ; j++){
    49             if(tmp + path[j] <= maxSwaps)
    50                 {tmp += path[j];x++;}
    51             else break;
    52         }
    53         ans = max(ans , x);
    54         
    55         
    56     }
    57     return ans;
    58     
    59 }
    60 
    61 
    62 //Powered by [KawigiEdit] 2.0TIJLNWNRXPQPBETEWQFFSEYZPJSIIWYDEHOSEDLDTEUUINAGGH!
    by 1957
  • 相关阅读:
    java String format格式字符串语法
    spring 小示例 yongqi
    mysql GROUP_CONCAT()函数最大长度之坑 yongqi
    连接数据库超时设置autoReconnect=true mysql经典的8小时问题 yongqi
    Kafka 可视化工具(Kafka Tool) yongqi
    kettle 优化 yongqi
    mysql 修改字段名 yongqi
    SQL: Cannot drop database XXX because it is currently in use解决方法 yongqi
    .Net Core 控制台应用程序 依赖注入
    记一次部署Skywalking(基于Elasticsearch),并使用 .NET6接入Skywalking
  • 原文地址:https://www.cnblogs.com/x1957/p/2623999.html
Copyright © 2011-2022 走看看