锘??xml version="1.0" encoding="utf-8" standalone="yes"?> This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. 瀛楃涓叉按棰橈紒 249.23PT錛侊紒錛?/p> You are given a rectangular map in which each space is marked with one of three characters: '.' (open), 'B' (a brick), or '#' (an indestructible block). Walls made of indestructible blocks surround the field on all sides, but are not shown on the map. A ball is bouncing around this map, destroying bricks, and your task is determine how long it takes the ball to destroy all the bricks. The top left space of the map is always open, and this is where the ball begins. More specifically, the ball begins at time 0 in the middle of the top edge of this space (see the diagram in Example 0). The ball is traveling diagonally down and to the right at a speed of half a meter per second vertically, and half a meter per second horizontally. Each space is 1 meter square, so the ball crosses half a space vertically and half a space horizontally each second. Whenever the ball strikes the edge of an obstacle--either a brick or an indestructible block--it bounces off at an angle perpendicular to its incoming path. The ball will never hit two obstacles simultaneously. Whenever the ball bounces off a brick, the brick is destroyed and removed from the map. Your method should return the time at which the last brick is destroyed. If one or more bricks will never be destroyed, return -1. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. The digits 3 and 9 share an interesting property. If you take any multiple of 3 and sum its digits, you get another multiple of 3. For example, 118*3 = 354 and 3+5+4 = 12, which is a multiple of 3. Similarly, if you take any multiple of 9 and sum its digits, you get another multiple of 9. For example, 75*9 = 675 and 6+7+5 = 18, which is a multiple of 9. Call any digit for which this property holds interesting, except for 0 and 1, for which the property holds trivially. A digit that is interesting in one base is not necessarily interesting in another base. For example, 3 is interesting in base 10 but uninteresting in base 5. Given an int base, your task is to return all the interesting digits for that base in increasing order. To determine whether a particular digit is interesting or not, you need not consider all multiples of the digit. You can be certain that, if the property holds for all multiples of the digit with fewer than four digits, then it also holds for multiples with more digits. For example, in base 10, you would not need to consider any multiples greater than 999. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. When a widget breaks, it is sent to the widget repair shop, which is capable of repairing at most numPerDay widgets per day. Given a record of the number of widgets that arrive at the shop each morning, your task is to determine how many days the shop must operate to repair all the widgets, not counting any days the shop spends entirely idle. For example, suppose the shop is capable of repairing at most 8 widgets per day, and over a stretch of 5 days, it receives 10, 0, 0, 4, and 20 widgets, respectively. The shop would operate on days 1 and 2, sit idle on day 3, and operate again on days 4 through 7. In total, the shop would operate for 6 days to repair all the widgets. Create a class WidgetRepairs containing a method days that takes a sequence of arrival counts arrivals (of type vector <int>) and an int numPerDay, and calculates the number of days of operation. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
Problem Statement
NOTE: This problem statement contains images that may not display properly if viewed outside of the applet.
We had a rectangular grid that consisted of W x H square cells. We placed a robot on one of the cells. The robot then followed the rules given below.
For example, the following seven images show a series of moves made by the robot in a 12 x 11 board:
We forgot the dimensions of the grid and the original (x,y) coordinates of the cell on which the robot was originally placed, but we do remember its movement. You are given a vector <int> moves. This sequence of positive integers shall be interpreted as follows: The robot performed moves[0] steps eastwards, turned left, performed moves[1] steps northwards, turned left, and so on. After performing the last sequence of steps, the robot stopped. (Either it detected that it should terminate, or we stopped it manually.) We are not sure if the sequence of moves is valid. If the sequence of moves is impossible, return -1. Else, return the minimum area of a grid in which the sequence of moves is possible. (I.e., the return value is the smallest possible value of the product of W and H.). Definition
Class: RotatingBot Method: minArea Parameters: vector <int> Returns: int Method signature: int minArea(vector <int> moves) (be sure your method is public) Constraints
- moves will contain between 1 and 50 elements, inclusive. - Each element of moves will be between 1 and 50, inclusive. Examples
0)
{15} Returns: 16
The smallest valid board is a 16x1 board, with the robot starting on the west end of the board. 1)
{3,10} Returns: 44
The smallest solution is to place the robot into the southwest corner of a 4 x 11 board. 2)
{1,1,1,1} Returns: -1
This sequence of moves is not possible because the robot would return to its initial location which is forbidden. 3)
{9,5,11,10,11,4,10} Returns: 132
These moves match the image from the problem statement. 4)
{12,1,27,14,27,12,26,11,25,10,24,9,23,8,22,7,21,6,20,5,19,4,18,3,17,2,16,1,15} Returns: 420
5)
{8,6,6,1} Returns: -1
6)
{8,6,6} Returns: 63
7)
{5,4,5,3,3} Returns: 30
妯℃嫙棰樼洰錛岀洿鎺ユ寜鐓х粰瀹氬簭鍒楅亶鍘嗭紝濡傛灉閬囧埌涓嶅悎娉曠殑鎯呭喌return -1銆傝瀹氬洓涓柟鍚戯紝mw,me,mn,ms錛屼笢鍖楄タ鍗楋紝鍒濆涓轟笂涓嬬晫銆傜劧鍚庝緷嬈℃壘鍒拌寖鍥淬?br />鍒濆鐐?100,100)銆?br />鏈鍚庣殑闈㈢Н涓?me-mw+1)*(ms-mn+1)
娌℃湁鑰冭檻瀹屽叏鍟婏紝鎬濈淮榪樻槸涓嶄弗璋ㄤ笉瀹屾暣錛屽氨WA浜嗕竴緇勬暟鎹紝鏄ㄦ櫄鍋氬氨鎮插墽浜嗭紒錛侊紒錛?br />鎴戞槸寮辮彍錛岀戶緇姫鍔涳紒
涓嶉敊鐨勬ā鎷熼鐩紝闇瑕佽冭檻鐨勪笢瑗垮緢澶氾紒
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define swap(t,x,y) (t=x,x=y,y=t)
#define clr(list) memset(list,0,sizeof(list))
using namespace std;
bool maps[205][205];
int go[4][2]={{1,0},{0,-1},{-1,0},{0,1}};
class RotatingBot{
public:
int minArea(vector <int> moves){
int n=moves.size();
int me=1000,mw=0,mn=0,ms=1000;
int tmp;
int dir;
int x,y,xx,yy;
clr(maps);
maps[100][100]=1;
x=100,y=100,dir=0;
for (int i=0;i<n ;i++)
{
tmp=moves[i];
for (int j=0;j<tmp;j++)
{
xx=x+go[dir][0];yy=y+go[dir][1];
if (maps[xx][yy] || (xx<mw || xx>me || yy<mn || yy>ms))
return -1;
else
{
maps[xx][yy]=1;
x=xx;y=yy;
}
}
xx=x+go[dir][0];yy=y+go[dir][1];
if (!maps[xx][yy])
{
if (dir==0)
if (me==1000)
me=x;
else
if (x<me && i<n-1)
return -1;
if (dir==1)
if (mn==0)
mn=y;
else if (y>mn && i<n-1)
return -1;
if (dir==2) //榪欓噷WA浜唗esting 5 & 6
if (mw==0 && x<=100)
mw=x;
else
if (x>mw && i<n-1)
return -1;
if (dir==3) //鏈鍚庢牻鍦ㄨ繖閲屼簡
if (ms==1000 && y>=100)
ms=y;
else if ((y<ms || y<100) && i<n-1)
return -1;
}
dir=(dir+1) % 4;
}
// return mw;
if (mw==0)
mw=100;
if (mn==0)
mn=100;
if (ms==1000)
ms=100;
return (me-mw+1)*(ms-mn+1);
}
};
]]>
Problem Statement
We have a string originalWord. Each character of originalWord is either 'a' or 'b'. Timmy claims that he can convert it to finalWord using exactly k moves. In each move, he can either change a single 'a' to a 'b', or change a single 'b' to an 'a'.
You are given the strings originalWord and finalWord, and the int k. Determine whether Timmy may be telling the truth. If there is a possible sequence of exactly k moves that will turn originalWord into finalWord, return "POSSIBLE" (quotes for clarity). Otherwise, return "IMPOSSIBLE". Definition
Class: EasyConversionMachine Method: isItPossible Parameters: string, string, int Returns: string Method signature: string isItPossible(string originalWord, string finalWord, int k) (be sure your method is public) Notes
- Timmy may change the same letter multiple times. Each time counts as a different move. Constraints
- originalWord will contain between 1 and 50 characters, inclusive. - finalWord and originalWord will contain the same number of characters. - Each character in originalWord and finalWord will be 'a' or 'b'. - k will be between 1 and 100, inclusive. Examples
0)
"aababba"
"bbbbbbb"
2
Returns: "IMPOSSIBLE"
It is not possible to reach finalWord in fewer than 4 moves. 1)
"aabb"
"aabb"
1
Returns: "IMPOSSIBLE"
The number of moves must be exactly k=1. 2)
"aaaaabaa"
"bbbbbabb"
8
Returns: "POSSIBLE"
Use each move to change each of the letters once. 3)
"aaa"
"bab"
4
Returns: "POSSIBLE"
The following sequence of 4 moves does the job:
aaa -> baa -> bab -> aab -> bab 4)
"aababbabaa"
"abbbbaabab"
9
Returns: "IMPOSSIBLE"
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <algorithm>
#define min(x,y) (x<y?x:y)
#define max(x,y) (x>y?x:y)
#define swap(t,x,y) (t=x,x=y,y=t)
#define clr(list) memset(list,0,sizeof(list))
using namespace std;
class EasyConversionMachine{
public:
string isItPossible(string originalWord, string finalWord, int k)
{
int n=originalWord.size();
int tot=0;
for (int i=0;i<n;i++)
if (originalWord[i]!=finalWord[i])
tot++;
if (tot<=k && (k-tot)%2==0) //鍌誨弶錛岃繖閲屽垰鍒氬紑濮嬫悶鍙嶄簡錛宼esting WA浜嗕竴嬈?br /> return "POSSIBLE";
return "IMPOSSIBLE";
}
};
]]>
Problem Statement
Definition
Class: BrickByBrick Method: timeToClear Parameters: vector <string> Returns: int Method signature: int timeToClear(vector <string> map) (be sure your method is public) Constraints
- map contains between 1 and 15 elements, inclusive. - All elements of map contain the same number of characters (between 1 and 15, inclusive). - All elements of map contain only the characters '.', 'B', and '#'. - The top left corner of map (that is, the first character of the first element) is '.'. - map contains at least one 'B'. Examples
0)
{ ".B", "BB" } Returns: 6
The following diagram illustrates the path of the ball. __0________ | / \ | | |/ \| | 3 . 1 B | |\ /|\ | | \ / | \ | |--2--|--6--| | | | | | | | B | B | | | | |_____|_____|
The ball begins at point 0, traveling down and to the right. It bounces off the first brick at time 1, and off the second brick at time 2. At time 3, it bounces off the left wall, and at time 4 it bounces off the upper wall at the same place it started. At time 6, the ball destroys the third and final brick, so the method returns 6. 1)
{ ".BB", "BBB", "BBB" } Returns: -1
The ball will never hit the brick in the bottom right corner. 2)
{ "......B", "###.###", "B.....B" } Returns: 35
3)
{ "..BBB...", ".#BB..#.", "B.#B.B..", "B.B.....", "##.B.B#.", "#BB.#.B.", "B..B.BB.", "#..BB..B", ".B....#." } Returns: -1
4)
{ ".BB..BBB.B...", "B.B...B..BB..", "#B...B#B.....", "B#B.B##...##B", "BB.B#.B##B.B#", "B.B#.BBB.BB#B", "B#BBB##.#B#B.", "B#BB.BBB#BB.#" } Returns: 3912
5)
{ ".BBBBBBBBBBBBBB", "##############B", "BBBBBBBBBBBBBBB", "B##############", "BBBBBBBBBBBBBBB", "##############B", "BBBBBBBBBBBBBBB", "B##############", "BBBBBBBBBBBBBBB", "##############B", "BBBBBBBBBBBBBBB", "B##############", "BBBBBBBBBBBBBBB", "##############B", "BBBBBBBBBBBBBBB" } Returns: 31753
棰樻剰錛氬氨鍍忔槸閭d釜鍟ユ父鎴忥紝鐪嬩簡閭d釜鍥懼氨鎸哄ソ鐞嗚В鐨勪簡銆?br /> 娉ㄦ剰鏂瑰悜鍙樻崲銆?br />
鍐欎簡濂戒箙濂戒箙鍟婏紝
404PT錛?br />
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class BrickByBrick
{
public:
int timeToClear(vector <string> map)
{
int time=0;
int bricks=0;
int impactTime=0;
float x,y,dirX,dirY,newX,newY;
for (int i=0; i<map.size(); i++)
{
map[i].insert(0,sizeof(char),'#');
map[i].push_back('#');
}
string s;
for (int i=0; i<map[0].length(); i++)
s.push_back('#');
map.insert(map.begin(),s);
map.push_back(s);
for (int i=0; i<map.size(); i++)
bricks+=count(map[i].begin(),map[i].end(),'B');
x=1.5;
y=1;
dirX=dirY=0.5;
while (bricks && impactTime <1000)
{
impactTime++;
x+=dirX;
y+=dirY;
if (dirX==0.5 && dirY==0.5)
{
if ((int)x==x)
{
newX=(int)x;
newY=(int)y;
}
else if ((int)y==y)
{
newX=(int)x;
newY=(int)y;
}
}
if (dirX==-0.5 && dirY==-0.5)
{
if ((int)x==x)
{
newX=(int)x-1;
newY=(int)y;
}
else if ((int)y==y)
{
newX=(int)x;
newY=(int)y-1;
}
}
if (dirX==-0.5 && dirY==0.5)
{
if ((int)x==x)
{
newX=(int)x-1;
newY=(int)y;
}
else if ((int)y==y)
{
newX=(int)x;
newY=(int)y;
}
}
if (dirX==0.5 && dirY==-0.5)
{
if ((int)x==x)
{
newX=(int)x;
newY=(int)y;
}
else if ((int)y==y)
{
newX=(int)x;
newY=(int)y-1;
}
}
if (map[newY][newX]!='.')
{
if ((int)x==x)
{
if (dirX==0.5 && dirY==0.5)
{
dirX=-0.5;
dirY=0.5;
}
else if (dirX==-0.5 && dirY==-0.5)
{
dirX=0.5;
dirY=-0.5;
}
else if (dirX==-0.5 && dirY==0.5)
{
dirX=0.5;
dirY=0.5;
}
else if (dirX==0.5 && dirY==-0.5)
{
dirX=-0.5;
dirY=-0.5;
}
}
if ((int)y==y)
{
if (dirX==0.5 && dirY==0.5)
{
dirX=0.5;
dirY=-0.5;
}
else if (dirX==0.5 && dirY==-0.5)
{
dirX=0.5;
dirY=0.5;
}
else if (dirX==-0.5 && dirY==0.5)
{
dirX=-0.5;
dirY=-0.5;
}
else if (dirX==-0.5 && dirY==-0.5)
{
dirX=-0.5;
dirY=0.5;
}
}
}
if (map[newY][newX]=='B')
{
map[newY][newX]='.';
time+=impactTime;
impactTime=0;
bricks--;
}
}
if (impactTime==1000)
return -1;
return time;
}
};
]]>
Problem Statement
Definition
Class: InterestingDigits Method: digits Parameters: int Returns: vector <int> Method signature: vector <int> digits(int base) (be sure your method is public) Notes
- When base is greater than 10, digits may have a numeric value greater than 9. Because integers are displayed in base 10 by default, do not be alarmed when such digits appear on your screen as more than one decimal digit. For example, one of the interesting digits in base 16 is 15. Constraints
- base is between 3 and 30, inclusive. Examples
0)
10
Returns: { 3, 9 }
All other candidate digits fail for base=10. For example, 2 and 5 both fail on 100, for which 1+0+0=1. Similarly, 4 and 8 both fail on 216, for which 2+1+6=9, and 6 and 7 both fail for 126, for which 1+2+6=9. 1)
3
Returns: { 2 } 2)
9
Returns: { 2, 4, 8 } 3)
26
Returns: { 5, 25 } 4)
30
Returns: { 29 }
棰樻剰緇欏畾base榪涘埗鏁伴噷錛屾壘鍑洪噷闈㈡墍鏈夌殑鐗瑰埆鐨勬暟x,瀵逛簬x鐨勫嶆暟kx錛屽畠鐨勬瘡涓浣嶇殑鍜岃繕鏄痻鐨勫嶆暟銆?br />姣斿base=10錛屽垯x=3鍜?銆?br />
榪欎釜搴旇鏄皬瀛︽椂鍊欏氨璁板緱鐨勭粨璁轟簡錛屼笓闂ㄧ畻3鍜?鐨勶紝鍝堝搱銆?br />
緇撹錛歜ase %x==1鏄厖瑕佹潯浠躲?br />璇佹槑錛?br /> a=a1*base^0+a2*base^1+a3*base^2,瀵逛簬浠諱綍a閮藉彲浠ヨ繖鏍峰垎瑙?br /> 鍥犱負 base % x=1錛屽垯base^k %x==1銆傚垯涓婂紡鍖栫畝涓?br /> a==a1+a2+a3+.. (mod x)
鍙嶄箣錛屼篃鍙緱base %x==1
440PT鍟婏紝灝忓皬鐘硅鮑浜嗕笅鍝︺傜洿鎺ユ灇涓句簡x楠岃瘉鍟娿?br />
鍏跺疄灝辨槸姹俠ase-1鐨勫洜瀛愶紝濡傛灉base寰堝ぇ鍦拌瘽錛屾槸涓嶈兘榪欐牱鍘繪灇涓劇殑銆傚洜瀛愬垎瑙e摝錛屽張鏄粡鍏擱棶棰樺晩銆?br />
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
class InterestingDigits{
public:
vector <int> digits(int base){
int a[31];
int n=0;
int i=2;
while (i<base){
if (base % i==1)
a[n++]=i;
i++;
}
vector <int> ans(n);
for (i=0;i<n;i++)
ans[i]=a[i];
return ans;
}
};
]]>
Problem Statement
Definition
Class: WidgetRepairs Method: days Parameters: vector <int>, int Returns: int Method signature: int days(vector <int> arrivals, int numPerDay) (be sure your method is public) Constraints
- arrivals contains between 1 and 20 elements, inclusive. - Each element of arrivals is between 0 and 100, inclusive. - numPerDay is between 1 and 50, inclusive. Examples
0)
{ 10, 0, 0, 4, 20 } 8
Returns: 6
The example above. 1)
{ 0, 0, 0 } 10
Returns: 0
2)
{ 100, 100 } 10
Returns: 20
3)
{ 27, 0, 0, 0, 0, 9 } 9
Returns: 4
4)
{ 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6 } 3
Returns: 15
鐩存帴閬嶅巻灝辮錛屾瘡澶╁畬鎴愪笉浜嗙殑鐣欏埌絎簩澶╁仛銆傛渶鍚庢病鏈夊畬鎴愮殑寤惰繜鍒板悗闈㈠仛銆?br />姹傜殑鐨勫仛鐨勫ぉ鏁版槸澶氬皯錛屾病鏈夊仛鐨勯偅澶╀笉鐢ㄧ畻銆?br />
228PT錛岄熷害榪樻槸鎱簡鐐廣?br />
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
class WidgetRepairs{
public:
int days(vector <int> arrivals, int numPerDay){
int n=arrivals.size();
int now=0;
int i=0;
int tot=0;
while (i<n || now) //榪欓噷鍙互浼樺寲涓涓嬶紝濡傛灉i==n灝變笉鐢ㄥ驚鐜簡錛屽墿涓嬬殑闇瑕佸畬鎴愮殑澶╂暟涓?now-1)/numPerDay+1銆?br /> {
if (i<n)
now+=arrivals[i];
if (now>0) //絎竴嬈¤繖閲岀悊瑙i敊浜嗭紝浠ュ悗瑕佹妸緇嗚妭鎼炴竻妤氬晩銆傘傘?br /> tot++;
now=now>numPerDay?now-numPerDay:0;
i++;
}
return tot;
}
};
]]>
Problem Statement
The Order of the Hats is a magical organization. One of their duties is to teach students how to cast spells. There are N spells numbered from 0 to N-1. As an aid for the students, the teachers have prepared a spell chart. The chart lists suggestions on the order in which to study the spells. (This is explained in more detail below.)
Recently, some changelings broke into the Order's spell archive and messed up the spell chart. You are given a String[] spellChart containing the new, messed-up state of the spell chart. Each character of each element of spellChart is either 'Y' or 'N'. The students will come to study soon. They will interpret the chart in the following way: If spellChart[i][j] is 'Y' then spell i must be learned before spell j.
As the chart is now messed up, it may be impossible to learn all the spells in the chart because of cycles in the requirements. Your task is to repair the given chart. Determine the minimum number of changes needed to remove all the cycles in the requirements. In a single change, you may either change some character spellChart[i][j] from 'Y' to 'N', or change some character from 'N' to 'Y'.
Definition
Class: OrderOfTheHats Method: minChanged Parameters: String[] Returns: int Method signature: int minChanged(String[] spellChart) (be sure your method is public) Constraints
- spellChart will contain between 1 and 20 elements, inclusive. - Each element of spellChart will contain N characters, where N is the number of elements in spellChart. - Each character in each element of spellChart will be either 'Y' or 'N'. Examples
0)
{"Y"} Returns: 1
This spell chart contains a spell that should be learned before itself. The students would never be able to learn such a spell. We can remove this cyclic dependency by changing the 'Y' to 'N'. 1)
{"NYN", "NNY", "NNN"} Returns: 0
This spell chart is already OK. 2)
{"NYN", "NNY", "YNN"} Returns: 1
Changing any single 'Y' to a 'N' will fix this spell chart. 3)
{"NYYYYYY", "YNYYYYY", "YYNYYYY", "YYYNYYY", "YYYYNYY", "YYYYYNY", "YYYYYYN"} Returns: 21
4)
{"NNNY", "YNYN", "YNNN", "YYYN"} Returns: 1
5)
{"YYYYYNNYYYNYNNNNYNNY", "NYNNNYYNNYNYYYNYYYYY", "NNYNNNYYNNNNNNYYYYNY", "YYNYNYYNNYYYNYNNNYYY", "NYYNNYNYNYNNNNYYYNYN", "NNNNNYYNYNNYYYYNYYYN", "YNYNYYNNNYNNNNNYNNYY", "NYYYYNYNYNNYNNYNNNNY", "YYYYNYYNNYYYNNYNNYNY", "YYYYYYNYNYNYNNNNNNYN", "NNYYYYYNNNYNNNYNNNNY", "YYNNNYNYYNYYNYYNYNYN", "NNYNYYNYYNYYNYNYNYYN", "YNYNYYNYNNNYNYNYYNYY", "NNYNNNYYYYYYYYYYYNYY", "YYYYYNYYNYYYYYNNYNNN", "NYYYYYYYYNNNNNYYNNYN", "YNNYNNNYYNYYYNYNYYYY", "YYNNYNYYYNYYNNNYYNNY", "NNYNYNYYYNYYNYNNYNNN"} Returns: 79
6)
{"YYNYNN", "YNYNNY", "YYYYNN", "NNNYNN", "NNNYNN", "YNYNYN"} Returns: 5
7)
{"NNNNNNNNNN", "NNNNNNNNNN", "NNNYNNYNNN", "NNNYNNYNNN", "NNNYNNYNNN", "NNNNNNNNNN", "NNYYYYYYNN", "NNYNNNNYNN", "NNNYYYYNNN", "NNNNNNNNNN"} Returns: 6
棰樻剰錛氱粰瀹氫竴寮燦*N鐨刴ap錛孨涓《鐐圭殑鍥撅紝map[i][j]=='Y'錛?lt;i,j>錛屽惁鍒?lt;j,i>銆傛眰鏈灝忕殑杞崲Y鎴栬匩錛屼嬌璇ュ浘娌℃湁鐜紒
鎬濊礬錛氭庝箞鍋氬憿錛焧hinking
]]>
Problem Statement
The Order of All Things Pointy and Magical has commissioned the creation of some new wizard hats. A wizard hat is created by taking two cones: a decorative top cone, and a warm and fluffy bottom cone. To assemble the hat, both cones are first placed onto a table, so that their bases are horizontal and their apexes point upwards. The top cone is then lifted and placed onto the bottom cone. The base of the top cone has to remain horizontal, and the apex of the top cone must be strictly above the apex of the bottom cone.
Not every pair of cones can be used to create a wizard hat. A wizard hat is only produced if the following two criteria are both met:
You have several top cones and several bottom cones of various sizes. Each cone can be described by its height (the distance between the apex and the base) and by the radius of its base. The top cones you have are described by topHeight and topRadius: for each valid i, you have one top cone with height topHeight[i] and radius topRadius[i]. The bottom cones you have are described by bottomHeight and bottomRadius in the same way.
Your task is to determine the maximum number of wizard hats you can make using each of the available top and bottom cones at most once. Definition
Class: PointyWizardHats Method: getNumHats Parameters: vector <int>, vector <int>, vector <int>, vector <int> Returns: int Method signature: int getNumHats(vector <int> topHeight, vector <int> topRadius, vector <int> bottomHeight, vector <int> bottomRadius) (be sure your method is public) Constraints
- topHeight and topRadius will contain the same number of elements. - bottomHeight and bottomRadius will contain the same number of elements. - topHeight will contain between 1 and 50 elements, inclusive. - topRadius will contain between 1 and 50 elements, inclusive. - bottomHeight will contain between 1 and 50 elements, inclusive. - bottomRadius will contain between 1 and 50 elements, inclusive. - Each element of topHeight, topRadius, bottomHeight, and bottomRadius will be between 1 and 10,000, inclusive. Examples
0)
{30} {3} {3} {30} Returns: 1
The top and bottom cone can be used together to make a wizard hat. 1)
{4,4} {4,3} {5,12} {5,4} Returns: 1
The only way to produce a wizard hat is to use the top cone 1 (height 4, radius 3) and the bottom cone 0 (height 5, radius 5). 2)
{3} {3} {1,1} {2,4} Returns: 1
3)
{10,10} {2,5} {2,9} {3,6} Returns: 2
4)
{3,4,5} {5,4,3} {3,4,5} {3,8,5} Returns: 2
5)
{1,2,3,4,5} {2,3,4,5,6} {2,3,4,5,6} {1,2,3,4,5} Returns: 0
6)
{123,214,232,323,342,343} {123,123,232,123,323,434} {545,322,123,545,777,999} {323,443,123,656,767,888} Returns: 5
7)
{999,999,999,10000,10000,10000} {10000,10000,10000,1,2,3} {2324,2323,234,5454,323,232} {1,2,3222,434,5454,23} Returns: 3
棰樻剰錛氫竴涓猦at鐢變笂闈op cone鍜屼笅闈㈢殑bottom cone緇勬垚銆傜粰瀹氫笂闈one鐨勯珮鍜屽簳鍗婂緞錛宼opHeigh[],topRadius[]涓嬮潰cone鐨刡ottomHeight[],bottomRadius[]
涓婁笅涓や釜cone緇勬垚hat闇瑕佹弧瓚蟲潯浠訛細
1錛歍he apex of the top cone must be strictly above the apex of the bottom cone. I.e., when the top cone is placed on top of the bottom cone and released, their apexes must not touch.
2錛歋ome part of the bottom cone must remain visible to form the brim of the hat. (Otherwise, the hat would look like a simple cone, not like a wizard hat!)
鎬濊礬錛氭眰浜屽垎鍥劇殑鏈澶у尮閰嶏紝妯$増棰樸?/span>
topcone 鍜宐ottomcone婊¤凍鐨勬潯浠舵槸錛歵opR<bottomR && topR*bottomH<topH*bottomR
閿欒鎻愪氦浜嗕竴嬈★紝灝肩帥錛侊紒錛佺姽璞笉鍐充笉鏁oding涓嶈鍛錛侊紒
175.22pt
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
bool map[55][55];
int result[55];
bool state[55];
int n,m;
class PointyWizardHats{
public:
int find(int x)
{
int i;
for (i=0;i<m ;i++ )
{
if (map[x][i]==1 && !state[i])
{
state[i]=1;
if (result[i]==-1 || find(result[i]))
{
result[i]=x;
return 1;
}
}
}
return 0;
}
bool can(int x1,int y1,int x2,int y2) //榪欎釜鏉′歡鎴戠姽璞簡鍗婂ぉ錛宼hinking涓嶅鍟婏紒
{
if (y2*x1>y1*x2 && y2>y1)
return 1;
return 0;
}
int getNumHats(vector <int> topHeight, vector <int> topRadius, vector <int> bottomHeight, vector <int> bottomRadius){
int i,j;
int ans;
n=topHeight.size();
m=bottomHeight.size();
memset(map,0,sizeof(map));
for (j=0;j<m;j++)
result[j]=-1; //榪欓噷涔嬪墠鍏ㄩ儴璁劇疆鐨?鍟婂晩鍟婏紒錛侊紒
for (i=0;i<n;i++)
for (j=0;j<m;j++)
if (can(topHeight[i],topRadius[i],bottomHeight[j],bottomRadius[j]))
map[i][j]=1;
ans=0;
for (i=0;i<n;i++)
{
memset(state,0,sizeof(state));
if (find(i))
ans++;
}
return ans;
}
};
]]>
Problem Statement
A magician has invited you to play a game. For this game, the magician uses a special table. On the table there are three spots in a row. The spots are labeled 0, 1, and 2, in order. He places three hats onto the table, so that each hat covers one of the spots. He then takes a ball and places it under one of the hats. The hats are not transparent, so you cannot see the ball while it is under a hat. Next, the magician shuffles the hats by repeatedly swapping two adjacent hats. Each swap is done by sliding the hats along the table, never showing you the ball. Once the magician finishes swapping the hats, you have to guess the spot where the ball is.
You are given a string hats which describes the contents of the hats in the beginning of the game. The i-th character of hats is 'o' if the ball was initially on the spot i. Otherwise, the i-th character of hats is '.' (a period).
You are also given a int numSwaps. Assume that the magician swapped the hat that contained the ball exactly numSwaps times. Please remember that in our version of the game the magician always swaps two adjacent hats. Also, note that the total number of swaps in the game may be larger than numSwaps, because the magician may sometimes swap two hats that don't contain the ball.
Assume that the magician chose the swaps he makes uniformly at random. That is, in each turn with probability 50% he swapped the hats on spots 0 and 1, and with probability 50% he swapped the hats on spots 1 and 2. Return the number of the spot that is most likely to contain the ball at the end of the game. If multiple spots are tied for the largest probability, return the smallest one of them. Definition
Class: BallAndHats Method: getHat Parameters: string, int Returns: int Method signature: int getHat(string hats, int numSwaps) (be sure your method is public) Notes
- Two hats are adjacent if their spots differ by 1. Constraints
- hats will contain exactly three characters. - hats will contain exactly one 'o' character. - hats will contain exactly two '.' characters. - numSwaps will be between 0 and 1000, inclusive. Examples
0)
".o."
1
Returns: 0
The spots 0 and 2 are equally likely to contain the ball after the hat that contains it is swapped once. We return the smallest spot number, which is 0. 1)
"..o"
0
Returns: 2
The ball does not change spots when 0 swaps are performed; therefore, the ball must be at spot 2. 2)
"o.."
1
Returns: 1
3)
"..o"
2
Returns: 0
4)
"o.."
101
Returns: 1
棰樻剰錛氱粰涓変釜甯藉瓙錛屼竴涓附瀛愪笅闈㈡湁姘旂悆銆備竴嬈WAP鍙皢鐩擱偦鐨勪袱涓附瀛愪氦鎹€傛瘡嬈WAP鐨勬鐜囦竴鏍風殑錛?鍜?錛?鍜?浜ゆ崲鐨勬鐜囬兘鏄?0%銆傜粰鍑哄垵濮嬬姸鎬侊紝鎬誨叡鏈塶umSwaps嬈WAP浜嗗甫姘旂悆鐨勫附瀛愩傞棶鏈鍚庢皵鐞冨湪鍝釜浣嶇疆鐨勬鐜囨渶澶э紝濡傛灉鏈夊嚑涓綅緗紝鍒欐眰鏈灝忕殑浣嶇疆銆?br />
鎬濊礬錛氭兂娉曢錛乶umSwaps濂囧伓鎬ц璁哄垎鏋?/span>
176.5pt thinking閫熷害澶綆錛屽鍒嗘瀽鍒嗘瀽錛岄敾鐐兼濈淮鍝︼紒
using namespace std;
class BallAndHats{
public:
int getHat(string hats, int numSwaps){
int i=0;
while (hats[i]!='o') i++;
if (numSwaps==0)
return i;
numSwaps%=2;
if (i==0 && numSwaps==0)
return 0;
if (i==1 && numSwaps==1)
return 0;
if (i==2 && numSwaps==0)
return 0;
return 1;
}
};
]]>
#include<string.h>
#include<vector>
using namespace std;
int h[55],n;
int max(int x,int y)
{
return x>y?x:y;
}
bool check(int x)
{
int last,now,i;
last=max(h[0]-x,1);
for (i=1;i<n;i++){
now=h[i];
if (now+x<=last)
return false;
last=max(last+1,now-x);
}
return true;
}
class KingdomAndTrees{
public:
int minLevel(vector <int> heights){
int l,r,mid,i;
n=heights.size();
for (i=0;i<n;i++)
h[i]=heights[i];
l=0;r=0;
for (i=0;i<n;i++)
if (h[i]>r)
r=h[i];
r=r+n;
while (l<r){
mid=(l+r)/2;
if (check(mid))
r=mid;
else
l=mid+1;
}
return r;
}
};
涓嬫TC瑕侀洩鑰繪墠琛岋紒
]]>
#include<vector>
using namespace std;
int t[51];
class KingdomAndDucks{
public:
int minDucks(vector <int> duckTypes){
int n,i,max,m;
memset(t,0,sizeof(t));
n=duckTypes.size();
for (i=0;i<n;i++)
t[duckTypes[i]]++;
max=0;m=0;
for (i=1;i<=50;i++){
if (t[i]>0)
m++;
max=max>t[i]?max:t[i];
}
return m*max;
}
};
灝卞彧榪囦簡榪欎竴涓鐩紝緙栫爜閫熷害涓嶈鍛錛屽緱蹇揩緇冧範璧鋒潵錛?img src ="http://www.shnenglu.com/ArcTan/aggbug/181223.html" width = "1" height = "1" />
]]>