[TopCoder] SRM 580 DIV 2, 250p, 500p, 1000p, Solution

[TopCoder] SRM 580 DIV 2, 250p, 500p, 1000p, Solution

250 points

Problem Statement

     A group of freshman rabbits has recently joined the Eel club. No two of the rabbits knew each other. Today, each of the rabbits went to the club for the first time. You are given vector <int>s s and t with the following meaning: For each i, rabbit number i entered the club at the time s[i] and left the club at the time t[i].
Each pair of rabbits that was in the club at the same time got to know each other, and they became friends on the social network service Shoutter. This is also the case for rabbits who just met for a single moment (i.e., one of them entered the club exactly at the time when the other one was leaving).
Compute and return the number of pairs of rabbits that became friends today.

Definition

    
Class: ShoutterDiv2
Method: count
Parameters: vector <int>, vector <int>
Returns: int
Method signature: int count(vector <int> s, vector <int> t)
(be sure your method is public)
    

Constraints

s and t will contain between 1 and 50 integers, inclusive.
s and t will contain the same number of elements.
Each integer in s and t will be between 0 and 100, inclusive.
For each i, t[i] will be greater than or equal to s[i].

Examples

0)
    
{1, 2, 4}
{3, 4, 6}
Returns: 2
Rabbit 0 and Rabbit 1 will be friends because both of them are in the club between time 2 and 3.
Rabbit 0 and Rabbit 2 won’t be friends because Rabbit 0 will leave the club before Rabbit 2 enters the club.
Rabbit 1 and Rabbit 2 will be friends because both of them are in the club at time 4.
1)
    
{0}
{100}
Returns: 0
2)
    
{0,0,0}
{1,1,1}
Returns: 3
3)
    
{9,26,8,35,3,58,91,24,10,26,22,18,15,12,15,27,15,60,76,19,12,16,37,35,25,4,22,47,65,3,2,23,26,33,7,11,34,74,67,32,15,45,20,53,60,25,74,13,44,51}
{26,62,80,80,52,83,100,71,20,73,23,32,80,37,34,55,51,86,97,89,17,81,74,94,79,85,77,97,87,8,70,46,58,70,97,35,80,76,82,80,19,56,65,62,80,49,79,28,75,78}
Returns: 830

       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.

[Thoughts]

简单的计算区间重合。对于任意两个区间A和B,存在以下四种重合情况:

1. A与B左部重合

2. A与B右部重合

3. A包括B

4. B包括A

假设A的区间是(s[i], t[i]), 而B的区间为(s[j],t[j]),那么重合的判断式如下:

if(
(s[j] >= s[i] && S[j]<=t[i]) || (t[j]>=s[i] && t[j]<=t[i]) || (s[i] < s[j] && t[i] > t[j]) || (s[i]>s[j] && t[i] < t[j]))

{


count++;

}

显然,这个公式太长了,可以用如下简写版替代:

if(
min(t[i],t[j]) – max(s[i], s[j]) ) >==0

[Code]

1:  #include <vector>  
2: using namespace std;
3: class ShoutterDiv2
4: {
5: public:
6: int count(vector <int> s, vector <int> t);
7: };
8: int ShoutterDiv2::count(vector <int> s, vector <int> t)
9: {
10: int count=0;
11: for(int i =0; i< s.size()-1; i++)
12: {
13: for(int j =i+1; j< s.size(); j++)
14: {
15: if(min(t[i],t[j]) - max(s[i], s[j])>=0)
16: {
17: count++;
18: }
19: }
20: }
21: return count;
22: }

  

500 points

Problem Statement

     Rabbit went to a river to catch eels. All eels are currently swimming down the stream at the same speed. Rabbit is standing by the river, downstream from all the eels.

Each point on the river has a coordinate. The coordinates increase as we go down the stream. Initially, Rabbit is standing at the origin, and all eels have non-positive coordinates.

