??xml version="1.0" encoding="utf-8" standalone="yes"?>
tchs-2 250pt 直接就?我写??500pt 暴力可以q,但是判断时候不能用stringstream 用算术判?也可以用构造法 1000pt 每一位有三种可能?/p>
不用Q保持不动,变化Q分别递归计算valueq更新结果即可,׃递归深度最多只?3层所以不会tle
另外也可以写出基Cؓ3的@环来遍历每一U情况具体看代码
for(i=0,A[0]++;A[i]>2;i++){
A[i]=0;A[i+1]++;
}
tchs-3 1000pt 要想使乘U最大,需要更多的3卛_ 500pt 又看错题?~~~ft 要注意题目一定要看清?/p>
tchs-4 500pt 模拟题,好难?音乐的~ 可以都乘?6 用整数来计算 点会很烦~ q种题思\要清?一步一步来
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)
要求最好的得分Q要遍历所有的排列方式昄不可能。如果{化问题ؓ依次攄每个选手到每一个位|,那么只要求出剩下选手排列中得得分最大的可以了Q可以想到dpҎ(gu)的最有子问题定义?/font>
用每一位记录当前的选手分配情况Q如果ؓ0Q则已经分配Q如果ؓ1Q则q未分配。pos来记录当前要分配的位|。对于每一个子集,考虑每一个ؓ1的也是未分配的选手使位于pos位置Q然后check 是否满在他之后的球员(也就是剩下的?的位Q是否满x件的不等式,满则更新当前分配的最优成l。最l?2^N-1 的成l即为所求?/font>
上面的方法用递归Q下面是使用递推的程序。即从子集出发最l到?^n-1的思\。在q里把A队伍先从大到排序。在每一个子集记录每一个未分配选手的得分和要分配的位置?/font>
E序中思\为:依次从子集中选取一个选手作ؓWbits-1个选手Q也是说子集中剩下的元素要攑֜q个选手前面Q由于选手从小到大遍历Q只需要遍历到s[j]<=s[bits-1]+lim 为止Q也p满q个子集中最的选手+lim >=选中?卛_Q然后更新当前子集的得分
注释中思\? 依次从子集选取一个选手作ؓ当前子集中的W一个元素,也就是整体的Wn-bits个元素,q次从大到小遍历Q这样只需要保证,位于子集W一个的元素+lim>=子集中最大的元素(s[0])Q即可,然后更新当前子集得分
最l全?^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'.
按照题目说明可知Q按照找到的序Q所有从低向高U发的信得数目和应该是最的。又观察刎ͼ只要定了第一个,也就是别最高的bossQ那么剩下的员工怎么排列Q他们向最高boss 发信数是不变的,从而子问题是在剩下员工中扑ֈ 低向高U发信数最的排列 Q显然这W合DP问题最优子l构的问题?/font>
T[1···N]=Min{sum(N <->{1-N-1},T[1-N-1] } 得到状态方E后很容易写出程?Q可以用备忘录dp
惌vz的服q没拿,于是跑下d服Q看了会dota vod Q重新登入,比赛竟然l箋·····交了500 开始看1000 没思\Q冻M?/font>
challenge没啥意思,都没人在Q?50的太弱智都对?#183;···׃中间出错Q这ơsrm 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'.
数据量很,扑ֈ未比的比赛场?Q然后枚丑U输赢情况,更新解就可以了。或者@?^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分,?队多3Z上比赛的 肯定?队靠前,?队分或者相{的一定在0队之后,剩下的就是我们要考虑的了。如果A3+L+1>=E3 也就是说?队多胜一场的队伍Q如果让他们在最后一轮都输,那么0队可以获得最好成l?A3+1;
然而如果不行剩下的q些E3要有一?E3+1)/2个要得到3分从而比0队高Qover~
刚才偶然看见pojq查集的题集 copyq来自己也写写看~~·
慢慢来~~ 写完一道上一道~~
POJ 1182 食物?/strong>
http://acm.pku.edu.cn/JudgeOnline/problem?id=1182
q以为是最单的一道呢Q被列在最前面Q原来是扩展的应用,不是那么直接啊~
U结了一会儿Q看了解题报告才有思\。关键是在ƈ查集的基上增加一个数l,l护其与根节点的关系Q?/font>
详情写到注释里面吧:
int A[100003][3]; int root[50005],k[50005];//root记录节点所属于的树木id k记录节点与根的关p? 同类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];//如果W一层,则返? int father=root[x]; root[x]=find(father);//攑ֈW一层,减少下次find旉Q减树的高? k[x]=(k[x]+k[father])%3;//p׃根的关系以及父节点与根的关系推断出新的自׃根的关系Q实际上是ؓ了解决union后的节点与根的关p?/span>
//因ؓunion后,非根节点q没有更新kQ因此需要在此处处理更新 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大的上去Q优化作用不? root[b]=a ; //b成ؓa的子? k[b]=(k[x]-k[y]+kind+3)%3;//要把b变成a的子树,从a出发Q到xQ经qkindQ到yQ再到b Q可以得到kb的状态改变公? 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路径压羃~{下试试看全压羃的效率~那样是递归调用? 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);//把size的弄到大的上面 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);//单的都和W一个合q? else{ root=Find(root); x=Find(stu);if(root!=x)Union(root,x); } } } printf("%d\n",sz[Find(0)]); } }
又一道水题~~ 呵呵
不脓(chung)代码?和上面一道基本一样~
http://acm.pku.edu.cn/JudgeOnline/problem?id=2524
POJ 1861
http://acm.pku.edu.cn/JudgeOnline/problem?id=1861
kruskal+q查?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];//把每一个\q的节点攑ֈ父下面? 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);//从小到大选n-1条边Q如果v点和l点在一个集合则continueQelse 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 数字表达教程里的习题~ 题目大意 求用桌子的期望。由于到来group的个C定,每个group需要的桌子不定Qɼ定期望变得困难。但考虑对于numTables来说,使用桌子的状态仅仅有 2^numTablesU,因此考虑在这些状态改变的q程中来计算期望Q也是计算在每个状态下面的期望桌子数目。在每个状态到达时Q依ơ考虑来了一个group需要k个位子,如果rU安排可以满k个位子,那么当前状态的期望D加上 来k个位子的概率 X QrU安排分别的期望?/ rQ?其中求r中安排期望和则需?递归调用函数。显然利用memo可以减少重复计算于是有下面的解法Q?/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
}
看比赛中有另一U解法,x据题目,在到达每ơfail to serve a group 的时?Ҏ(gu)此时的桌子数量,和到达这U状态的概率 来计:
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++) //循环最多numTables+1 ?/font>
{flag=true;
for(int j=0;j<p.size();j++){
int mask=(1<<(j+1))-1;//注意UMq算W的优先U低,注意加括?/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;
优先U很Ҏ(gu)错:
http://www.cppreference.com/wiki/operator_precedence~。~
典型的几?/font>
++ -- <post-incre-decre>
~ <bitwise complement> !<not>&<addresss> *<dereference>&<address>
* / %
+ -
>> <<
< <= > >=
== !=
&
^ xor
|
&&
||
?=
= += –= <<= >>=
,
从上C依次降低~~~~~~~~~~~~~~~~~~··