TopCoder SRM 606 Div2 题解

第一次做TC,惨败而归。。。就当做熟悉TC的规则了吧%>_<%

250

妈蛋,这个题for的下标打错了。。。也过了pretest。。。最后被叉了。。。

题意:

给你一个字符串S,和一个int数字L,让你找到从S中找到一个长L的字串排序后的放回原位得到新字符串的字典序最小的字符串

解法:

暴力即可

代码:

 1 // #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <set>
 8 #include <list>
 9 #include <map>
10 #include <iterator>
11 #include <cstdlib>
12 #include <vector>
13 #include <queue>
14 #include <stack>
15 #include <algorithm>
16 #include <functional>
17 using namespace std;
18 typedef long long LL;
19 #define ROUND(x) round(x)
20 #define FLOOR(x) floor(x)
21 #define CEIL(x) ceil(x)
22 const int maxn = 0;
23 const int maxm = 0;
24 const int inf = 0x3f3f3f3f;
25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
26 const double INF = 1e30;
27 const double eps = 1e-6;
28 const int P[4] = {0, 0, -1, 1};
29 const int Q[4] = {1, -1, 0, 0};
30 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
31 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
32 
33 class EllysSubstringSorter
34 {
35 public:
36     string getMin(string S, int L);
37     char xxx[100];
38     int aaa[100];
39     bool cmp(char a,char b)
40     {
41         return a<b;
42     }
43 };
44 
45 string EllysSubstringSorter::getMin(string S, int L)
46 {
47     string ans=S;
48     for(int i=0;i<=S.size()-L;i++)
49     {
50         string tmp=S.substr(i,L);
51         string first=S.substr(0,i);
52         string last=S.substr(i+L);
53         // sort(tmp,cmp);
54         for(int j=0;j<tmp.size();j++) aaa[j]=tmp[j];
55         sort(aaa,aaa+tmp.size());
56         for(int j=0;j<tmp.size();j++) tmp[j]=(char)aaa[j];
57         string tt=first+tmp+last;
58         ans=min(tt,ans);
59     }
60     return ans;
61 }
View Code

500

dfs里面忘写向下一层递归,竟然神奇地pretests passed了,而且还没有被叉掉。。。

题意:

给你一个guesses和一个answer数组,对于每一个i,总有guesses[i]+answer[i]或guesses[i]-answer[i]与guesses[i-1]+answer[i-1]或guesses[i-1]-answer[i-1]相同么?

且guesses[i]+answer[i]<=1000000000    guesses[i]-answer[i]>=1

如果有两个这样的数,输出-1;如果一个,则输出这个数;如果没有,则输出-2;

解法:

因为数组才50个,暴力即可,dfs

代码:

  1 // #pragma comment(linker, "/STACK:102400000,102400000")
  2 #include <cstdio>
  3 #include <iostream>
  4 #include <cstring>
  5 #include <string>
  6 #include <cmath>
  7 #include <set>
  8 #include <list>
  9 #include <map>
 10 #include <iterator>
 11 #include <cstdlib>
 12 #include <vector>
 13 #include <queue>
 14 #include <stack>
 15 #include <algorithm>
 16 #include <functional>
 17 using namespace std;
 18 typedef long long LL;
 19 #define ROUND(x) round(x)
 20 #define FLOOR(x) floor(x)
 21 #define CEIL(x) ceil(x)
 22 const int maxn = 0;
 23 const int maxm = 0;
 24 const int inf = 0x3f3f3f3f;
 25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
 26 const double INF = 1e30;
 27 const double eps = 1e-6;
 28 const int P[4] = {0, 0, -1, 1};
 29 const int Q[4] = {1, -1, 0, 0};
 30 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
 31 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
 32 
 33 class EllysNumberGuessing
 34 {
 35 public:
 36     bool flag = 0;
 37     int ans = -1;
 38     vector <int> tmp1;
 39     vector <int> tmp2;
 40     void dfs(int dep);
 41     int n;
 42     vector <int> g, a;
 43     int getNumber(vector <int> guesses, vector <int> answers);
 44 };
 45 void EllysNumberGuessing::dfs(int dep)
 46 {
 47     if (dep >= n) return;
 48     if (ans == -2) return;
 49     if (dep == 0)
 50     {
 51         tmp1.push_back(g[dep] + a[dep]);
 52         tmp2.push_back(g[dep] - a[dep]);
 53         if (tmp2[0] <= 0 && tmp1[0] > 1000000000)
 54         {
 55             flag = 1;
 56             ans = -2;
 57             return;
 58         }
 59         if (tmp2[0] <= 0)
 60         {
 61             flag = 1;
 62             ans = g[dep] + a[dep];
 63         }
 64         if (tmp1[0] > 1000000000)
 65         {
 66             flag = 1;
 67             ans = g[dep] - a[dep];
 68         }
 69         dfs(dep + 1);
 70         return;
 71     }
 72     if (!flag)
 73     {
 74         int aa = g[dep] + a[dep];
 75         int bb = g[dep] - a[dep];
 76         if (aa == tmp1[dep - 1] && bb == tmp2[dep - 1])
 77         {
 78             tmp1.push_back(aa);
 79             tmp2.push_back(bb);
 80             dfs(dep + 1);
 81         }
 82         else
 83         {
 84             if (aa == tmp1[dep - 1] || aa == tmp2[dep - 1])
 85             {
 86                 ans = aa;
 87                 flag = 1;
 88             }
 89             else if (bb == tmp1[dep - 1] || bb == tmp2[dep - 1])
 90             {
 91                 ans = bb;
 92                 flag = 1;
 93             }
 94             else
 95             {
 96                 ans = -2;
 97                 flag = 0;
 98             }
 99             tmp1.push_back(aa);
100             tmp2.push_back(bb);
101             dfs(dep + 1);
102         }
103         return;
104     }
105     if (flag)
106     {
107         int aa = g[dep] + a[dep];
108         int bb = g[dep] - a[dep];
109         if (aa != ans && bb != ans)
110         {
111             ans = -2;
112             flag = 0;
113         }
114         dfs(dep + 1);
115     }
116 }
117 
118 int EllysNumberGuessing::getNumber(vector <int> guesses, vector <int> answers)
119 {
120     tmp1.clear();
121     tmp2.clear();
122     n = guesses.size();
123     g = guesses;
124     a = answers;
125     flag = 0;
126     ans = -1;
127     dfs(0);
128     return ans;
129 }
View Code