You are given two vector <int>s: l and t. These describe the current configuration of eels. The speed of each eel is 1 (one). For each i, the length of eel number i is l[i]. The head of eel number i will arrive at the coordinate 0 precisely at the time t[i]. Therefore, at any time T the eel number i has its head at the coordinate T-t[i], and its tail at the coordinate T-t[i]-l[i].

Rabbit may only catch an eel when some part of the eel (between head and tail, inclusive) is at the same coordinate as the rabbit. Rabbit can catch eels at most twice. Each time he decides to catch eels, he may catch as many of the currently available eels as he wants. (That is, he can only catch eels that are in front of him at that instant, and he is allowed and able to catch multiple eels at once.)

Return the maximal total number of eels Rabbit can catch.

Definition

    
Class: EelAndRabbit
Method: getmax
Parameters: vector <int>, vector <int>
Returns: int
Method signature: int getmax(vector <int> l, vector <int> t)
(be sure your method is public)
    

Constraints

l will contain between 1 and 50 elements, inclusive.
Each element of l will be between 1 and 1,000,000,000, inclusive.
l and t will contain the same number of elements.
Each element of t will be between 0 and 1,000,000,000, inclusive.

Examples

0)
    
{2, 4, 3, 2, 2, 1, 10}
{2, 6, 3, 7, 0, 2, 0}
Returns: 6
Rabbit can catch 6 eels in the following way:

  • At time 2, catch Eel 0, Eel 4, Eel 5, and Eel 6.
  • At time 8, catch Eel 1 and Eel 3.
1)
    
{1, 1, 1}
{2, 0, 4}
Returns: 2
No two eels are in front of Rabbit at the same time, so Rabbit can catch at most two eels.
2)
    
{1}
{1}
Returns: 1
3)
    
{8, 2, 1, 10, 8, 6, 3, 1, 2, 5}
{17, 27, 26, 11, 1, 27, 23, 12, 11, 13}
Returns: 7

       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.  

[Thoughts]

这一题和Leetcode里面买股票的题很类似。

将输入参数转化为Interval,对于任意鱼i,Interval[i].left = t[i], Interval[i].right = t[i]+l[i]。有了Interval数组之后,只要计算该数组在任意时间点上的重合数量,即可确定在该时间点上可以抓到多少鱼。题目已经说了,机器人最多可以抓两次鱼,所以模拟两次抓鱼即可。

[Code]

1:  #include <vector>  
2: #include <string>
3: using namespace std;
4: class EelAndRabbit
5: {
6: public:
7: int getmax(vector <int> l, vector <int> t);
8: int GetFish(vector <int>& l, vector <int>& t, int time, vector<int>& visited, bool isSecond);
9: };
10: int EelAndRabbit::getmax(vector <int> l, vector <int> t)
11: {
12: vector<int> visited(t.size());
13: int maxCatch=0;
14: for(int i =0;i<t.size(); i++) // t is the left boundary of interval, and l will be right boundary
15: {
16: l[i] = l[i] +t[i];
17: }
18: for(int i =0; i< t.size(); i++)
19: {
20: std::fill(visited.begin(), visited.end(),0);
21: int firstCatch = GetFish(l,t, t[i], visited,false); //first catch
22: int secondCatch=0;
23: for(int j=0;j<t.size(); j++)
24: {
25: if(j== i || t[i]>t[j]) continue;
26: int temp = GetFish(l,t, t[j], visited,true); //second catch
27: if(temp>secondCatch) secondCatch = temp;
28: }
29: if((firstCatch+secondCatch) >maxCatch)
30: {
31: maxCatch=firstCatch+secondCatch;
32: }
33: }
34: return maxCatch;
35: }
36: int EelAndRabbit::GetFish(vector <int>& l, vector <int>& t, int time, vector<int>& visited, bool isSecond)
37: {
38: int temp=0;
39: for(int i =0; i< t.size(); i++)
40: {
41: if(visited[i]) continue; //fish had been caught in first round.
42: if((time >= t[i] && time<=l[i])) // find the overlap interval
43: {
44: if(!isSecond) visited[i] =1;
45: temp++;
46: }
47: }
48: return temp;
49: }


