250pt:
给一个操作序列,每次向当前方向走a[i]步,然后90度转a[i]次,问做T次该操作序列之后的位置与起始位置的manhattan距离。
首先肯定做完4次操作序列后肯定能还原方向,那么模拟做完剩下的就行了。
1 #include <vector>
2 #include <list>
3 #include <map>
4 #include <set>
5 #include <queue>
6 #include <deque>
7 #include <stack>
8 #include <bitset>
9 #include <algorithm>
10 #include <functional>
11 #include <numeric>
12 #include <utility>
13 #include <sstream>
14 #include <iostream>
15 #include <iomanip>
16 #include <cstdio>
17 #include <cmath>
18 #include <cstdlib>
19 #include <ctime>
20
21 using namespace std;
22
23
24 class RobotHerb {
25 public:
26 long long getdist(int T, vector <int> a) {
27 long long x=0,y=0,dir=0;
28 for (int c=0;c<4;c++)
29 for (int b=0;b<(int)a.size();b++)
30 {
31 if (dir==0) y+=a[b];
32 else
33 {
34 if (dir==1) x+=a[b];
35 else
36 {
37 if (dir==2) y-=a[b];
38 else x-=a[b];
39 }
40 }
41 dir+=a[b];
42 dir%=4;
43 }
44 long long ans=(abs(x)+abs(y))*(T/4);
45 x=0,y=0,dir=0;
46 for (int c=0;c<T%4;c++)
47 for (int b=0;b<(int)a.size();b++)
48 {
49 if (dir==0) y+=a[b];
50 else
51 {
52 if (dir==1) x+=a[b];
53 else
54 {
55 if (dir==2) y-=a[b];
56 else x-=a[b];
57 }
58 }
59 dir+=a[b];
60 dir%=4;
61 }
62 return ans+abs(x)+abs(y);
63 }
64 };
65
66
67 //<%:testing-code%>
68 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
69 // BEGIN KAWIGIEDIT TESTING
70 // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
71 bool KawigiEdit_RunTest(int testNum, int p0, vector <int> p1, bool hasAnswer, long long p2) {
72 cout << "Test " << testNum << ": [" << p0 << "," << "{";
73 for (int i = 0; int(p1.size()) > i; ++i) {
74 if (i > 0) {
75 cout << ",";
76 }
77 cout << p1[i];
78 }
79 cout << "}";
80 cout << "]" << endl;
81 RobotHerb *obj;
82 long long answer;
83 obj = new RobotHerb();
84 clock_t startTime = clock();
85 answer = obj->getdist(p0, p1);
86 clock_t endTime = clock();
87 delete obj;
88 bool res;
89 res = true;
90 cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
91 if (hasAnswer) {
92 cout << "Desired answer:" << endl;
93 cout << " " << p2 << endl;
94 }
95 cout << "Your answer:" << endl;
96 cout << " " << answer << endl;
97 if (hasAnswer) {
98 res = answer == p2;
99 }
100 if (!res) {
101 cout << "DOESN'T MATCH!!!!" << endl;
102 } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
103 cout << "FAIL the timeout" << endl;
104 res = false;
105 } else if (hasAnswer) {
106 cout << "Match :-)" << endl;
107 } else {
108 cout << "OK, but is it right?" << endl;
109 }
110 cout << "" << endl;
111 return res;
112 }
113 int main() {
114 bool all_right;
115 all_right = true;
116
117 int p0;
118 vector <int> p1;
119 long long p2;
120
121 {
122 // ----- test 0 -----
123 p0 = 1;
124 int t1[] = {1,2,3};
125 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
126 p2 = 2ll;
127 all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
128 // ------------------
129 }
130
131 {
132 // ----- test 1 -----
133 p0 = 100;
134 int t1[] = {1};
135 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
136 p2 = 0ll;
137 all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
138 // ------------------
139 }
140
141 {
142 // ----- test 2 -----
143 p0 = 5;
144 int t1[] = {1,1,2};
145 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
146 p2 = 10ll;
147 all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
148 // ------------------
149 }
150
151 {
152 // ----- test 3 -----
153 p0 = 1000000000;
154 int t1[] = {100};
155 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
156 p2 = 100000000000ll;
157 all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
158 // ------------------
159 }
160
161 {
162 // ----- test 4 -----
163 p0 = 570;
164 int t1[] = {2013,2,13,314,271,1414,1732};
165 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
166 p2 = 4112ll;
167 all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
168 // ------------------
169 }
170
171 if (all_right) {
172 cout << "You're a stud (at least on the example cases)!" << endl;
173 } else {
174 cout << "Some of the test cases had errors." << endl;
175 }
176 return 0;
177 }
178 // END KAWIGIEDIT TESTING
550pt:
题意大概是N个结点的树每个结点会随机被分给某一方,考虑其中一方的结点,你需要加一些线使得它们形成一个新的联通块,当某个结点加入第二条边的时候会花费1的代价,问期望的最小代价。
这题挺神……一开始想出n^7的……觉得能过但写不出来……后来变成n^5的……结果写出来不知道为什么变成n^3了……
首先两方是对称的,所以只用算出一方的代价然后乘2就行了。接下来假设当前方有n个结点,m个联通块,那么当前方的代价为max(2*m-n-2,0),这个感觉挺显然的。所以我们可以dp当前有几个结点几个联通块,但我们发现后面两个信息可以整合,然后每个结点再做一次背包就完了。我也不知道为什么写出来就成n^3的了,n^5的已经记不到了。
1 #include <vector>
2 #include <list>
3 #include <map>
4 #include <set>
5 #include <queue>
6 #include <deque>
7 #include <stack>
8 #include <bitset>
9 #include <algorithm>
10 #include <functional>
11 #include <numeric>
12 #include <utility>
13 #include <sstream>
14 #include <iostream>
15 #include <iomanip>
16 #include <cstdio>
17 #include <cmath>
18 #include <cstdlib>
19 #include <ctime>
20 #include <cstring>
21
22 using namespace std;
23
24 const int maxn=40;
25 const int d=37;
26
27 int en;
28
29 long long f[maxn][maxn<<1][2];
30
31 bool solve[maxn][2];
32
33 long long g[maxn][maxn<<1];
34
35 struct edge
36 {
37 int e;
38 edge *next;
39 }*v[maxn],ed[maxn<<1];
40
41 void add_edge(int s,int e)
42 {
43 en++;
44 ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;
45 }
46
47 void dfs(int now,int pre,int precol)
48 {
49 if (solve[now][precol]) return;
50 solve[now][precol]=true;
51 for (int a=0;a<2;a++)
52 {
53 for (edge *e=v[now];e;e=e->next)
54 if (e->e!=pre) dfs(e->e,now,a);
55 memset(g,0,sizeof(g));
56 if (a) g[0][d]=1;
57 else
58 {
59 if (a==precol) g[0][-1+d]=1;
60 else g[0][1+d]=1;
61 }
62 int cnt=0;
63 for (edge *e=v[now];e;e=e->next)
64 if (e->e!=pre)
65 {
66 for (int b=0;b<=d+d;b++)
67 if (f[e->e][b][a])
68 for (int c=0;c<=d+d;c++)
69 if (g[cnt][c]) g[cnt+1][c+b-d]+=g[cnt][c]*f[e->e][b][a];
70 cnt++;
71 }
72 for (int b=0;b<=d+d;b++)
73 f[now][b][precol]+=g[cnt][b];
74 }
75 }
76
77 class CentaurCompany {
78 public:
79 double getvalue(vector <int> a, vector <int> b) {
80 en=0;
81 memset(v,0,sizeof(v));
82 int n=a.size();
83 for (int c=0;c<n;c++)
84 add_edge(a[c],b[c]),add_edge(b[c],a[c]);
85 memset(solve,false,sizeof(solve));
86 memset(f,0,sizeof(f));
87 dfs(1,0,1);
88 double ans=0;
89 for (int a=0;a<=d+d;a++)
90 ans+=(double)max(a-d-2,0)*f[1][a][1];
91 ans*=2.0;
92 n++;
93 ans/=(1ll<<n);
94 return ans;
95 }
96 };
97
98
99 //<%:testing-code%>
100 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
101 // BEGIN KAWIGIEDIT TESTING
102 // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
103 bool KawigiEdit_RunTest(int testNum, vector <int> p0, vector <int> p1, bool hasAnswer, double p2) {
104 cout << "Test " << testNum << ": [" << "{";
105 for (int i = 0; int(p0.size()) > i; ++i) {
106 if (i > 0) {
107 cout << ",";
108 }
109 cout << p0[i];
110 }
111 cout << "}" << "," << "{";
112 for (int i = 0; int(p1.size()) > i; ++i) {
113 if (i > 0) {
114 cout << ",";
115 }
116 cout << p1[i];
117 }
118 cout << "}";
119 cout << "]" << endl;
120 CentaurCompany *obj;
121 double answer;
122 obj = new CentaurCompany();
123 clock_t startTime = clock();
124 answer = obj->getvalue(p0, p1);
125 clock_t endTime = clock();
126 delete obj;
127 bool res;
128 res = true;
129 cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
130 if (hasAnswer) {
131 cout << "Desired answer:" << endl;
132 cout << " " << p2 << endl;
133 }
134 cout << "Your answer:" << endl;
135 cout << " " << answer << endl;
136 if (hasAnswer) {
137 res = fabs(p2 - answer) <= 1e-9 * max(1.0, fabs(p2));
138 }
139 if (!res) {
140 cout << "DOESN'T MATCH!!!!" << endl;
141 } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
142 cout << "FAIL the timeout" << endl;
143 res = false;
144 } else if (hasAnswer) {
145 cout << "Match :-)" << endl;
146 } else {
147 cout << "OK, but is it right?" << endl;
148 }
149 cout << "" << endl;
150 return res;
151 }
152 int main() {
153 bool all_right;
154 all_right = true;
155
156 vector <int> p0;
157 vector <int> p1;
158 double p2;
159
160 {
161 // ----- test 0 -----
162 int t0[] = {1};
163 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
164 int t1[] = {2};
165 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
166 p2 = 0.0;
167 all_right = KawigiEdit_RunTest(0, p0, p1, true, p2) && all_right;
168 // ------------------
169 }
170
171 {
172 // ----- test 1 -----
173 int t0[] = {1,1,1};
174 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
175 int t1[] = {2,3,4};
176 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
177 p2 = 0.125;
178 all_right = KawigiEdit_RunTest(1, p0, p1, true, p2) && all_right;
179 // ------------------
180 }
181
182 {
183 // ----- test 2 -----
184 int t0[] = {1,2,3,2,2};
185 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
186 int t1[] = {2,3,4,5,6};
187 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
188 p2 = 0.375;
189 all_right = KawigiEdit_RunTest(2, p0, p1, true, p2) && all_right;
190 // ------------------
191 }
192
193 {
194 // ----- test 3 -----
195 int t0[] = {1,2,3,4,5,6,7,8,9};
196 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
197 int t1[] = {2,3,4,5,6,7,8,9,10};
198 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
199 p2 = 0.41796875;
200 all_right = KawigiEdit_RunTest(3, p0, p1, true, p2) && all_right;
201 // ------------------
202 }
203
204 {
205 // ----- test 4 -----
206 int t0[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
207 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
208 int t1[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36};
209 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
210 p2 = 15.500000001076842;
211 all_right = KawigiEdit_RunTest(4, p0, p1, true, p2) && all_right;
212 // ------------------
213 }
214
215 {
216 // ----- test 5 -----
217 int t0[] = {10,7,2,5,6,2,4,9,7};
218 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
219 int t1[] = {8,10,10,4,1,6,2,2,3};
220 p1.assign(t1, t1 + sizeof(t1) / sizeof(t1[0]));
221 p2 = 0.646484375;
222 all_right = KawigiEdit_RunTest(5, p0, p1, true, p2) && all_right;
223 // ------------------
224 }
225
226 if (all_right) {
227 cout << "You're a stud (at least on the example cases)!" << endl;
228 } else {
229 cout << "Some of the test cases had errors." << endl;
230 }
231 return 0;
232 }
233 // END KAWIGIEDIT TESTING
900pt:
给你一张n*m的矩阵,然后你划线,要把整个矩阵分成多个环,要求某些特殊点相邻的环点必须也是特殊点,否则就会有1的代价,问最小代价。
这题比上题简单多了……每个点出度入度为1,根据出入度限制跑费用流就行了。
1 #include <vector>
2 #include <list>
3 #include <map>
4 #include <set>
5 #include <queue>
6 #include <deque>
7 #include <stack>
8 #include <bitset>
9 #include <algorithm>
10 #include <functional>
11 #include <numeric>
12 #include <utility>
13 #include <sstream>
14 #include <iostream>
15 #include <iomanip>
16 #include <cstdio>
17 #include <cmath>
18 #include <cstdlib>
19 #include <ctime>
20 #include <cstring>
21
22 using namespace std;
23
24 #define get(a,b) (a*m+b)
25
26 const int maxn=26*26*3;
27 const int maxm=maxn*20;
28 const int INF=0x3f3f3f3f;
29
30 int s,t,en,cost,q[maxn],dist[maxn],f[maxn],bx[5],by[5];
31
32 bool use[maxn];
33
34 struct edge
35 {
36 int e,f,d;
37 edge *next,*op;
38 }*v[maxn],ed[maxm],*fe[maxn];
39
40 void add_edge(int s,int e,int f,int d)
41 {
42 en++;
43 ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;v[s]->f=f;v[s]->d=d;
44 en++;
45 ed[en].next=v[e];v[e]=ed+en;v[e]->e=s;v[e]->f=0;v[e]->d=-d;
46 v[s]->op=v[e];v[e]->op=v[s];
47 }
48
49 bool spfa()
50 {
51 int front=1,tail=2;
52 q[1]=s;
53 use[s]=true;
54 memset(dist,0x3f,sizeof(dist));
55 dist[s]=0;
56 for (;front!=tail;)
57 {
58 int now=q[front++];
59 if (front==maxn) front=0;
60 use[now]=false;
61 for (edge *e=v[now];e;e=e->next)
62 if (e->f && dist[e->e]>dist[now]+e->d)
63 {
64 dist[e->e]=dist[now]+e->d;
65 f[e->e]=now;
66 fe[e->e]=e;
67 if (!use[e->e])
68 {
69 q[tail++]=e->e;
70 if (tail==maxn) tail=0;
71 use[e->e]=true;
72 }
73 }
74 }
75 return dist[t]!=INF;
76 }
77
78 int agument()
79 {
80 int delta=INF;
81 for (int p=t;p!=s;p=f[p])
82 delta=min(delta,fe[p]->f);
83 for (int p=t;p!=s;p=f[p])
84 {
85 fe[p]->f-=delta;
86 fe[p]->op->f+=delta;
87 }
88 cost+=delta*dist[t];
89 return delta;
90 }
91
92 int flow()
93 {
94 int ans=0;
95 while (spfa())
96 ans+=agument();
97 return ans;
98 }
99
100 class CurvyonRails {
101 public:
102 int getmin(vector <string> field) {
103 bx[1]=1;bx[2]=-1;by[3]=1;by[4]=-1;
104 int n=field.size();
105 int m=field[0].size();
106 en=0;
107 memset(v,0,sizeof(v));
108 int sum=0,cnt=0;
109 s=n*m*3;t=s+1;
110 for (int a=0;a<n;a++)
111 for (int b=0;b<m;b++)
112 if (field[a][b]!='w')
113 {
114 if (!((a+b)&1))
115 {
116 cnt++;
117 sum+=2;
118 add_edge(s,get(a,b),2,0);
119 add_edge(get(a,b),get(a,b)+n*m,1,0);add_edge(get(a,b),get(a,b)+2*n*m,1,0);
120 add_edge(get(a,b),get(a,b)+n*m,1,field[a][b]=='C');add_edge(get(a,b),get(a,b)+2*n*m,1,field[a][b]=='C');
121 for (int c=1;c<=4;c++)
122 {
123 int x=a+bx[c];
124 int y=b+by[c];
125 if (x>=0 && x<n && y>=0 && y<m && field[x][y]!='w')
126 {
127 if (x==a) add_edge(get(a,b)+n*m,get(x,y)+n*m,1,0);
128 else add_edge(get(a,b)+2*n*m,get(x,y)+2*n*m,1,0);
129 }
130 }
131 }
132 else
133 {
134 cnt--;
135 add_edge(get(a,b),t,2,0);
136 add_edge(get(a,b)+n*m,get(a,b),1,0);add_edge(get(a,b)+n*m*2,get(a,b),1,0);
137 add_edge(get(a,b)+n*m,get(a,b),1,field[a][b]=='C');add_edge(get(a,b)+n*m*2,get(a,b),1,field[a][b]=='C');
138 }
139 }
140 if (cnt!=0) return -1;
141 cost=0;
142 if (flow()==sum) return cost;
143 else return -1;
144 }
145 };
146
147
148 //<%:testing-code%>
149 //Powered by KawigiEdit 2.1.4 (beta) modified by pivanof!
150 // BEGIN KAWIGIEDIT TESTING
151 // Generated by KawigiEdit 2.1.4 (beta) modified by pivanof
152 bool KawigiEdit_RunTest(int testNum, vector <string> p0, bool hasAnswer, int p1) {
153 cout << "Test " << testNum << ": [" << "{";
154 for (int i = 0; int(p0.size()) > i; ++i) {
155 if (i > 0) {
156 cout << ",";
157 }
158 cout << """ << p0[i] << """;
159 }
160 cout << "}";
161 cout << "]" << endl;
162 CurvyonRails *obj;
163 int answer;
164 obj = new CurvyonRails();
165 clock_t startTime = clock();
166 answer = obj->getmin(p0);
167 clock_t endTime = clock();
168 delete obj;
169 bool res;
170 res = true;
171 cout << "Time: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl;
172 if (hasAnswer) {
173 cout << "Desired answer:" << endl;
174 cout << " " << p1 << endl;
175 }
176 cout << "Your answer:" << endl;
177 cout << " " << answer << endl;
178 if (hasAnswer) {
179 res = answer == p1;
180 }
181 if (!res) {
182 cout << "DOESN'T MATCH!!!!" << endl;
183 } else if (double(endTime - startTime) / CLOCKS_PER_SEC >= 2) {
184 cout << "FAIL the timeout" << endl;
185 res = false;
186 } else if (hasAnswer) {
187 cout << "Match :-)" << endl;
188 } else {
189 cout << "OK, but is it right?" << endl;
190 }
191 cout << "" << endl;
192 return res;
193 }
194 int main() {
195 bool all_right;
196 all_right = true;
197
198 vector <string> p0;
199 int p1;
200
201 {
202 // ----- test 0 -----
203 string t0[] = {"..",".."};
204 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
205 p1 = 0;
206 all_right = KawigiEdit_RunTest(0, p0, true, p1) && all_right;
207 // ------------------
208 }
209
210 {
211 // ----- test 1 -----
212 string t0[] = {"wCCww","wCC..","..w..","....w","ww..w"};
213 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
214 p1 = 0;
215 all_right = KawigiEdit_RunTest(1, p0, true, p1) && all_right;
216 // ------------------
217 }
218
219 {
220 // ----- test 2 -----
221 string t0[] = {"C.w","...",".C."};
222 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
223 p1 = 1;
224 all_right = KawigiEdit_RunTest(2, p0, true, p1) && all_right;
225 // ------------------
226 }
227
228 {
229 // ----- test 3 -----
230 string t0[] = {"."};
231 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
232 p1 = -1;
233 all_right = KawigiEdit_RunTest(3, p0, true, p1) && all_right;
234 // ------------------
235 }
236
237 {
238 // ----- test 4 -----
239 string t0[] = {"w"};
240 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
241 p1 = 0;
242 all_right = KawigiEdit_RunTest(4, p0, true, p1) && all_right;
243 // ------------------
244 }
245
246 {
247 // ----- test 5 -----
248 string t0[] = {"CC..CCCw.CwC..CC.w.C","C.CCCwCCC.w.w..C.w..","wwww...CC.wC.Cw.CC..","CC..CC.w..w.C..CCCC.","CC.CCC..CwwCCC.wCC..","w.C..wwCC.CC.wwwCC..",".CC.CC..CCC..CC.CC.C","Cw....C.C.CCC...CC..","CC.C..Cww.C.CwwwC..w","wCCww..C...CCCCCCC.w","C.CCw.CC.ww...C.CCww","C.C.C.CCwCC..wCCw.Cw","CCC.C...w..C.wC.wCCw","CC.C..C..CCC.CC.C...","C.ww...CCC..CC...CCC","...CCC.CwwwC..www.C.","wwCCCCC.w.C.C...wCwC","CCwC.CwCCC.C.w.Cw...","C.w.wC.CC.CCC.C.w.Cw","CCw.CCC..C..CC.CwCCw","C.wwwww.CwwCCwwwwwww"};
249 p0.assign(t0, t0 + sizeof(t0) / sizeof(t0[0]));
250 p1 = 9;
251 all_right = KawigiEdit_RunTest(5, p0, true, p1) && all_right;
252 // ------------------
253 }
254
255 if (all_right) {
256 cout << "You're a stud (at least on the example cases)!" << endl;
257 } else {
258 cout << "Some of the test cases had errors." << endl;
259 }
260 return 0;
261 }
262 // END KAWIGIEDIT TESTING