250pt:
题目:
Problem Statement
|
|
Elly has a string S of uppercase letters and a magic device that can modify the string. The strength of the device is an int
L.
The device does the following: The user enters a 0-based index i such that 0 <= i <= length(S)-L. The device then takes
L letters of S, starting at index i, and puts these letters into alphabetical order.
Formally, the letters that get reordered are the letters S[i],
S[i+1], and so on, until and including S[i+L-1].
For example, let S="TOPCODER" and let L=4. If the user chooses i=0, the selected substring will be "TOPC". These letters are rearranged into alphabetical order ("COPT") while the rest of the string remains unchanged ("....ODER").
Thus, the result would be the string "COPTODER". If the user were to choose i=2 instead, the resulting string would be "TOCDOPER". Here, "TO....ER" was left unchanged, and "PCOD" was changed into "CDOP".
Elly‘s magic device has a flaw: it can only be used once and then it self-destructs. You are given the string
S and the int L described above. Return the lexicographically smallest string Elly can create by using the device exactly once. |
Definition
|
|
Class: |
EllysSubstringSorter |
Method: |
getMin |
Parameters: |
string, int |
Returns: |
string |
Method signature: |
string getMin(string S, int L) |
(be sure your method is public) |
|
Limits
|
|
Time limit (s): |
2.000 |
Memory limit (MB): |
256 |
|
Notes
|
- |
A string S1 is lexicographically smaller than a string S2 if S1 contains a smaller character than S2 at the first index where they differ. |
Constraints
|
- |
L will be between 2 and 50, inclusive. |
- |
S will contain between L and 50 characters, inclusive. |
- |
Each character of S will be an uppercase letter of the English alphabet (‘A‘-‘Z‘). |
Examples
|
0) |
|
|
|
Returns: "COPTODER"
|
The best we can do here is to sort the first 4 characters of the string. |
|
|
1) |
|
|
|
Returns: "EPRSIT"
|
The best solution is obtained by choosing i=1, i.e., by sorting the letters in the substring "SPR". |
|
|
2) |
|
|
|
Returns: "AAAAAAAAA"
|
Sometimes sorting doesn‘t do anything. |
|
|
3) |
|
|
|
4) |
|
|
|
5) |
|
|
"AAAWDIUAOIWDESBEAIWODJAWDBPOAWDUISAWDOOPAWD"
|
21
|
|
Returns: "AAAAAABDDDEEIIIJOOSUWWWWDBPOAWDUISAWDOOPAWD"
|
|
|
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.
思路:水题,枚举把长度为L的字符串排序
代码:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class EllysSubstringSorter {
public:
string getMin(string, int);
};
string EllysSubstringSorter::getMin(string S, int L) {
string ret = S;
for(int i=0;i<=S.length()-L;i++)
{
string temp = S;
sort(temp.begin()+i,temp.begin()+i+L);
if(temp<ret)
ret = temp;
}
return ret;
}
500pt:
题目:
Problem Statement
|
|
Elly and Kris play the following game. In the beginning Kristina thinks of a number between 1 and 1,000,000,000, inclusive. After that Elly starts trying to guess it. In each round she says a number and Kristina says what is the absolute difference between
the number she has thought of, and the number Elly guessed. Now Elly wonders if the guesses she has already made are sufficient to uniquely determine Kristina‘s number.
You are given a vector <int> guesses and a vector <int>
answers of the same length. For each valid i, in round i of the game (0-based index) Elly guessed the number
guesses[i] and Kristina answered answers[i]. If Kristina‘s number can be uniquely determined, return that number. If there are multiple possibilities that are consistent with the current set of guesses and answers, return -1.
If it can be shown that at some point Kristina has lied (some of her answers were inconsistent), return -2. |
Definition
|
|
Class: |
EllysNumberGuessing |
Method: |
getNumber |
Parameters: |
vector <int>, vector <int> |
Returns: |
int |
Method signature: |
int getNumber(vector <int> guesses, vector <int> answers) |
(be sure your method is public) |
|
Limits
|
|
Time limit (s): |
2.000 |
Memory limit (MB): |
256 |
|
Constraints
|
- |
guesses and answers will each contain between 1 and 50 elements, inclusive. |
- |
guesses and answers will contain the same number of elements. |
- |
Each element of guesses will be between 1 and 1,000,000,000, inclusive. |
- |
Each element of answers will be between 1 and 999,999,999, inclusive. |
Examples
|
0) |
|
|
|
Returns: 606
|
Apparently Kristina has thought of the number of this SRM. |
|
|
1) |
|
|
{100, 50, 34, 40}
|
{58, 8, 8, 2}
|
|
Returns: 42
|
It is not guaranteed that Elly has used a perfect strategy so far. |
|
|
2) |
|
|
{500000, 600000, 700000}
|
{120013, 220013, 79987}
|
|
Returns: -2
|
The answers here are inconsistent. After the second guess we can conclude that the answer is below 500000. But the third one indicates that it is above 500000. Thus, Kristina is a liar and you should return -2. |
|
|
3) |
|
|
|
Returns: -1
|
There are multiple possibilities here, thus you should return -1. |
|
|
4) |
|
|
{76938260, 523164588, 14196746, 296286419, 535893832,
41243148, 364561227, 270003278, 472017422, 367932361,
395758413, 301278456, 186276934, 316343129, 336557549,
52536121, 98343562, 356769915, 89249181, 335191879}
|
{466274085, 20047757, 529015599, 246925926, 7318513,
501969197, 178651118, 273209067, 71194923, 175279984,
147453932, 241933889, 356935411, 226869216, 206654796,
490676224, 444868783, 186442430, 453963164, 208020466}
|
|
Returns: 543212345
|
|
|
5) |
|
|
|
Returns: 84
|
Don‘t forget that the number Kris has thought of must be between 1 and 1,000,000,000. |
|
|
6) |
|
|
|
Returns: 999799999
|
Don‘t forget that the number Kris has thought of must be between 1 and 1,000,000,000. |
|
|
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.
分析:
通过第一个可以得到两个候选值,然后遍历后面的数据,看是不是在这两个里面,这样就能立马排除一个,然后再看后面的有没有冲突,到了最后如果有两个后悬置就返回-1,只有一个就返回值,没有候选值就返回-2.
代码:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class EllysNumberGuessing {
public:
int getNumber(vector <int>, vector <int>);
};
int EllysNumberGuessing::getNumber(vector <int> guesses, vector <int> answers) {
const int low = 1;
const int high=1000000000;
set<int> anss;
for(int i=0;i<guesses.size();i++)
{
int a = guesses[i];
int b = answers[i];
set<int> newans;
if(a+b>=low&&a+b<=high)
{
if(i==0)
{
newans.insert(a+b);
}
else
{
if(anss.count(a+b)>0)
newans.insert(a+b);
}
}
if(a-b>=low&&a-b<=high)
{
if(i==0)
{
newans.insert(a-b);
}
else
{
if(anss.count(a-b)>0)
newans.insert(a-b);
}
}
anss = newans;
}
if(anss.size()==1)
{
int ret = 0;
set<int>::iterator it = anss.begin();
for(;it!=anss.end();it++)
ret = *it;
return ret;
}
else if(anss.size()==2)
return -1;
else
return -2;
}
1000pt:
题目:
Problem Statement
|
|
Elly and Kris play the following game. In the beginning there are several boxes aligned in a row. The boxes may or may not contain candy. As a matter of fact, the girls know exactly how many candies each of them contains. This information is given to you
in the vector <int> sweets.
Starting with Elly, the girls make moves in alternating order. A single move looks as follows: the player whose turn it is chooses one of the non-empty boxes and takes all the sweets from it. After that the amount of candy in the neighboring boxes is doubled.
For example, suppose that there were five boxes with {20, 50, 70, 0, 30} sweets, respectively. If the girl whose turn it was chose box 0, then in the next turn the number of sweets in the boxes would be {0, 100, 70, 0, 30}. If she chose box 1, then it would
be {40, 0, 140, 0, 30}. If she chose box 2, it would be {20, 100, 0, 0, 30}. If she chose box 4, it would be {20, 50, 70, 0, 0}. Note that the girl cannot choose box 3, because it is empty.
The game ends when all boxes are empty. The winner of the game is the girl who has more candies at the end of the game.
Return the name of the girl that will win the game if both girls play optimally, or "Draw" if they end up with the same number of candies. |
Definition
|
|
Class: |
EllysCandyGame |
Method: |
getWinner |
Parameters: |
vector <int> |
Returns: |
string |
Method signature: |
string getWinner(vector <int> sweets) |
(be sure your method is public) |
|
Limits
|
|
Time limit (s): |
2.000 |
Memory limit (MB): |
256 |
|
Notes
|
- |
Playing optimally means that if there is a move, which guarantees that the girl whose turn it is will win no matter what the other girl does, she will play it. If there is no such move, but there is one, which would guarantee a draw, she will use it instead. |
- |
The game always ends after a finite number of moves, because the number of empty boxes increases in each step. |
Constraints
|
- |
sweets will contain between 1 and 10 elements, inclusive. |
- |
Each element of sweets will be between 0 and 1000, inclusive. |
Examples
|
0) |
|
|
|
Returns: "Kris"
|
If Elly takes the 20, then Kris can take the 30, leaving Elly the choice between 100 and 70. Elly should take the 100, but then Kris will take 140 and win.
If Elly takes the 30, then Kris can take the 20, leaving Elly the same choice.
If Elly takes the 50, then Kris can take the 140, leaving Elly the choice between a 40 and a 30.
If Elly takes the 70, then Kris can take the 100, leaving Elly the same choice.
In any case, Kris will take more than Elly and win. |
|
|
1) |
|
|
|
Returns: "Elly"
|
Here Elly can take the 42 in her first move and win, no matter whether Kris chooses the 26 or 7. |
|
|
2) |
|
|
|
Returns: "Draw"
|
Elly cannot win, but she can force a draw by taking the 20 (and leaving 20 to her opponent). |
|
|
3) |
|
|
|
4) |
|
|
{41, 449, 328, 474, 150, 501, 467, 329, 536, 440}
|
|
Returns: "Kris"
|
|
|
5) |
|
|
{177, 131, 142, 171, 411, 391, 17, 222, 100, 298}
|
|
Returns: "Elly"
|
|
|
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.
比赛的时候本来以为dfs太耗时了,结果数据规定最多只有10个元素,数据小爆搜就行
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
int dfs(vector<int> sweets,vector<int> nz,int a,int b,bool amove)
{
if(nz.size()==0)
{
if(a>b)
return 1;
else if(a==b)
return 0;
else
return -1;
}
int ret = -1;
if(!amove)ret= 1;
for(int i=0;i<nz.size();i++)
{
int index = nz[i];
int candy = sweets[index];
if(amove)a+=candy;
else b+=candy;
sweets[index] = 0;
if(index>0)
sweets[index-1]*=2;
if(index<sweets.size()-1)
sweets[index+1]*=2;
vector<int> newNz = nz;
newNz.erase(find(newNz.begin(),newNz.end(),index));
int r = dfs(sweets,newNz,a,b,!amove);
sweets[index]=candy;
if(amove)a-=candy;
else b-=candy;
if(index>0)
sweets[index-1]/=2;
if(index<sweets.size()-1)
sweets[index+1]/=2;
if(amove)
{
if(r==1)
return 1;
else if(r==0)
ret = 0;
}
else
{
if(r==-1)
return -1;
else if(r==0)
ret = 0;
}
}
return ret;
}
class EllysCandyGame {
public:
string getWinner(vector <int>);
};
string EllysCandyGame::getWinner(vector <int> sweets) {
vector<int> nz;
for(int i=0;i<sweets.size();i++)
{
if(sweets[i]>0)
nz.push_back(i);
}
int r = dfs(sweets,nz,0,0,true);
if(r==1)
return "Elly";
else if (r==0)
return "Draw";
else
return "Kris";
}
topcoder srm606 div2