1000 points

Problem Statement

     Rabbit and Eel are playing a board game. The game is played with a single token on a rectangular board that is divided into a grid of unit cells. Some cells contain a digit that represents the cost of placing the token onto that cell. Other cells contain the letter ‘x’ that represents a blocked cell. It is not allowed to place the token onto a blocked cell.
Initially, the token is placed on the leftmost cell of the topmost row. (The constraints guarantee that this cell will never be blocked and its cost will always be 0.) Eel starts the game by putting up some walls. Eel may place as many walls as he wants, including none. Each wall must be placed between two adjacent cells in the same column.
Once Eel has placed the walls, Rabbit gets to move the token. In each step, Rabbit may move the token one cell left, one cell right, or one cell down. (Note that Rabbit is not allowed to move the token upwards.) Rabbit may only move the token into cells that are not blocked. Each time Rabbit moves the token into a cell, he has to pay the cost associated with that cell.
The game ends when Rabbit first moves the token into the bottommost row. The constraints guarantee that this can be achieved if Eel does not place any walls. The game must always be allowed to end. That is, Eel must not place a set of walls that blocks all possible paths to the bottommost row.
Rabbit’s goal is to minimize and Eel’s goal is to maximize the total cost paid by Rabbit during the game. You are given the String[] costs representing the costs of cells: character j of element i of cost is either a digit that represents the cost written in the cell in row i, column j; or it is the letter ‘x’ that represents a blocked cell. Return the total cost of the game assuming that both Rabbit and Eel play optimally.

Definition

    
Class: WallGameDiv2
Method: play
Parameters: String[]
Returns: int
Method signature: int play(String[] costs)
(be sure your method is public)
    

Constraints

costs will contain between 2 and 50 elements, inclusive.
Each element of costs will contain between 1 and 50 characters, inclusive.
Each element of costs will contain the same number of characters.
Each character of each element of costs will be a letter ‘x’ or a decimal digit (‘0’-‘9’).
There will be at least one valid path from the leftmost cell of topmost row to a cell in the bottommost row.
costs[0][0] will always be ‘0’.

Examples

0)
    
{"042"
,"391"}
Returns: 13
Eel’s optimal stategy is to put two walls: between ‘0’-‘3’ and between ‘2’-‘1’. Then Rabbit’s optimal strategy is to move the token along the path ‘0’->’4′->’9′. The total cost will be 13.
1)
    
{"0xxxx"
,"1x111"
,"1x1x1"
,"11191"
,"xxxx1"}
Returns: 16
There’s only one path from the starting cell to the bottom row and Eel isn’t allowed to block it. Rabbit will move the token along this path and will get to pay a cost of 16. Note that it is not allowed to move the token upwards.
2)
    
{"0"
,"5"}
Returns: 5
3)
    