1000

因为过年要走,读了题就没时间做了。。。

题意:

给你一个数组,最大为10个数,两个人轮流取数,当取一个数的时候,它相邻两边的数字大小*2,被取到的数变为0,当全变成0的时候结束,取到数字总数最大的赢,两个人都是最优策略,问谁赢

解法:

因为数据规模只有10,直接暴力模拟一下就好了。。。

代码:

 1 // #pragma comment(linker, "/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cstring>
 5 #include <string>
 6 #include <cmath>
 7 #include <set>
 8 #include <list>
 9 #include <map>
10 #include <iterator>
11 #include <cstdlib>
12 #include <vector>
13 #include <queue>
14 #include <stack>
15 #include <algorithm>
16 #include <functional>
17 using namespace std;
18 typedef long long LL;
19 #define ROUND(x) round(x)
20 #define FLOOR(x) floor(x)
21 #define CEIL(x) ceil(x)
22 const int maxn = 0;
23 const int maxm = 0;
24 const int inf = 0x3f3f3f3f;
25 const LL inf64 = 0x3f3f3f3f3f3f3f3fLL;
26 const double INF = 1e30;
27 const double eps = 1e-6;
28 const int P[4] = {0, 0, -1, 1};
29 const int Q[4] = {1, -1, 0, 0};
30 const int PP[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
31 const int QQ[8] = { -1, 0, 1, -1, 1, -1, 0, 1};
32 
33 class EllysCandyGame
34 {
35 public:
36     int tt[20];
37     int n;
38     int dfs(int a, int b, int cnt)
39     {
40         if (cnt == 0) return a - b;
41         int ret = -inf;
42         for (int i = 1; i <= n; i++)
43         {
44             if (!tt[i]) continue;
45             int t1 = tt[i], t2 = tt[i - 1], t3 = tt[i + 1];
46             tt[i] = 0, tt[i - 1] *= 2, tt[i + 1] *= 2;
47             ret = max(ret, -dfs(b, a + t1, cnt - 1));
48             tt[i] = t1, tt[i - 1] = t2, tt[i + 1] = t3;
49         }
50         return ret;
51     }
52     string getWinner(vector <int> sweets)
53     {
54         int cnt = 0;
55         tt[0] = 0;
56         n = sweets.size();
57         for (int i = 0; i < sweets.size(); i++)
58         {
59             tt[i + 1] = sweets[i];
60             if (tt[i + 1]) cnt++;
61         }
62         int ans = dfs(0, 0, cnt);
63         if (ans > 0) return "Elly";
64         else if (ans < 0) return "Kris";
65         else return "Draw";
66     }
67 };
View Code
原文地址:https://www.cnblogs.com/xysmlx/p/3536557.html