锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
tchs-2 250pt 鐩存帴綆楀氨琛?鎴戝啓浜?鍒?500pt 鏆村姏鍙互榪囷紝浣嗘槸鍒ゆ柇鏃跺欎笉鑳界敤stringstream 鐢ㄧ畻鏈垽鏂?涔熷彲浠ョ敤鏋勯犳硶 1000pt 姣忎竴浣嶆湁涓夌鍙兘鎬?/p>
涓嶇敤錛屼繚鎸佷笉鍔紝鍙樺寲錛屽垎鍒掑綊璁$畻value騫舵洿鏂扮粨鏋滃嵆鍙紝鐢變簬閫掑綊娣卞害鏈澶氬彧鏈?3灞傛墍浠ヤ笉浼?xì)tle
鍙﹀涔熷彲浠ュ啓鍑哄熀鏁頒負(fù)3鐨勫驚鐜潵閬嶅巻姣忎竴縐嶆儏鍐靛叿浣撶湅浠g爜
for(i=0,A[0]++;A[i]>2;i++){
A[i]=0;A[i+1]++;
}
tchs-3 1000pt 瑕佹兂浣夸箻縐渶澶э紝闇瑕佹洿澶氱殑3鍗沖彲 500pt 鍙堢湅閿欓?shù)?~~~ft 瑕佹敞鎰忛鐩竴瀹氳鐪嬫竻妤?/p>
tchs-4 500pt 妯℃嫙棰橈紝濂介毦鎳?闊充箰鐨剘 鍙互閮戒箻?shù)?6 鐢ㄦ暣鏁版潵璁$畻 嫻偣浼?xì)寰堢儲(chǔ)~ 榪欑棰樻濊礬瑕佹竻鏅?涓姝ヤ竴姝ユ潵
姣旇禌閭eぉ鎰熷啋錛岀涓棰樺氨寮勬檿浜?jiǎn)锛岀幇鍦ㄩ瑙e嚭鏉ヤ簡(jiǎn)锛岃ˉ涓婂惂~~
鏆傛椂鍙湁絎竴棰樼殑錛?/p>
Problem 6: Bobsledding [Brian Jacokes, 2009] Bessie has entered a bobsled competition because she hopes her hefty weight will give her an advantage over the L meter course (2 <= L <= 1,000,000,000). Bessie will push off the starting line at 1 meter per second, but her speed can change while she rides along the course. Near the middle of every meter Bessie travels, she can change her speed either by using gravity to accelerate by one meter per second or by braking to stay at the same speed or decrease her speed by one meter per second. Naturally, Bessie must negotiate N (1 <= N <= 100,000) turns on the way down the hill. Turn i is located T_i meters from the course start (1 <= T_i <= L-1), and she must be enter the corner meter at a speed of at most S_i meters per second (1 <= S_i <= 1,000,000,000). Bessie can cross the finish line at any speed she likes. Help Bessie learn the fastest speed she can attain without exceeding the speed limits on the turns. Consider this course with the meter markers as integers and the turn speed limits in brackets (e.g., '[3]'): | 1 2 3 4 5 6 7[3] |---+---+---+---+---+---+---+ | \ Start + 8 \ + 9 \ + 10 +++ 14 (finish) \ / 11[1] +---+---+ 12 13[8] Below is a chart of Bessie's speeds at the beginning of each meter length of the course: Max: 3 1 8 Mtrs: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Spd: 1 2 3 4 5 5 4 3 4 3 2 1 2 3 4 Her maximum speed was 5 near the beginning of meter 4. PROBLEM NAME: bobsled INPUT FORMAT: * Line 1: Two space-separated integers: L and N * Lines 2..N+1: Line i+1 describes turn i with two space-separated integers: T_i and S_i SAMPLE INPUT (file bobsled.in): 14 3 7 3 11 1 13 8 OUTPUT FORMAT: * Line 1: A single integer, representing the maximum speed which Bessie can attain between the start and the finish line, inclusive. SAMPLE OUTPUT (file bobsled.out): 5
棰樼洰鐪嬭搗鏉ユ尯澶嶆潅錛屽叾瀹炰富瑕佹槸姹傚嚭鍚勪釜turn澶勭殑鏈澶ч熷害錛屽垎鏋愬緱鍒版瘡涓猼urn鐨勬渶澶ч熷害闇瑕佹弧瓚充笁涓潯浠訛紝 M_i = min (S_i , t_i – t_{i-1} + M_{i-1} , S_k + t_k – t_i [for all k > i ] )
鍥犳澶勭悊姣忎竴涓猼urn閮借鏌ヨN涓猼urn N*N鐨勫鏉傚害鏄劇劧瀵逛簬澶ф暟鎹TLE鐨?/font>
閫嗗悜鎬濊冿紝濡傛灉鎴戜滑鍙嶈繃鏉ヨ冭檻錛屽浜庢瘡涓涓箣鍚庣殑turn鏉ヨ 濡傦細(xì)i 濡傛灉浠栨渶澶ч熷害涓?m_i
閭d箞 鍦╰urn i-1澶勶紝浠栦笉鑳借秴榪囩殑鏈澶ч熷害 m_{i-1} = min(S_i,m_i+t_i – t_{i-1});榪欐牱鎴愬姛鐨勬妸鍚庨潰涓や釜闄愬埗杞崲涓洪嗘帹鐨勭粨鏋滆屼笉鏄悜鍚庢煡璇?/font>
鍓╀笅鐨勯棶棰樹(shù)究鏄鏋滅煡閬撲袱涓猼urn涔嬮棿璺濈錛屼互鍙?qiáng)turn鐨勯熷害鏈澶у鹼紝濡備綍姹傚嚭涔嬮棿鐨勬渶澶у鹼紝鐢誨浘鏄劇劧鍙互寰楀埌涓縐嶇畻寮?maxspeed = min(s1,s2) + (dist2-dist1+abs(s1-s2))/2;
鎴栬?maxspeed = max(s1,s2) + (dist2 – dist1 – abs(s1-s2))/2;
娉ㄦ剰鍦ㄥ紑澶村拰緇撳熬鍔犲叆铏氭嫙鐨則urn灝卞彲浠ヤ簡(jiǎn)
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)
瑕佹眰鏈濂界殑寰楀垎錛岃閬嶅巻鎵鏈夌殑鎺掑垪鏂瑰紡鏄劇劧涓嶅彲鑳姐傚鏋滆漿鍖栭棶棰樹(shù)負(fù)渚濇鏀劇疆姣忎釜閫夋墜鍒版瘡涓涓綅緗紝閭d箞鍙姹傚嚭鍓╀笅閫夋墜鎺掑垪涓嬌寰楀緱鍒嗘渶澶х殑灝卞彲浠ヤ簡(jiǎn)錛屽彲浠ユ兂鍒癲p鏂規(guī)硶鐨勬渶鏈夊瓙闂瀹氫箟銆?/font>
鐢ㄦ瘡涓浣嶈褰曞綋鍓嶇殑閫夋墜鍒嗛厤鎯呭喌錛屽鏋滀負(fù)0錛屽垯宸茬粡鍒嗛厤錛屽鏋滀負(fù)1錛屽垯榪樻湭鍒嗛厤銆俻os鏉ヨ褰曞綋鍓嶈鍒嗛厤鐨勪綅緗傚浜庢瘡涓涓瓙闆嗭紝鑰冭檻姣忎竴涓負(fù)1鐨勪篃灝辨槸鏈垎閰嶇殑閫夋墜浣夸綅浜巔os浣嶇疆錛岀劧鍚巆heck 鏄惁婊¤凍鍦ㄤ粬涔嬪悗鐨勭悆鍛橈紙涔熷氨鏄墿涓嬬殑鏄?鐨勪綅錛夋槸鍚︽弧瓚蟲(chóng)潯浠剁殑涓嶇瓑寮忥紝婊¤凍鍒欐洿鏂板綋鍓嶅垎閰嶇殑鏈浼樻垚緇┿傛渶緇?2^N-1 鐨勬垚緇╁嵆涓烘墍姹傘?/font>
涓婇潰鐨勬柟娉曚嬌鐢ㄩ掑綊錛屼笅闈㈡槸浣跨敤閫掓帹鐨勭▼搴忋傚嵆浠庡瓙闆嗗嚭鍙戞渶緇堝埌杈?^n-1鐨勬濊礬銆傚湪榪欓噷鎶夾闃熶紞鍏堜粠澶у埌灝忔帓搴忋傚湪姣忎竴涓瓙闆嗚褰曟瘡涓涓湭鍒嗛厤閫夋墜鐨勫緱鍒嗗拰瑕佸垎閰嶇殑浣嶇疆銆?/font>
紼嬪簭涓濊礬涓猴細(xì)渚濇浠庡瓙闆嗕腑閫夊彇涓涓夋墜浣滀負(fù)絎琤its-1涓夋墜錛屼篃灝辨槸璇村瓙闆嗕腑鍓╀笅鐨勫厓绱犺鏀懼湪榪欎釜閫夋墜鍓嶉潰錛岀敱浜庨夋墜浠庡皬鍒板ぇ閬嶅巻錛屽彧闇瑕侀亶鍘嗗埌s[j]<=s[bits-1]+lim 涓烘錛屼篃灝辮婊¤凍榪欎釜瀛愰泦涓渶灝忕殑閫夋墜+lim >=閫変腑鐨?鍗沖彲錛岀劧鍚庢洿鏂板綋鍓嶅瓙闆嗙殑寰楀垎
娉ㄩ噴涓濊礬涓? 渚濇浠庡瓙闆嗛夊彇涓涓夋墜浣滀負(fù)褰撳墠瀛愰泦涓殑絎竴涓厓绱狅紝涔熷氨鏄暣浣撶殑絎琻-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'.
鎸夌収棰樼洰璇存槑鍙煡錛屾寜鐓ф壘鍒扮殑欏哄簭錛屾墍鏈変粠浣庣駭鍚戦珮綰у彂鐨勪俊寰楁暟鐩拰搴旇鏄渶灝忕殑銆傚張瑙傚療鍒幫紝鍙紜畾浜?jiǎn)绗竴涓紝涔熷氨鏄駭鍒渶楂樼殑boss錛岄偅涔堝墿涓嬬殑鍛樺伐鎬庝箞鎺掑垪錛屼粬浠悜鏈楂榖oss 鍙戜俊鏁版槸涓嶅彉鐨勶紝浠庤屽瓙闂灝辨槸鍦ㄥ墿涓嬪憳宸ヤ腑鎵懼埌 浣庣駭鍚戦珮綰у彂淇℃暟鏈灝忕殑鎺掑垪 錛屾樉鐒惰繖絎﹀悎DP闂鏈浼樺瓙緇撴瀯鐨勯棶棰樸?/font>
T[1···N]=Min{sum(N <->{1-N-1},T[1-N-1] } 寰楀埌鐘舵佹柟紼嬪悗寰堝鏄撳啓鍑虹▼搴?錛屽彲浠ヤ嬌鐢ㄥ蹇樺綍dp
鎯寵搗媧楃殑琛f湇榪樻病鎷匡紝浜庢槸璺戜笅鍘誨彇琛f湇錛岀湅浜?jiǎn)浼?xì)dota vod 錛岄噸鏂扮櫥鍏ワ紝姣旇禌绔熺劧緇х畫(huà)·····浜や簡(jiǎn)500 寮濮嬬湅1000 娌℃濊礬錛屽喕姝諱簡(jiǎn)銆?/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氨鍙互浜?jiǎn)銆傛垨鑰呭驚鐜?^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.
鍥犱負(fù)姣忛槦鍙墿涓嬩竴鍦烘瘮璧涳紝鎵浠ラ鐩彉寰楀緢綆鍗曘?闃熸渶鍚庝竴杞偗瀹氭槸鍙栧埌3鍒嗭紝姣?闃熷3鍦轟互涓婃瘮璧涚殑 鑲畾姣?闃熼潬鍓嶏紝姣?闃熷垎灝戞垨鑰呯浉絳夌殑涓瀹氬湪0闃熶箣鍚庯紝鍓╀笅鐨勫氨鏄垜浠鑰冭檻鐨勪簡(jiǎn)銆傚鏋淎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
榪樹(shù)互涓烘槸鏈綆鍗曠殑涓閬撳憿錛岃鍒楀湪鏈鍓嶉潰錛屽師鏉ユ槸鎵╁睍鐨勫簲鐢紝涓嶆槸閭d箞鐩存帴鍟妦
綰犵粨浜?jiǎn)涓浼?xì)鍎垮Q岀湅浜?jiǎn)瑙i鎶ュ憡鎵嶆湁鎬濊礬銆傚叧閿槸鍦ㄥ茍鏌ラ泦鐨勫熀紜涓婂鍔犱竴涓暟緇勶紝緇存姢鍏朵笌鏍硅妭鐐圭殑鍏崇郴錛?/font>
璇︽儏鍐欏埌娉ㄩ噴閲岄潰鍚э細(xì)
int A[100003][3]; int root[50005],k[50005];//root璁板綍鑺傜偣鎵灞炰簬鐨勬爲(wèi)鏈╥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鏃墮棿錛屽噺灝忔爲(wèi)鐨勯珮搴? k[x]=(k[x]+k[father])%3;//鐢辮嚜宸變笌鏍圭殑鍏崇郴浠ュ強(qiáng)鐖惰妭鐐逛笌鏍圭殑鍏崇郴鎺ㄦ柇鍑烘柊鐨勮嚜宸變笌鏍圭殑鍏崇郴錛屽疄闄呬笂鏄負(fù)浜?jiǎn)瑙e喅union鍚庣殑鑺傜偣涓庢牴鐨勫叧緋?/span>
//鍥犱負(fù)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鎴愪負(fù)a鐨勫瓙鏍? k[b]=(k[x]-k[y]+kind+3)%3;//瑕佹妸b鍙樻垚a鐨勫瓙鏍?wèi)锛屼粠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)]); } }
鍙堜竴閬撴按棰榽~ 鍛靛懙
涓嶈創(chuàng)浠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 鏁板瓧琛ㄨ揪鏁欑▼閲岀殑涔?fàn)棰榽 棰樼洰澶ф剰 姹備嬌鐢ㄦ瀛愮殑鏈熸湜銆傜敱浜庡埌鏉roup鐨勪釜鏁頒笉瀹氾紝姣忎釜group闇瑕佺殑妗屽瓙涓嶅畾錛屼嬌紜畾鏈熸湜鍙樺緱鍥伴毦銆備絾鑰冭檻瀵逛簬numTables鏉ヨ,浣跨敤妗屽瓙鐨勭姸鎬佷粎浠呮湁 2^numTables縐嶏紝鍥犳鑰冭檻鍦ㄨ繖浜涚姸鎬佹敼鍙樼殑榪囩▼涓潵璁$畻鏈熸湜錛屼篃灝辨槸璁$畻鍦ㄦ瘡涓姸鎬佷笅闈㈢殑鏈熸湜妗屽瓙鏁扮洰銆傚湪姣忎釜鐘舵佸埌杈炬椂錛屼緷嬈¤冭檻鏉ヤ簡(jiǎn)涓涓猤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
}
鐪嬫瘮璧涗腑鏈夊彟涓縐嶈В娉曪紝鍗蟲(chóng)牴鎹鐩紝鍦ㄥ埌杈炬瘡嬈ail to serve a group 鐨勬椂鍊?鏍規(guī)嵁姝ゆ椂鐨勬瀛愭暟閲忥紝鍜屽埌杈捐繖縐嶇姸鎬佺殑姒傜巼 鏉ヨ綆楋細(xì)
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;
浼樺厛綰у緢瀹規(guī)槗閿欙細(xì)
http://www.cppreference.com/wiki/operator_precedence~銆倊
鍏稿瀷鐨勫嚑涓?/font>
++ -- <post-incre-decre>
~ <bitwise complement> !<not>&<addresss> *<dereference>&<address>
* / %
+ -
>> <<
< <= > >=
== !=
&
^ xor
|
&&
||
?=
= += –= <<= >>=
,
浠庝笂鍒頒笅渚濇闄嶄綆~~~~~~~~~~~~~~~~~~··
浠婂ぉ棰樿В鍑烘潵浜?jiǎn)~ 鍏堢湅浜?jiǎn)澶ф鎬濊礬 鐒跺悗鑷繁鍐欏嚭鏉ヤ簡(jiǎn)~
棰樼洰錛?/font>
Farmer John's cows like to play coin games so FJ has invented with a new two-player coin game called Xoinc for them. Initially a stack of N (5 <= N <= 2,000) coins sits on the ground; coin i from the top has integer value C_i (1 <= C_i <= 100,000). The first player starts the game by taking the top one or two coins (C_1 and maybe C_2) from the stack. If the first player takes just the top coin, the second player may take the following one or two coins in the next turn. If the first player takes two coins then the second player may take the top one, two, three or four coins from the stack. In each turn, the current player must take at least one coin and at most two times the amount of coins last taken by the opposing player. The game is over when there are no more coins to take. Afterwards, they can use the value of the coins they have taken from the stack to buy treats from FJ, so naturally, their purpose in the game is to maximize the total value of the coins they take. Assuming the second player plays optimally to maximize his own winnings, what is the highest total value that the first player can have when the game is over? MEMORY LIMIT: 20 MB PROBLEM NAME: xoinc INPUT FORMAT: * Line 1: A single integer: N * Lines 2..N+1: Line i+1 contains a single integer: C_i SAMPLE INPUT (file xoinc.in): 5 1 3 1 7 2綆鍗曟潵璇村氨鏄袱涓漢杞祦鍙朿oins錛屾瘡涓漢姣忔鍙栧緱涓暟涓?- 2*n;n涓轟笂涓杞鏂瑰彇寰楁暟鐩紝姹備袱涓漢閮芥槸鐢ㄦ渶浣崇瓥鐣ワ紝鍏堝彇寰楅偅涓浼欐渶澶氳兘鎷垮埌澶氬皯紜竵銆傝矊浼煎彲浠ョ畻鏄畝鍗曞崥寮堣鐨勬濇兂鎬濊礬錛?/font>coins[1···N] 浠庝笅鍒頒笂 sum[1···N] 鍓╀笅 i涓殑鍜?/font>鎵懼埌鏃犲悗鏁堟х殑瀛愰棶棰樸傝冭檻鍦ㄨ繕鍓╀笅p涓挶甯佹椂鍊欑殑鎯呭喌錛屾鏃跺彲浠ユ嬁k涓挶鐢變簬鏉′歡錛宬鐨勫ぇ灝忓彈涓婁竴杞嬁鐨勪釜鏁癷鐨勯檺鍒?錛屾墍浠ユ垜浠鍔犱笂涓涓彉閲廼銆傚緱鍒?/font>dp[p][i]榪欎釜瀛愰棶棰樸傞偅涔堝鏄撳緱鍒?/font>dp[p][i]=max(1=<k<=i*2){SuM(p to p-k+1)+SuM(p-k to 1)-dp[p-k][k]}=max(1=<k<=i*2){sum[p]-dp[p-k][k]}鎸夌収榪欎釜鍙互寰楀埌涓涓狾錛圢^3錛夌殑綆楁硶oidsolve(){
for(inti=1;i<=N;i++)//鍓╀笅i涓?br> for(intj=1;j<=N;j++)//涓婁竴浜烘嬁浜?jiǎn)j 涓?br> for(intk=1;k<=j*2&&i-k>=0;k++){
dp[i][j]=max(dp[i][j],sum[1]-sum[i+1]-dp[i-k][k]);
}
ret=dp[N][1];
}涓夐噸閫掑綊 錛屾渶澶氬彲浠ヨ繃500鐨勬暟鎹噺 瑙傚療鍙互寰楀嚭 dp[p][j] 鍜?dp[p][j+1] 鐨勮綆楁湁寰堝鐨勯噸鍙?br>鍥犱負(fù) 涓婃鎷夸簡(jiǎn)j+1 鍒欏彲浠ユ瘮 dp[p][j] 澶氭嬁 2 涓?nbsp;
鐒跺悗錛岀敱浜庤冭檻j鐨勮寖鍥?搴旇涓?N-i+1
榪欐牱寰楀埌浜?jiǎn)鏈緇堜唬鐮侊細(xì)
scanf("%d",&N); for(int i=1;i<=N;i++) scanf("%d",coins+i);//{fin>>coins[i]; } sum[0]=0; for(int i=1;i<=N;i++) sum[i]=sum[i-1]+coins[N-i+1]; for(int i=1;i<=N;i++) //鍓?涓?i 涓? for(int j=1;j<= N-i +1;j++){ // 涓?嬈?鎷?浜?j 涓? if(dp[i][j]<dp[i][j-1])dp[i][j]=dp[i][j-1]; if(2*j-1<=i&&dp[i][j]<sum[i]-dp[i-2*j+1][2*j-1]) dp[i][j]=sum[i]-dp[i-2*j+1][2*j-1]; if(2*j<=i&&dp[i][j]<sum[i]-dp[i-2*j][2*j]) dp[i][j]= sum[i]-dp[i-2*j][2*j]; } printf("%d\n",dp[N][1]);
寰堟櫄浜?錛屽厛鍐欒繖涔堝 錛屾湁絀烘妸bronze鐨勫啓浜?/strong>
涓昏鍐呭 FIND-UNION ALGORITHM
灝辨槸鍒╃敤騫舵煡闆嗘潵鑰冨療榪為氭х殑綆楁硶 銆傛潯浠禢涓妭鐐癸紝M瀵硅妭鐐癸紝杈撳叆姣忎竴瀵硅妭鐐規(guī)椂鍊欙紝濡傛灉宸茬粡榪為氾紝鍒欏拷鐣ワ紝鍚﹀垯杈撳嚭鎺ョ偣騫舵洿鏂?
涓昏浠嬬粛涓夌綆楁硶錛氱涓縐嶏紝QUCK-FIND 鍒╃敤鏁扮粍璁板綍姣忎竴涓仈閫氬瓙鍥撅紝鍚屼竴涓仈閫氬瓙鍥劇殑鑺傜偣鏁扮粍鍙橀噺鏄浉鍚岀殑銆傚洜姝ゆ瘡璇誨叆涓瀵瑰氨闇瑕侀亶鍘哊涓暟緇勫彉閲忚冨療鏄惁闇瑕佹洿鏂幫紝鏈鍧忔椂闂碝N錛屽疄闄呬笂姣忎釜瀛愬浘涓洪珮搴︿負(fù)2鐨勬爲(wèi);絎簩縐嶏紝QUICK-UNION 鑱旈氬瓙鍥鵑渶瑕乽nion鏃?浠呬粎闇瑕佸皢涓や釜root union 鍥犳姣忎釜鑱旈氬瓙鍥鵑珮搴﹀彉楂橈紝鎵鏈塮ind root 浼?xì)娑堣椾竴瀹氭椂闂達(dá)紝褰撶劧鏃犻渶閬嶅巻N浜?錛屾渶鍧忔椂闂達(dá)紝涔熷氨鏄瘡嬈″潎闇瑕佷粠鍙跺瓙鑺傜偣鍘誨洖婧眰鏍癸紙榪欓噷涔︿腑涓懼緱渚嬪瓙濂藉儚鏈夐棶棰橈級(jí)涔熸槸MN錛涚涓夌涔熷氨鏄?QUICK WEIGHT UNION 榪欑鏂規(guī)硶鍦ㄤ袱涓瓙鏍?wèi)鍚堯q剁殑鏃跺欙紝涓嶆槸綆鍗曠殑鎸囧畾鍚堝茍絳栫暐錛岃屾槸緇忚繃鍒ゆ柇鐨勶紝鎶妔ize灝忥紙鐩稿簲楂樺害涔熷皬錛夌殑鍚堝茍鍒皊ize澶х殑閲岄潰錛涘疄闄呬笂寰堝鏄撶悊瑙e氨鏄敖閲忓噺灝戞爲(wèi)鐨勯珮搴︽潵榪芥眰quick-find鐨勬晥鐜囷紱
榪涜屼綔鑰呭啓閬撹礬寰勫帇緙╋紝涔熸槸榪欑鎬濇兂銆傛垨鑰呭疄鐜頒負(fù)鍏ㄩ儴鍘嬬緝錛屼篃灝辨槸璇村湪榪涜union鎿嶄綔鐨勬椂鍊欙紝鎵鏈塩heck鐨勮妭鐐歸兘鐩存帴閾炬帴鍒皍nion鍚庣殑鏂皉oot涓嬮潰錛屾垨鑰卙alf union 錛堝鏄撳疄鐜頒笖鎬ц兘濂斤級(jí)姣忔check鏃跺欙紝濡傛灉娌℃湁鍋滄loop綆鍗曠殑浠?id[i]=id[id[i]];鍗沖彲杈懼埌鍑忓皯鍒版牴璺濈鐨勬晥鏋溿?
half union 鐨勪富瑕佷唬鐮侊細(xì)
int i,j; for(i=p;id[i]!=i;i=id[i]) id[i]=id[id[i]]; for(j=p;id[j]!=j;j=id[j]) id[j]=id[id[j]]; if(i==j)continue; if(size[i]>size[j]) id[j]=i,size[i]+=size[j]; else id[i]=j,size[j]+=size[i];
絎竴嬈$敤windows live writer 璇曡瘯鏁堟灉鍝~