{"0698023477896606x2235481563x59345762591987823x663"
,"1x88x8338355814562x2096x7x68546x18x54xx1077xx5131"
,"64343565721335639575x18059738478x156x781476124780"
,"2139850139989209547489708x3466104x5x3979260330074"
,"15316x57171x182167994729710304x24339370252x2x8846"
,"459x479948xx26916349194540891252317x99x4329x34x91"
,"96x3631804253899x69460666282614302698504342364742"
,"4x41693527443x7987953128673046855x793298x8747219x"
,"7735427289436x56129435153x83x37703808694432026643"
,"340x973216747311x970578x9324423865921864682853036"
,"x1442314831447x860181542569525471281x762734425650"
,"x756258910x0529918564126476x481206117984425792x97"
,"467692076x43x91258xx3xx079x34x29xx916574022682343"
,"9307x08x451x2882604411x67995x333x045x0x5xx4644590"
,"4x9x088309856x342242x12x79x2935566358156323631235"
,"04596921625156134477422x2691011895x8564609x837773"
,"223x353086929x27222x48467846970564701987061975216"
,"x4x5887805x89746997xx1419x758406034689902x6152567"
,"20573059x699979871151x444449x5170122650576586x898"
,"683344308229681464514453186x51040742xx138x5170x93"
,"1219892x9407xx63107x24x4914598xx4x478x31485x69139"
,"856756530006196x8722179365838x9037411399x41126560"
,"73012x9290145x1764125785844477355xx827269976x4x56"
,"37x95684445661771730x80xx2x459547x780556228951360"
,"54532923632041379753304212490929413x377x204659874"
,"30801x8716360708478705081091961915925276739027360"
,"5x74x4x39091353819x10x433010250089676063173896656"
,"03x07174x648272618831383631629x020633861270224x38"
,"855475149124358107x635160129488205151x45274x18854"
,"091902044504xx868401923845074542x50x143161647934x"
,"71215871802698346x390x2570413992678429588x5866973"
,"87x4538137828472265480468315701832x24590429832627"
,"9479550007750x658x618193862x80317248236583631x846"
,"49802902x511965239855908151316389x972x253946284x6"
,"053078091010241324x8166428x1x93x83809001454563464"
,"2176345x693826342x093950x12x7290x1186505760xx978x"
,"x9244898104910492948x2500050208763770568x92514431"
,"6855xx7x145213846746325656963x0419064369747824511"
,"88x15690xxx31x20312255171137133511507008114887695"
,"x391503034x01776xx30264908792724712819642x291x750"
,"17x1921464904885298x58x58xx174x7x673958x9615x9230"
,"x9217049564455797269x484428813681307xx85205112873"
,"19360179004x70496337008802296x7758386170452xx359x"
,"5057547822326798x0x0569420173277288269x486x582463"
,"2287970x0x474635353111x85933x33443884726179587xx9"
,"0x697597684843071327073893661811597376x4767247755"
,"668920978869307x17435748153x4233659379063530x646x"
,"0019868300350499779516950730410231x9x18749463x537"
,"00508xx083203827x42144x147181308668x3192478607467"}
Returns: 3512

       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.  

[Thoughts]

DP

首先如果这个题只有Robbit的话,很容易发现这是一个最小路径的DP。此题巧妙处在于做了一个转换,增加了Eel,转换为双人博弈。题目中说了,Eel总是会增加阻碍,让Robbit只能走代价最高的路径。简单的说,如果Eel非常尽职的话,Robbit在每一行只能选择代价最高的那个cell,因为凡是好的路径,必然已经被Eel设置了路障。所以这个题说到底,就是最大路径的DP。具体看code。

[Code]

1:  #include <vector>  
2: #include <string>
3: using namespace std;
4: class WallGameDiv2
5: {
6: public:
7: int play(vector <string> costs);
8: };
9: int cost[51][51];
10: int dp[51][51];
11: int row, col;
12: int WallGameDiv2::play(vector <string> costs)
13: {
14: //initialize the cost and dp array
15:
memset(cost, -1, sizeof(cost));
16:
memset(dp, -1, sizeof(dp));
17: row = costs.size();
18: col = costs[0].size();
19: for(int i =0; i< row; i++)
20: {
21: for(int j=0; j< col; j++)
22: {
23: if(costs[i][j] != 'x')
24: {
25: cost[i][j] = costs[i][j]-'0';
26: }
27: }
28: }
29: dp[0][0] =0;
30: //first row
31: for(int i =1; i< col && cost[0][i]!=-1; i++)
32: {
33: dp[0][i] = (dp[0][i-1] + cost[0][i]);
34: }
35: for(int i =1; i< row-1; i++)
36: {
37: for(int j=0; j<col; j++)
38: {
39: if(dp[i-1][j] == -1) continue;
40: //for each cost i,j, update the value with worst cost
41: int estiCost = dp[i-1][j];
42: //right
43: for(int k = j; k<col && cost[i][k]!=-1; k++)
44: {
45: estiCost+=cost[i][k];
46: dp[i][k] = max(dp[i][k], estiCost);
47: }
48: //left
49: estiCost = dp[i-1][j];
50: for(int k=j; k >=0&& cost[i][k]!=-1; k--)
51: {
52: estiCost+=cost[i][k];
53: dp[i][k] = max(dp[i][k], estiCost);
54: }
55: }
56: }
57: // last row is special
58: int steps = 0;
59: for(int i =0; i< col; i++)
60: {
61: if(cost[row-1][i] ==-1) continue;
62: steps = max(steps, dp[row-2][i]+cost[row-1][i]);
63: }
64: return steps;
65: }

