USACO 1.1 - Broken Necklace(杂题)
2014-10-21 18:25
375 查看
You have a necklace of N red, white, or blue beads (3<=N<=350)some of which are red, others blue, and others white, arranged at random.Here are two examples for n=29:
The beads considered first and second in the text that follows havebeen marked in the picture.
The configuration in Figure A may be represented as a string of b'sand r's, where b represents a blue bead and r represents a red one, asfollows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it outstraight, and then collect beads of the same color from one enduntil you reach a bead of a different color, and do the same forthe other end (which might not be of the same color as the beadscollected
before this).
Determine the point where the necklace should be broken so that themost number of beads can be collected.
In some necklaces, white beads had been included as shown inFigure B above. When collecting beads,a white bead that isencountered may be treated as either red or blue and then paintedwith the desired color. The string that represents thisconfiguration
can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can becollected from a supplied necklace.
题意:
题目中的字母形成环,求出任意一个节点向左向右得到的相同颜色的珠子的总和,求出最大和。
思路:
将原串再复制一遍使环变成线性,循环一遍得到每一个节点的和,得到最大值即答案。
CODE;
1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r b b b b b b r b r r b r b r r r b r r r r r r b r b r r r w Figure A Figure B r red bead b blue bead w white bead
The beads considered first and second in the text that follows havebeen marked in the picture.
The configuration in Figure A may be represented as a string of b'sand r's, where b represents a blue bead and r represents a red one, asfollows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .
Suppose you are to break the necklace at some point, lay it outstraight, and then collect beads of the same color from one enduntil you reach a bead of a different color, and do the same forthe other end (which might not be of the same color as the beadscollected
before this).
Determine the point where the necklace should be broken so that themost number of beads can be collected.
Example
For example, for the necklace in Figure A, 8 beads can be collected,with the breaking point either between bead 9 and bead 10 or else betweenbead 24 and bead 25.In some necklaces, white beads had been included as shown inFigure B above. When collecting beads,a white bead that isencountered may be treated as either red or blue and then paintedwith the desired color. The string that represents thisconfiguration
can include any of the three symbols r, b and w.
Write a program to determine the largest number of beads that can becollected from a supplied necklace.
PROGRAM NAME: beads
INPUT FORMAT
Line 1: | N, the number of beads |
Line 2: | a string of N characters, each of whichis r, b, or w |
SAMPLE INPUT (file beads.in)
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
OUTPUT FORMAT
A single line containing the maximum of number of beads that can becollected from the supplied necklace.SAMPLE OUTPUT (file beads.out)
11
OUTPUT EXPLANATION
Consider two copies of the beads (kind of like being able to runaroundthe ends). The string of 11 is marked.Two necklace copies joined here v wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ******|***** rrrrrb bbbbb <-- assignments rrrrr#bbbbbb 5 x r 6 x b <-- 11 total
题意:
题目中的字母形成环,求出任意一个节点向左向右得到的相同颜色的珠子的总和,求出最大和。
思路:
将原串再复制一遍使环变成线性,循环一遍得到每一个节点的和,得到最大值即答案。
CODE;
/* ID:sotifis3 LANG:C++ TASK:beads */ #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <string> #include <cstring> #include <queue> #include <stack> #include <vector> #include <set> #include <map> const int inf=0xfffffff; typedef long long ll; using namespace std; char s[800]; int n; int cal(int t) { int r = 1, b = 1, sum = 0; for(int i = t; i >= 0; --i){ if(s[i] == 'w') sum++; if(r && s[i] == 'r'){ b = 0; sum ++; } if(b && s[i] == 'b'){ r = 0; sum ++; } if(b == 0 && s[i] == 'b') break; if(r == 0 && s[i] == 'r') break; } r = 1; b = 1; for(int i = t + 1; i < 2*n; ++i){ if(s[i] == 'w') sum++; if(r && s[i] == 'r'){ b = 0; sum ++; } if(b && s[i] == 'b'){ r = 0; sum ++; } if(b == 0 && s[i] == 'b') break; if(r == 0 && s[i] == 'r') break; } return sum; } int main() { //freopen("in", "r", stdin); freopen("beads.in","r",stdin); freopen("beads.out","w",stdout); while(~scanf("%d", &n)){ scanf("%s", s); for(int i = n; i < 2*n; ++i){ s[i] = s[i - n]; } int ans = 0; for(int i = 0; i < 2*n; ++i){ ans = max(ans, cal(i)); } printf("%d\n", min(ans, n)); } return 0; }
相关文章推荐
- 洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace
- [USACO1.1]Broken Necklace C++(错误1)
- USACO section1.1 Broken Necklace
- P1203 [USACO1.1]坏掉的项链Broken Necklace
- 洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace
- USACO 1.1 Broken Necklace(USACO官方)
- USACO 1.1 Broken Necklace
- USACO - Chapter1 Section 1.1 - Broken Necklace
- USACO Section 1.1 Broken Necklace 解题报告
- USACO 1.1 Broken Necklace
- [USACO1.1]坏掉的项链Broken Necklace 题解
- USACO 1.1 Broken Necklace (beads)
- USACO1.1 Broken Necklace 比较分析
- 【USACO1.1_4】★Broken Necklace 破碎的项链
- P1203 [USACO1.1]坏掉的项链Broken Necklace
- USACO 1.1 Broken Necklace
- usaco 1.1:Broken Necklace
- 【搜索】洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace
- USACO-Section1.1 Broken Necklace
- USACO 1.1 Broken Necklace