锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
tchs-2 250pt 鐩存帴綆楀氨琛?鎴戝啓浜?鍒?500pt 鏆村姏鍙互榪囷紝浣嗘槸鍒ゆ柇鏃跺欎笉鑳界敤stringstream 鐢ㄧ畻鏈垽鏂?涔熷彲浠ョ敤鏋勯犳硶 1000pt 姣忎竴浣嶆湁涓夌鍙兘鎬?/p>
涓嶇敤錛屼繚鎸佷笉鍔紝鍙樺寲錛屽垎鍒掑綊璁$畻value騫舵洿鏂扮粨鏋滃嵆鍙紝鐢變簬閫掑綊娣卞害鏈澶氬彧鏈?3灞傛墍浠ヤ笉浼歵le
鍙﹀涔熷彲浠ュ啓鍑哄熀鏁頒負3鐨勫驚鐜潵閬嶅巻姣忎竴縐嶆儏鍐靛叿浣撶湅浠g爜
for(i=0,A[0]++;A[i]>2;i++){
A[i]=0;A[i+1]++;
}
tchs-3 1000pt 瑕佹兂浣夸箻縐渶澶э紝闇瑕佹洿澶氱殑3鍗沖彲 500pt 鍙堢湅閿欓浜?~~~ft 瑕佹敞鎰忛鐩竴瀹氳鐪嬫竻妤?/p>
tchs-4 500pt 妯℃嫙棰橈紝濂介毦鎳?闊充箰鐨剘 鍙互閮戒箻浠?6 鐢ㄦ暣鏁版潵璁$畻 嫻偣浼氬緢鐑 榪欑棰樻濊礬瑕佹竻鏅?涓姝ヤ竴姝ユ潵
You are in a maze containing revolving doors. The doors can be turned 90 degrees by pushing against them in either direction. You are to find a route from the start square to the end square that involves revolving as few doors as possible. Given a map of the maze, determine the fewest number of door revolutions necessary to get from the start to the end.
In the map:
' ': empty space '#': wall 'O': center of a revolving door (letter "oh", not zero) '-': horizontal door (always adjacent to a 'O') '|': vertical door (always adjacent to a 'O') 'S': start square 'E': end square
Each revolving door will always be oriented horizontally (with two horizontal segments) or vertically (with two vertical segments):
| O or -O- |
Doors can be revolved 90 degrees by moving onto a door segment from any of the 4 squares diagonally adjacent to the door center, i.e., the 'X' characters below:
X|X X X O or -O- X|X X X
Here is an example map:
### #E# ## # #### ## # S -O-# # ### # # # ########
In this example, 2 door revolutions are necessary to get from 'S' to 'E'. The first turn is shown here:
### ### #E# #E# ## # ## # #### ## #### |## # S -O-# # S OX# # ### X# # ###| # # # # # ######## ########
And the second turn is shown here:
### ### #E# #E# ## # ## # ####X|## #### X## # S O # # S -O-# # ###| # # ### # # # # # ######## ########
Your method should return an int, the minimum number of door revolutions necessary to get from the start square to the end square. If there is no way to reach the end square, return -1.
Class:
RevolvingDoors
Method:
turns
Parameters:
vector <string>
Returns:
int
Method signature:
int turns(vector <string> map)
(be sure your method is public)
-
Assume that all squares off the edge of the map are walls.
-
map will contain between 3 and 50 elements, inclusive.
-
Each element of map will contain between 3 and 50 characters, inclusive.
-
Each element of map will contain the same number of characters.
-
Each character in map will be one of 'S', 'E', 'O', '-', '|', '#', and ' ' (space).
-
There will be exactly one 'S' and one 'E' in map.
-
There will be between 1 and 10 doors, inclusive, and they will be formatted in map as described in the problem statement.
-
No two doors will be close enough for any part of them to occupy the same square.
-
It is not allowed for a door to be blocked and unable to turn. There will not be any walls in any of the 4 squares immediately adjacent to the center of a door, nor will a door be on the edge of the map.
涓枃copy鏄貢鐮?#183;··鍥у晩~~ 淇虹殑鐮寸儌鑻辨枃娉ㄩ噴鍟妦~~
Just before a chess match between two teams, each team's coach secretly determines an ordering of his team's players. The first players in each team then get to play against each other, the second players in each team play against each other, etc. The team with the most wins will win the match.
You are the coach for one of the teams, and you have somehow managed to find out the order of the players in the other team. Based on that, you want to order your players so that your team's expected score is maximized to your advantage. The expected score of a single game can be calculated by the following formula (which is directly derived from how the international chess rating system works):
EA = 1 / (1 + 10 (RB - RA)/400) EB = 1 / (1 + 10 (RA - RB)/400)
where RA and RB are the ratings of player A and B, and EA and EB are the expected scores for each player. For instance, if RA is 2432 and RB is 2611, the expected score for player A is 1 / (1 + 10179/400) = 0.263005239459. The expected score for a team is the sum of the expected scores for the players in that team.
To make things a bit more complicated, the players in a team must be ordered such that a player with rating x plays before all players with rating strictly less than x - lim, where lim is a given non-negative integer. For example, if lim is 100, a player with rating 2437 must play before a player with rating 2336 but not necessarily before a player with rating 2337.
Create a class ChessMatch containing the method bestExpectedScore which takes a vector <int> teamA, the ratings of your players (in no particular order); a vector <int> teamB, the ratings of your opponent's players in the order they will play; and an int lim, the limit determining how freely you can order your players. You can assume that your opponent's players will be ordered in accordance with lim. The method should return a double, your team's expected score if you order your players optimally.
Class:
ChessMatch
Method:
bestExpectedScore
Parameters:
vector <int>, vector <int>, int
Returns:
double
Method signature:
double bestExpectedScore(vector <int> teamA, vector <int> teamB, int lim)
(be sure your method is public)
瑕佹眰鏈濂界殑寰楀垎錛岃閬嶅巻鎵鏈夌殑鎺掑垪鏂瑰紡鏄劇劧涓嶅彲鑳姐傚鏋滆漿鍖栭棶棰樹負渚濇鏀劇疆姣忎釜閫夋墜鍒版瘡涓涓綅緗紝閭d箞鍙姹傚嚭鍓╀笅閫夋墜鎺掑垪涓嬌寰楀緱鍒嗘渶澶х殑灝卞彲浠ヤ簡錛屽彲浠ユ兂鍒癲p鏂規硶鐨勬渶鏈夊瓙闂瀹氫箟銆?/font>
鐢ㄦ瘡涓浣嶈褰曞綋鍓嶇殑閫夋墜鍒嗛厤鎯呭喌錛屽鏋滀負0錛屽垯宸茬粡鍒嗛厤錛屽鏋滀負1錛屽垯榪樻湭鍒嗛厤銆俻os鏉ヨ褰曞綋鍓嶈鍒嗛厤鐨勪綅緗傚浜庢瘡涓涓瓙闆嗭紝鑰冭檻姣忎竴涓負1鐨勪篃灝辨槸鏈垎閰嶇殑閫夋墜浣夸綅浜巔os浣嶇疆錛岀劧鍚巆heck 鏄惁婊¤凍鍦ㄤ粬涔嬪悗鐨勭悆鍛橈紙涔熷氨鏄墿涓嬬殑鏄?鐨勪綅錛夋槸鍚︽弧瓚蟲潯浠剁殑涓嶇瓑寮忥紝婊¤凍鍒欐洿鏂板綋鍓嶅垎閰嶇殑鏈浼樻垚緇┿傛渶緇?2^N-1 鐨勬垚緇╁嵆涓烘墍姹傘?/font>
涓婇潰鐨勬柟娉曚嬌鐢ㄩ掑綊錛屼笅闈㈡槸浣跨敤閫掓帹鐨勭▼搴忋傚嵆浠庡瓙闆嗗嚭鍙戞渶緇堝埌杈?^n-1鐨勬濊礬銆傚湪榪欓噷鎶夾闃熶紞鍏堜粠澶у埌灝忔帓搴忋傚湪姣忎竴涓瓙闆嗚褰曟瘡涓涓湭鍒嗛厤閫夋墜鐨勫緱鍒嗗拰瑕佸垎閰嶇殑浣嶇疆銆?/font>
紼嬪簭涓濊礬涓猴細渚濇浠庡瓙闆嗕腑閫夊彇涓涓夋墜浣滀負絎琤its-1涓夋墜錛屼篃灝辨槸璇村瓙闆嗕腑鍓╀笅鐨勫厓绱犺鏀懼湪榪欎釜閫夋墜鍓嶉潰錛岀敱浜庨夋墜浠庡皬鍒板ぇ閬嶅巻錛屽彧闇瑕侀亶鍘嗗埌s[j]<=s[bits-1]+lim 涓烘錛屼篃灝辮婊¤凍榪欎釜瀛愰泦涓渶灝忕殑閫夋墜+lim >=閫変腑鐨?鍗沖彲錛岀劧鍚庢洿鏂板綋鍓嶅瓙闆嗙殑寰楀垎
娉ㄩ噴涓濊礬涓? 渚濇浠庡瓙闆嗛夊彇涓涓夋墜浣滀負褰撳墠瀛愰泦涓殑絎竴涓厓绱狅紝涔熷氨鏄暣浣撶殑絎琻-bits涓厓绱狅紝榪欐浠庡ぇ鍒板皬閬嶅巻錛岃繖鏍峰彧闇瑕佷繚璇侊紝浣嶄簬瀛愰泦絎竴涓殑鍏冪礌+lim>=瀛愰泦涓渶澶х殑鍏冪礌(s[0])錛屽嵆鍙紝鐒跺悗鏇存柊褰撳墠瀛愰泦寰楀垎
鏈緇堝叏闆?^n-1鐨勫嵆涓烘墍姹?/font>
Character j in element i (both 0-based) of messages denotes how many messages employee i sent to employee j. You will return a vector <int> containing the hierarchy of the employees within the company. Element 0 is the highest ranked employee, and so forth. The returned ranking should minimize the number of messages sent from lower ranked employees to higher ranked employees. If multiple solutions are possible, return the one with element 0 minimal. If there are still ties, minimize element 1, etc.
Class:
CompanyMessages
Method:
getRank
Parameters:
vector <string>
Returns:
vector <int>
Method signature:
vector <int> getRank(vector <string> messages)
(be sure your method is public)
-
messages will contain between 2 and 15 elements inclusive.
-
Each element of messages will contain exactly N characters, where N is the number of elements in messages.
-
Each character in messages will be a digit ('0'-'9').
-
Character i in element i of messages will be '0'.
鎸夌収棰樼洰璇存槑鍙煡錛屾寜鐓ф壘鍒扮殑欏哄簭錛屾墍鏈変粠浣庣駭鍚戦珮綰у彂鐨勪俊寰楁暟鐩拰搴旇鏄渶灝忕殑銆傚張瑙傚療鍒幫紝鍙紜畾浜嗙涓涓紝涔熷氨鏄駭鍒渶楂樼殑boss錛岄偅涔堝墿涓嬬殑鍛樺伐鎬庝箞鎺掑垪錛屼粬浠悜鏈楂榖oss 鍙戜俊鏁版槸涓嶅彉鐨勶紝浠庤屽瓙闂灝辨槸鍦ㄥ墿涓嬪憳宸ヤ腑鎵懼埌 浣庣駭鍚戦珮綰у彂淇℃暟鏈灝忕殑鎺掑垪 錛屾樉鐒惰繖絎﹀悎DP闂鏈浼樺瓙緇撴瀯鐨勯棶棰樸?/font>
T[1···N]=Min{sum(N <->{1-N-1},T[1-N-1] } 寰楀埌鐘舵佹柟紼嬪悗寰堝鏄撳啓鍑虹▼搴?錛屽彲浠ヤ嬌鐢ㄥ蹇樺綍dp
鎯寵搗媧楃殑琛f湇榪樻病鎷匡紝浜庢槸璺戜笅鍘誨彇琛f湇錛岀湅浜嗕細dota vod 錛岄噸鏂扮櫥鍏ワ紝姣旇禌绔熺劧緇х畫·····浜や簡500 寮濮嬬湅1000 娌℃濊礬錛屽喕姝諱簡銆?/font>
challenge娌″暐鎰忔濓紝閮芥病浜哄湪錛?50鐨勫お寮辨櫤閮藉鐨?#183;···鐢變簬涓棿鍑洪敊錛岃繖嬈rm not rated~~
250 pt 鐣ヨ繃
500 pt
John and Brus have an interest in team sports tournaments. They are currently investigating a basketball tournament. Basketball is a team sport in which two teams of five players try to score points against one another by placing a ball through a ten foot high hoop. Basketball is one of the most popular and widely viewed sports in the world.
There are n teams in the tournament. Each pair of teams plays exactly two games against each other. In the first of these games, one of the teams is the host, and in the second, the other team is the host. Each game results in one team winning. There are no draws. After the tournament is over, the team with the highest total number of wins is crowned the winner.
The tournament is currently in progress and the current results are described in the vector <string> table. For each pair of distinct indices, i and j, the j-th character of the i-th element of tableis the result of the game where team i hosted team j. The result is 'W' if team i won, 'L' if team i lost, and '?' if the game hasn't been played yet. Assuming that every possible outcome is possible for the games that haven't been played yet, return the minimal number of total wins the tournament winner can have at the end of the tournament.
Class:
TheBasketballDivTwo
Method:
find
Parameters:
vector <string>
Returns:
int
Method signature:
int find(vector <string> table)
(be sure your method is public)
-
table will contain between 2 and 5 elements, inclusive.
-
Each element of table will contain exactly n characters, where n is the number of elements in table.
-
The j-th character of the i-th element of table, where i and j are different, will be 'W', 'L', or '?'.
-
The i-th character of the i-th element of table will be 'X'.
鏁版嵁閲忓緢灝忥紝鎵懼埌鏈瘮鐨勬瘮璧涘満嬈?錛岀劧鍚庢灇涓懼悇縐嶈緭璧㈡儏鍐碉紝鏇存柊瑙e氨鍙互浜嗐傛垨鑰呭驚鐜?^n嬈★紝鎴栬呴掑綊璋冪敤
1000 pt
n this tournament, each game results in either a victory for one team or a draw. If a team wins a game, it gains three points and its opponent gains no points. In case of a draw, each team gains one point. The score of a team is the sum of all the points it has gained from all its games. Each pair of teams can play against each other any number of times.
You are given a vector <int> points representing the current standings in the tournament. The i-th element of points is the score of the i-th team. You can assume that the points represent a valid state, i.e., intermediate standings that can be achieved in a tournament according to the rules described above.
Each team will play exactly one more game in the tournament, but it is not known what the matchups will be. After the tournament is over, the teams will be ranked by score. 1st place will go to the team with the highest score, 2nd place will go to the team with the second highest score, and so on. If two teams have the same score, the team with the lower number will place higher. For example, if team 0 and team 3 each have the highest score of 100 points, then team 0 will place 1st and team 3 will place 2nd.
John's favorite team is team 0, and he wants it to place as high as possible. Assuming that the remaining games can be scheduled arbitrarily and can end with any possible outcome, return the highest possible place for team 0 at the end of the tournament.
Class:
TheSoccerDivTwo
Method:
find
Parameters:
vector <int>
Returns:
int
Method signature:
int find(vector <int> points)
(be sure your method is public)
-
points will contain between 2 and 50 elements, inclusive.
-
points will contain an even number of elements.
-
Each element of points will be between 0 and 1,000,000, inclusive.
-
points will represent a valid state.
鍥犱負姣忛槦鍙墿涓嬩竴鍦烘瘮璧涳紝鎵浠ラ鐩彉寰楀緢綆鍗曘?闃熸渶鍚庝竴杞偗瀹氭槸鍙栧埌3鍒嗭紝姣?闃熷3鍦轟互涓婃瘮璧涚殑 鑲畾姣?闃熼潬鍓嶏紝姣?闃熷垎灝戞垨鑰呯浉絳夌殑涓瀹氬湪0闃熶箣鍚庯紝鍓╀笅鐨勫氨鏄垜浠鑰冭檻鐨勪簡銆傚鏋淎3+L+1>=E3 涔熷氨鏄姣?闃熷鑳滀竴鍦虹殑闃熶紞錛屽鏋滆浠栦滑鍦ㄦ渶鍚庝竴杞兘杈擄紝閭d箞0闃熷彲浠ヨ幏寰楁渶濂芥垚緇?A3+1;
鐒惰屽鏋滀笉琛屽墿涓嬬殑榪欎簺E3瑕佹湁涓鍗?E3+1)/2涓寰楀埌3鍒嗕粠鑰屾瘮0闃熼珮錛宱ver~
鍒氭墠鍋剁劧鐪嬭poj騫舵煡闆嗙殑棰橀泦 灝眂opy榪囨潵鑷繁涔熷啓鍐欑湅~~·
鎱㈡參鏉~ 鍐欏畬涓閬撲笂涓閬搤~
POJ 1182 椋熺墿閾?/strong>
http://acm.pku.edu.cn/JudgeOnline/problem?id=1182
榪樹互涓烘槸鏈綆鍗曠殑涓閬撳憿錛岃鍒楀湪鏈鍓嶉潰錛屽師鏉ユ槸鎵╁睍鐨勫簲鐢紝涓嶆槸閭d箞鐩存帴鍟妦
綰犵粨浜嗕竴浼氬効錛岀湅浜嗚В棰樻姤鍛婃墠鏈夋濊礬銆傚叧閿槸鍦ㄥ茍鏌ラ泦鐨勫熀紜涓婂鍔犱竴涓暟緇勶紝緇存姢鍏朵笌鏍硅妭鐐圭殑鍏崇郴錛?/font>
璇︽儏鍐欏埌娉ㄩ噴閲岄潰鍚э細
int A[100003][3]; int root[50005],k[50005];//root璁板綍鑺傜偣鎵灞炰簬鐨勬爲鏈╥d k璁板綍鑺傜偣涓庢牴鐨勫叧緋? 鍚岀被1瀛愭槸椋熺墿2瀛愭槸澶╂晫 int sz[50005]; int find(int x){ if(root[x]==x)return x;//濡傛灉鏄牴鍒欒繑鍥? else if(root[root[x]]==root[x])return root[x];//濡傛灉絎竴灞傦紝鍒欒繑鍥? int father=root[x]; root[x]=find(father);//鏀懼埌絎竴灞傦紝鍑忓皯涓嬫find鏃墮棿錛屽噺灝忔爲鐨勯珮搴? k[x]=(k[x]+k[father])%3;//鐢辮嚜宸變笌鏍圭殑鍏崇郴浠ュ強鐖惰妭鐐逛笌鏍圭殑鍏崇郴鎺ㄦ柇鍑烘柊鐨勮嚜宸變笌鏍圭殑鍏崇郴錛屽疄闄呬笂鏄負浜嗚В鍐硊nion鍚庣殑鑺傜偣涓庢牴鐨勫叧緋?/span>
//鍥犱負union鍚庯紝闈炴牴鑺傜偣騫舵病鏈夋洿鏂発錛屽洜姝ら渶瑕佸湪姝ゅ澶勭悊鏇存柊 return root[x]; } inline void Union(int x,int y,int kind){ int a=find(x);int b=find(y); if(sz[a]>sz[b]){//鍒ゆ柇鍚庡喅瀹氾紝瑕佹妸size灝忕殑鏀懼埌size澶х殑涓婂幓錛屼紭鍖栦綔鐢ㄤ笉澶? root[b]=a ; //b鎴愪負a鐨勫瓙鏍? k[b]=(k[x]-k[y]+kind+3)%3;//瑕佹妸b鍙樻垚a鐨勫瓙鏍戯紝浠巃鍑哄彂錛屽埌x錛岀粡榪噆ind錛屽埌y錛屽啀鍒癰 錛屽彲浠ュ緱鍒発b鐨勭姸鎬佹敼鍙樺叕寮? sz[a]+=sz[b];} else{ if(kind==1)kind=2; root[a]=b; k[a]=(k[y]-k[x]+kind+3)%3; sz[b]+=sz[a]; } } int main() { int N,d; freopen("d:\\input.txt","r",stdin); scanf("%d %d",&N,&d); for(int i=1;i<=N;i++)sz[i]=1; int cnt=0; for(int s=1;s<=N;s++)root[s]=s; int LIE=0; int i=0; while(i !=d) { scanf("%d %d %d",&A[i][0],&A[i][1],&A[i][2]); if(A[i][1]>N || A[i][2]>N ) {LIE++;i++;continue;} if(A[i][0]==1){ if(find(A[i][1]) == find(A[i][2])){ if(k[A[i][1]]!=k[A[i][2]]) LIE++; } else Union(A[i][1],A[i][2],0); } else{ if( find(A[i][1])==find(A[i][2]) ){if(k[A[i][2]] != ( k[A[i][1]]+1)%3)LIE++;} else Union(A[i][1],A[i][2],1); } i++; } cout<<LIE<<endl; return 0; }POJ 1611 The Suspects
http://acm.pku.edu.cn/JudgeOnline/problem?id=1611
int G[30005]; int sz[30005]; int Find(int x){ if(G[x]==x)return x; else { while(G[x]!=x){//half璺緞鍘嬬緝~絳変笅璇曡瘯鐪嬪叏鍘嬬緝鐨勬晥鐜噡閭f牱灝辨槸閫掑綊璋冪敤鍟? int t=G[x]; G[x]=G[t]; x=t; } return x; } } void Union(int x,int y){ if(sz[x]<sz[y])swap(x,y);//鎶妔ize灝忕殑寮勫埌澶х殑涓婇潰 G[y]=x; sz[x]+=sz[y]; } int main() { freopen("d:\\input.txt","r",stdin); int N,M,num; while(true){ scanf("%d %d",&N,&M); if(N==0&&M==0)return 0; for(int i=0;i<N;i++)G[i]=i,sz[i]=1; for(int i=0;i<M;i++){ scanf("%d",&num); int root,stu,x,y; for(int j=0;j<num;j++){ scanf("%d",&stu); if(j==0)root=Find(stu);//綆鍗曠殑閮藉拰絎竴涓悎騫? else{ root=Find(root); x=Find(stu);if(root!=x)Union(root,x); } } } printf("%d\n",sz[Find(0)]); } }
鍙堜竴閬撴按棰榽~ 鍛靛懙
涓嶈創浠g爜浜?鍜屼笂闈竴閬撳熀鏈竴鏍穨
http://acm.pku.edu.cn/JudgeOnline/problem?id=2524
POJ 1861
http://acm.pku.edu.cn/JudgeOnline/problem?id=1861
kruskal+騫舵煡闆?half璺緞鍘嬬緝
姣旇緝鍩虹鐨勯
struct Edge{ int from; int to; int value; }E[15000]; int A[1005]; int sz[2009]; bool comp(const Edge& a,const Edge& b){ return a.value<b.value; } using namespace std; int Find(int x){ if(A[x]==x)return x; else if(A[A[x]]==A[x]) return A[x]; int father; while(A[x]!=x){ father=A[x]; A[x]=A[father];//鎶婃瘡涓涓礬榪囩殑鑺傜偣鏀懼埌紲栫埗涓嬮潰鍘? x=father; } return x; } void Union(int x,int y){ if(sz[y]>sz[x])swap(x,y);//灝忕殑鏀懼埌澶х殑涓嬮潰 sz[x]+=sz[y]; A[y]=x; } int main() { freopen("d:\\input.txt","r",stdin); int N,M,num,x,y; scanf("%d %d",&N,&M); int cnt=0; while(cnt<M) scanf("%d %d %d",&E[cnt].from,&E[cnt].to,&E[cnt].value),cnt++; sort(E,E+M,comp);//浠庡皬鍒板ぇ閫塶-1鏉¤竟錛屽鏋滆搗鐐瑰拰緇堢偣鍦ㄤ竴涓泦鍚堝垯continue錛沞lse Union for(int i=0;i<=N;i++)sz[i]=1,A[i]=i; vector<int> ans(N-1); int mst=0,MX=0; for(int i=0;mst!=N-1;i++){ int x=Find(E[i].from);int y=Find(E[i].to); if(x==y)continue; Union(x,y); ans[mst]=i; mst++; if(E[i].value>MX)MX=E[i].value; } printf("%d\n%d\n",MX,N-1); for(int i=0;i<N-1;i++) printf("%d %d\n",E[ans[i]].from,E[ans[i]].to); }
POJ 1456 Supermarket
http://acm.pku.edu.cn/JudgeOnline/problem?id=1456
POJ 1733 Parity game
http://acm.pku.edu.cn/JudgeOnline/problem?id=1733
hdu 3038 How Many Answers Are Wrong
http://acm.hdu.edu.cn/showproblem.php?pid=3038
POJ 1417 True Liars(闅?
http://acm.pku.edu.cn/JudgeOnline/problem?id=1417
POJ 2912 Rochambeau(闅?
http://acm.pku.edu.cn/JudgeOnline/problem?id=2912
numTables will be between 1 and 12 inclusive.
probs will contain between 1 and 12 elements inclusive.
Each element of probs will be between 0 and 100 inclusive.
The elements of probs will sum to 100.
misof 鏁板瓧琛ㄨ揪鏁欑▼閲岀殑涔犻~ 棰樼洰澶ф剰 姹備嬌鐢ㄦ瀛愮殑鏈熸湜銆傜敱浜庡埌鏉roup鐨勪釜鏁頒笉瀹氾紝姣忎釜group闇瑕佺殑妗屽瓙涓嶅畾錛屼嬌紜畾鏈熸湜鍙樺緱鍥伴毦銆備絾鑰冭檻瀵逛簬numTables鏉ヨ,浣跨敤妗屽瓙鐨勭姸鎬佷粎浠呮湁 2^numTables縐嶏紝鍥犳鑰冭檻鍦ㄨ繖浜涚姸鎬佹敼鍙樼殑榪囩▼涓潵璁$畻鏈熸湜錛屼篃灝辨槸璁$畻鍦ㄦ瘡涓姸鎬佷笅闈㈢殑鏈熸湜妗屽瓙鏁扮洰銆傚湪姣忎釜鐘舵佸埌杈炬椂錛屼緷嬈¤冭檻鏉ヤ簡涓涓猤roup闇瑕乲涓綅瀛愶紝濡傛灉r縐嶅畨鎺掑彲浠ユ弧瓚砶涓綅瀛愶紝閭d箞褰撳墠鐘舵佺殑鏈熸湜鍊艱鍔犱笂 鏉涓綅瀛愮殑姒傜巼 X 錛坮縐嶅畨鎺掑垎鍒殑鏈熸湜鍜?/ r錛?鍏朵腑姹俽涓畨鎺掓湡鏈涘拰鍒欓渶瑕?閫掑綊璋冪敤鍑芥暟銆傛樉鐒跺埄鐢╩emo鍙互鍑忓皯閲嶅璁$畻浜庢槸鏈変笅闈㈢殑瑙f硶錛?/font>
vector<double> p;
double dp[1<<13];
int tb;
double solve(int cur){
if(dp[cur]>-1.0)return dp[cur]; //memo available
double ret=0;double sum;int kind;
for(int i=0;i<p.size();i++){
sum=0,kind=0;
int mask=(1<<(i+1))-1; //new group need i+1 adjacent tables
for(int j=0;j+i+1<=tb;j++){
if((cur&(mask<<j))==0){ //current pattern could meet the need
sum+=solve(cur+(mask<<j))+i+1; //total method ++
kind++;
}
}
if(kind!=0)sum/=kind; //caculate the average need
ret+=sum*p[i];
}
dp[cur]=ret;
return ret;
}
double getExpected(int numTables, vector <int> probs)
{
tb=numTables;
REP(i,1<<13)dp[i]=-1.0;
p.resize(probs.size());
for(int i=0;i<probs.size();i++)p[i]=probs[i]*0.01;
return solve(0);//the beginning pattern
}
鐪嬫瘮璧涗腑鏈夊彟涓縐嶈В娉曪紝鍗蟲牴鎹鐩紝鍦ㄥ埌杈炬瘡嬈ail to serve a group 鐨勬椂鍊?鏍規嵁姝ゆ椂鐨勬瀛愭暟閲忥紝鍜屽埌杈捐繖縐嶇姸鎬佺殑姒傜巼 鏉ヨ綆楋細
dp[1<<13][15];memset(dp,0,sizeof(dp));// :D lucily I can do this for 0
double fails=0.0;bool flag ;
for(int i=1;i<=numTables+1;i++) //寰幆鏈澶歯umTables+1 嬈?/font>
{flag=true;
for(int j=0;j<p.size();j++){
int mask=(1<<(j+1))-1;//娉ㄦ剰縐諱綅榪愮畻絎︾殑浼樺厛綰т綆,娉ㄦ剰鍔犳嫭鍙?/font>
for(int k=0;k<=(1<<numTables-1);k++){
if(dp[k][i-1]<=0.0)continue;
flag=false;
int cnt=0;
for(int m=0;m+j+1<=numTables;m++) if((mask<<m)&k==0)cnt++;
if(cnt)for(int m=0;m+j+1<=numTables;m++)if((mask<<m)&k==0)dp[mask<<m|k][i]+=dp[k][i-1]*p[j]/cnt;
if(!cnt){
int b=k,bn=0;while(b){if(b&1)bn++;b>>=1;}
fail+=dp[k][i-1]*bn;
}
}
}
if(flag)return fail;//all dp[][k]==0.0
}
return fail;
浼樺厛綰у緢瀹規槗閿欙細
http://www.cppreference.com/wiki/operator_precedence~銆倊
鍏稿瀷鐨勫嚑涓?/font>
++ -- <post-incre-decre>
~ <bitwise complement> !<not>&<addresss> *<dereference>&<address>
* / %
+ -
>> <<
< <= > >=
== !=
&
^ xor
|
&&
||
?=
= += –= <<= >>=
,
浠庝笂鍒頒笅渚濇闄嶄綆~~~~~~~~~~~~~~~~~~··