[Note]

Line 15 & 16

最初的写法是:


memset(cost, -1, sizeof(cost)
/sizeof(int));


memset(dp, -1, sizeof(dp)
/sizeof(int));

刚开始提交,过不了大数据,翻来覆去的看,也没发现有什么问题。最后拿到visual studio里面debug才发现问题,多了sizeof(int)。对于c++不是特别熟悉呀。

转载于:https://www.cnblogs.com/codingtmd/archive/2013/05/26/5078868.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110118.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SQL Server备份集中的数据库备份与现有的数据库不同的解决

    SQL Server备份集中的数据库备份与现有的数据库不同的解决在SQLServer2005和2008中,如果您新建了数据库,打算还原数据库备份的话,如果两者的数据库结构等不同的话,就会提示:备份集中的数据库备份与现有的数据库不同……之类的错误,如下图。巧的很,今天烈火小编的一位朋友也遇到这样的问题,我们一起来看看解决方法。其实解决方法很简单,就是在左侧的选项中,将“覆盖现有数据库”打上勾就可以了。如下图:

    2022年5月6日
    56
  • Linux nmap命令整理

    Linux nmap命令整理nmap–iflist:查看本地主机的接口信息和路由信息-A:选项用于使用进攻性方式扫描-T4:指定扫描过程使用的时序,总有6个级别(0-5),级别越高,扫描速度越快,但也容易被防火墙或IDS检测并屏蔽掉,在网络通讯状况较好的情况下推荐使用T4-oXtest.xml:将扫描结果生成test.xml文件,如果中断,则结果打不开-oAtest.xml:将扫描结果生成test.xml文件,中断后,结果也可保存-oGtest.txt:将扫描结果生成test…

    2022年5月28日
    44
  • 微信表情 代码_微信暗语代码大全

    微信表情 代码_微信暗语代码大全/::)笑话/::~撇嘴/::B色/:,@f奋斗/:heart爱心/:showlove嘴唇/:cake蛋糕/:gift礼物/:handclap鼓掌/::*亲亲/:rose玫瑰/:kiss献吻/:love爱情/:okOK/:lvu爱你/:jj勾引/:@)抱拳/:sh…

    2025年8月15日
    2
  • 至尊问题「建议收藏」

    称号:已知m、n为整数,且满足下列两个条件:① m、n∈1,2,…,K,(1≤K≤10^9)② (n^ 2-mn-m^2)^2=1编一程序,对给定K,求一组满足上述两个条件的m、n,而且使m^2+n^2的值最大。比如,若K=1995,则m=987,n=1597,则m、n满足条件,且可使m^2+n^2的值最大。输入输入仅一行,K的值。输出…

    2022年4月13日
    45
  • 关于ValidateInput(false)无法取消验证的解决方案

    关于ValidateInput(false)无法取消验证的解决方案

    2021年6月6日
    89
  • Linux守护进程的编程实现

    Linux守护进程的编程实现

    2021年12月1日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号