博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【TopCoder SRM 551 Div2】Solutions
阅读量:4969 次
发布时间:2019-06-12

本文共 3752 字,大约阅读时间需要 12 分钟。

【250】

  Beaver Bindu has some colored bricks. Each color is described by an uppercase letter. Bricks of each color all look exactly the same. You are given a string bricks. Each character of bricks represents the color of one of Bindu's bricks.

  Bindu wants to arrange all her bricks into a row. A row of bricks is nice if there is at most one pair of adjacent bricks which have different colors.
  Return the number of ways in which Bindu can form a nice row, using all her bricks. (Two ways are considered identical if they correspond to the same sequence of brick colors.) 

  显然,如果只有一种颜色,就只有一中排列方案;如果有两种颜色(A、B),就只有两种方案(AB、BA);如果有大于两种颜色,就没有符合题意的方案

class ColorfulBricks{	public:		int countLayouts(string s){			int cnt[100],num=0;			memset(cnt,0,sizeof(cnt));			for(int i=0;i
2) return 0; else return num; }};

 

【500】

  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.

  因为所给的n很小(n≤50),所以幂次的方法也可以接受。

  枚举一个字母为“参照物”,它是不动的,而其他的围绕着它来交换并向它靠拢。处理完所有相同字母的距离之后,将距离排序,从小到大相加,直到最大且小于等于maxstep,更新答案。

template
inline void gmax(T &a,T b){if(a
inline void gmin(T &a,T b){if(a>b)a=b;}class ColorfulChocolates{ public: int maximumSpread(string s,int k){ int dist[200],len=s.size(),ans=0; for(int i=0;i
=0;j--) if(s[i]==s[j]) cnt++,dist[j]=i-j-cnt; sort(dist,dist+len); int tmp=0,x=0,t_ans=1; while(tmp+dist[x]<=k) tmp+=dist[x],t_ans++,x++; gmax(ans,t_ans); } return ans; }};

 

【950】

  Beaver Bindu has N cupcakes. Each cupcake has one of three possible colors. In this problem we will represent the colors by uppercase letters 'A', 'B', and 'C'. Two cupcakes of the same color are indistinguishable. You are given a string cupcakes consisting of exactly N characters. Each character in cupcakes gives the color of one of Bindu's cupcakes. 

  Bindu has N friends, sitting around a round table. She wants to give each friend one of the cupcakes. Moreover, she does not want to give cupcakes of the same color to any pair of friends who sit next to each other. 

  Let X be the number of ways in which she can hand out the cupcakes to her friends. As X can be very large, compute and return the value (X modulo

1,000,000,007).

  五维DP:f[x][i][j][k][l]表示前x个数,用了i个A,j个B,k个C,第x-1位是l(l=1,2,3) 的方案数,至于首尾不相同的问题,可以枚举第一个的颜色,然后后面的DP。

class ColorfulCupcakesDivTwo{	public:		int countArrangements(string s){			const int BASE=1000000007;			int f[51][51][51][51][4],A(0),B(0),C(0),n=s.size(),ans(0);			for(int i=0;i

   但是,上面这种代码是错误的!!!!自己算算多少空间把……绝对会开爆了内存。怎么办怎么办?

   我们发现在转移时总是i+j+k==x,所以k=x-i-j,当i和j一定时,枚举k是多余的,所以可以压掉一维,正确代码如下:

 

class ColorfulCupcakesDivTwo{	public:		int countArrangements(string s){			int A(0),B(0),C(0),n=s.size(),ans=0;			const int BASE=1000000007;			int f[51][51][51][4];			for(int i=0;i

 

 

  人生第一次TC,最后一题因为最后时刻没有点Compile而直接submit失去了AK的机会,分数只有670分(Cha人家的很爽)。名字颜色变成黄色了,下次只能做Div1了,不知道Div1的题目有多难。反正Div2感觉是挺水的。

转载于:https://www.cnblogs.com/Delostik/archive/2012/08/05/2623563.html

你可能感兴趣的文章
在ubuntu14.04上漏洞环境vulndocker的DOCKER搭建
查看>>
HTML 上传图片实用小技巧
查看>>
Android入门:向TextView添加滚动条
查看>>
LeetCode Sparse Matrix Multiplication
查看>>
【转】 《基于MFC的OpenGL编程》Part 8 Colors
查看>>
LeetCode Find K Pairs with Smallest Sums
查看>>
C#XML配置链接Oracle数据库
查看>>
颜色字符串转换
查看>>
Hdu 1011 Starship Troopers 树形dp
查看>>
C++中bool类型变量初值对程序的影响
查看>>
详细介绍如何自研一款"博客搬家"功能
查看>>
PreparedStatement 在mysql下中文乱码解决方案
查看>>
C#_ProgressBar 显示进度数字
查看>>
欧拉回路与欧拉路径
查看>>
奇葩锁
查看>>
根据可用内存决定每次申请多大内存
查看>>
整合 nginx php-fpm
查看>>
docker-compose 快速部署持续集成测试环境 Gitlab+Harbor+Jenkins pipeline 实现 tag run docker Images...
查看>>
URLs
查看>>
关于上一篇随笔的纠正
查看>>