Bayan 2015 Contest Warm Up

http://codeforces.com/contest/475

A - Bayan Bus

B - Strongly Connected City

C - Kamal-ol-molk's Painting

A. Bayan Bus

题意:输入人数k,输出一辆公交车!优先坐最后,同一排优先坐左边。

题解:暴力找地方坐啊!

  1. //#pragma comment(linker, "/STACK:102400000,102400000")
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<stack>
  11. #include<queue>
  12. using namespace std;
  13. #define ll long long
  14. #define usll unsigned ll
  15. #define mz(array) memset(array, 0, sizeof(array))
  16. #define mf1(array) memset(array, -1, sizeof(array))
  17. #define minf(array) memset(array, 0x3f, sizeof(array))
  18. #define REP(i,n) for(i=0;i<(n);i++)
  19. #define FOR(i,x,n) for(i=(x);i<=(n);i++)
  20. #define RD(x) scanf("%d",&x)
  21. #define RD2(x,y) scanf("%d%d",&x,&y)
  22. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  23. #define WN(x) printf("%d\n",x);
  24. #define RE freopen("D.in","r",stdin)
  25. #define WE freopen("huzhi.txt","w",stdout)
  26. #define mp make_pair
  27. #define pb push_back
  28. #define pf push_front
  29. #define ppf pop_front
  30. #define ppb pop_back
  31. const double pi=acos(-1.0);
  32. const double eps=1e-;
  33.  
  34. string s[];
  35. int k;
  36.  
  37. int main(){
  38. int i,j;
  39. s[]="+------------------------+";
  40. s[]="|#.#.#.#.#.#.#.#.#.#.#.|D|)";
  41. s[]="|#.#.#.#.#.#.#.#.#.#.#.|.|";
  42. s[]="|#.......................|";
  43. s[]="|#.#.#.#.#.#.#.#.#.#.#.|.|)";
  44. s[]="+------------------------+";
  45. int len=s[].length();
  46. RD(k);
  47. while(k--){
  48. bool flag=;
  49. FOR(i,,len-){
  50. FOR(j,,){
  51. if(s[j][i]=='#'){
  52. flag=;
  53. s[j][i]='O';
  54. break;
  55. }
  56. }
  57. if(flag)break;
  58. }
  59. }
  60. FOR(i,,){
  61. cout<<s[i]<<endl;
  62. }
  63. return ;
  64. }

B. Strongly Connected City

题意:城市由一堆单行线组成,给出一堆单行线的方向,判断是否每个交点能到达每个交点。

题解:从每个点出发暴力dfs啊!每个点出发最多每个点走一次,O(n^2)!

  1. //#pragma comment(linker, "/STACK:102400000,102400000")
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<stack>
  11. #include<queue>
  12. using namespace std;
  13. #define ll long long
  14. #define usll unsigned ll
  15. #define mz(array) memset(array, 0, sizeof(array))
  16. #define mf1(array) memset(array, -1, sizeof(array))
  17. #define minf(array) memset(array, 0x3f, sizeof(array))
  18. #define REP(i,n) for(i=0;i<(n);i++)
  19. #define FOR(i,x,n) for(i=(x);i<=(n);i++)
  20. #define RD(x) scanf("%d",&x)
  21. #define RD2(x,y) scanf("%d%d",&x,&y)
  22. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  23. #define WN(x) printf("%d\n",x);
  24. #define RE freopen("D.in","r",stdin)
  25. #define WE freopen("huzhi.txt","w",stdout)
  26. #define mp make_pair
  27. #define pb push_back
  28. #define pf push_front
  29. #define ppf pop_front
  30. #define ppb pop_back
  31. const double pi=acos(-1.0);
  32. const double eps=1e-;
  33.  
  34. string s[];
  35. int n,m;
  36.  
  37. bool f[][];
  38. int cnt;
  39.  
  40. void dfs(int x,int y){
  41. if(x< || x>=n || y< || y>=m || f[x][y])return;
  42. //printf("(%d,%d) ",x,y);
  43. f[x][y]=;
  44. cnt++;
  45. if(s[][x]=='>')dfs(x,y+);
  46. else dfs(x,y-);
  47. if(s[][y]=='v')dfs(x+,y);
  48. else dfs(x-,y);
  49. }
  50.  
  51. bool farm(){
  52. int i,j;
  53. REP(i,n){
  54. REP(j,m){
  55. mz(f);
  56. cnt=;
  57. dfs(i,j);
  58. if(cnt!=m*n)return ;
  59. }
  60. }
  61. return ;
  62. }
  63.  
  64. int main(){
  65. int i,j;
  66. RD2(n,m);
  67. cin>>s[]>>s[];
  68. //cout<<s[0]<<'!'<<s[1]<<'!';
  69. if(farm())puts("YES");
  70. else puts("NO");
  71. return ;
  72. }

C. Kamal-ol-molk's Painting

题意:给出一个图,其中“X”是刷的,“.”是不能刷的。刷子是个矩形,刷的时候要从某个地方下笔,只能往右或者往下移动。问是否能用刷子刷出这个图,不能则输出-1,能则输出最小的刷子的面积。

题解:先找到最左上的X,当做起点。然后找两个轨迹:

优先往右其次往下,可以得到刷子右上角的轨迹。

优先往下其次往右,能得到刷子左下角的轨迹。

但这些轨迹的初始和结尾部分不是轨迹,真正是轨迹的只有中间部分。从哪里开始是轨迹,由刷子的形状决定。

有两种情况,一种是右上角的轨迹开始向下走的那一格,当做右上角的初始位置,左下角的初始位置暂定为起点,然后我们就开始两个轨迹同步动。

若两个轨迹的移动方向不同,则说明左下角的初始位置选得不对,将左下角轨迹的下标++,直到两种轨迹相同。(若++使得刷子面积变小,则说明到结尾处了,则不++,使其结束)

最后时刻,得到的左下角坐标和右上角坐标,就真正决定了刷子形状,检查一下轨迹中是否都符合这个形状,符合而且刷的格子数等于X的数量的话,就得到一种解。

上面说的这种是先找右上角起始位置,另一种是先找左下角的起始位置,为左下角的轨迹开始往右走的那一格。而右上角的初始位置暂定为起点。与上面说的类似,弄着弄着就得到另一种解。

上面这两种情况也可能只有一种有解或者都没解。

最后有解的话输出面积小的那个面积,没解则输出-1。

(总体来说特别难搞,我是结束之后再交才A的……)

代码:

  1. //#pragma comment(linker, "/STACK:102400000,102400000")
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<stack>
  11. #include<queue>
  12. using namespace std;
  13. #define ll long long
  14. #define usll unsigned ll
  15. #define mz(array) memset(array, 0, sizeof(array))
  16. #define mf1(array) memset(array, -1, sizeof(array))
  17. #define minf(array) memset(array, 0x3f, sizeof(array))
  18. #define REP(i,n) for(i=0;i<(n);i++)
  19. #define FOR(i,x,n) for(i=(x);i<=(n);i++)
  20. #define RD(x) scanf("%d",&x)
  21. #define RD2(x,y) scanf("%d%d",&x,&y)
  22. #define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
  23. #define WN(x) printf("%d\n",x);
  24. #define RE freopen("D.in","r",stdin)
  25. #define WE freopen("huzhi.txt","w",stdout)
  26. #define mp make_pair
  27. #define pb push_back
  28. #define pf push_front
  29. #define ppf pop_front
  30. #define ppb pop_back
  31. const double pi=acos(-1.0);
  32. const double eps=1e-;
  33.  
  34. pair<int,int> operator -(pair<int,int>x,pair<int,int>y) {
  35. return mp(x.first-y.first,x.second-y.second);
  36. }
  37.  
  38. int n,m;
  39. char a[][];
  40.  
  41. int cnt;
  42. bool b[][];
  43.  
  44. inline bool in(const int &x,const int &y) {
  45. return x>= && x<n && y>= && y<m;
  46. }
  47.  
  48. inline bool can(const int &x,const int &y) {
  49. return (in(x,y)&&a[x][y]=='X');
  50. }
  51.  
  52. inline int mj(pair<int,int>x , pair<int,int>y) {
  53. return (abs(x.first-y.first)+) * (abs(x.second-y.second)+);
  54. }
  55.  
  56. vector<pair<int,int> > v[];///右上角,左下角,右上角,左下角
  57. int sz[];
  58.  
  59. int farm() {
  60. if(cnt==)return ;
  61. int i,j;
  62. // REP(i,n){
  63. // REP(j,m){
  64. // printf("%c",a[i][j]);
  65. // }
  66. // puts("");
  67. // }
  68. REP(i,n) {
  69. REP(j,m) {
  70. if(a[i][j]=='X')break;
  71. }
  72. if(a[i][j]=='X')break;
  73. }
  74. int sx=i,sy=j;
  75. int x=sx,y=sy;
  76. //printf("%d,%d\n",sx,sy);
  77. v[].clear();
  78. while() {
  79. v[].pb(mp(x,y));
  80. int c1=can(x+,y),c2=can(x,y+),c3=can(x+,y+);
  81. //printf("(%d,%d) %d,%d,%d %c\n",x,y,c1,c2,c3,a[x+1][y]);
  82. if(c1&&c2&&!c3)return -;
  83. if(!c1 && !c2)break;
  84. if(c2)y++;
  85. else if(c1)x++;
  86. }
  87. x=sx;
  88. y=sy;
  89. v[].clear();
  90. while() {
  91. //printf("[%d,%d]\n",x,y);
  92. v[].pb(mp(x,y));
  93. int c1=can(x+,y),c2=can(x,y+),c3=can(x+,y+);
  94. if(c1&&c2&&!c3)return -;
  95. if(!c1 && !c2)break;
  96. if(c1)x++;
  97. else if(c2)y++;
  98. }
  99. if(v[].back()!=v[].back())return -;
  100. int ex=v[].back().first,ey=v[].back().second;
  101.  
  102. x=ex;
  103. y=ey;
  104. v[].clear();
  105. while() {
  106. //printf("[%d,%d]\n",x,y);
  107. v[].pb(mp(x,y));
  108. int c1=can(x-,y),c2=can(x,y-),c3=can(x-,y-);
  109. if(c1&&c2&&!c3)return -;
  110. if(!c1 && !c2)break;
  111. if(c1)x--;
  112. else if(c2)y--;
  113. }
  114.  
  115. x=ex;
  116. y=ey;
  117. v[].clear();
  118. while() {
  119. //printf("[%d,%d]\n",x,y);
  120. v[].pb(mp(x,y));
  121. int c1=can(x-,y),c2=can(x,y-),c3=can(x-,y-);
  122. if(c1&&c2&&!c3)return -;
  123. if(!c1 && !c2)break;
  124. if(c2)y--;
  125. else if(c1)x--;
  126. }
  127. //printf("WOW!\n");
  128. if(v[].size()!=v[].size() || v[].size()!=v[].size())return -;
  129. REP(i,)sz[i]=v[i].size();
  130. int t=sz[]-;
  131. REP(i,sz[]) {
  132. if(v[][i]!=v[][t-i])return -;
  133. }
  134. t=sz[]-;
  135. REP(i,sz[]) {
  136. if(v[][i]!=v[][t-i])return -;
  137. }
  138.  
  139. //printf("WA!");
  140.  
  141. t=;
  142. while(t<sz[]) {
  143. if(t+<sz[] && v[][t+].first!=v[][t].first)break;
  144. t++;
  145. }
  146. int st=t;
  147. int t2=;
  148. pair<int,int>cha=v[][t]-v[][t2];
  149. while(t<sz[] && t2<sz[] && v[][t]-v[][t2] == cha) {
  150. while(t2<sz[] && t<sz[]) {
  151. if(t2+<sz[] && t+<sz[] && (v[][t2+]-v[][t2]!=v[][t+]-v[][t])
  152. && mj(v[][t],v[][t2+]) > mj(v[][t],v[][t2]))
  153. t2++;
  154. else break;
  155. }
  156. cha=v[][t]-v[][t2];
  157. //printf("%d,%d %d,%d\n",v[0][t].first,v[0][t].second,v[1][t2].first,v[1][t2].second);
  158. t++;
  159. t2++;
  160. }
  161.  
  162. mz(b);
  163. bool fail=;
  164. int cnt2=;
  165. t--,t2--;
  166. while(t>=st) {
  167. if(cha!=v[][t]-v[][t2]){
  168. //printf("cha!%d,%d %d,%d\n",v[0][t].first,v[0][t].second,v[1][t2].first,v[1][t2].second);
  169. fail=;break;}
  170. int rx=v[][t2].first , lx=v[][t].first;
  171. int ry=v[][t].second , ly=v[][t2].second;
  172. FOR(i,lx,rx) {
  173. FOR(j,ly,ry) {
  174. if(a[i][j]!='X') {
  175. fail=;
  176. break;
  177. }
  178. if(b[i][j])continue;
  179. b[i][j]=;
  180. cnt2++;
  181. }
  182. if(fail)break;
  183. }
  184. if(fail)break;
  185. t--;
  186. t2--;
  187. }
  188. vector<int>re;
  189. //printf("%d %d %d\n",fail,cnt2,cnt);
  190. if(!fail && cnt2==cnt) {
  191. //printf("!1\n");
  192. re.pb((abs(cha.first)+)*(abs(cha.second)+));
  193. }
  194. ///上面那段复制过来改的!要错很可能是这里错
  195. t=;
  196. while(t<sz[]) {
  197. if(t+<sz[] && v[][t+].second!=v[][t].second)break;
  198. t++;
  199. }
  200. st=t;
  201. t2=;
  202. cha=v[][t2]-v[][t];
  203. while(t<sz[] && t2<sz[] && v[][t2]-v[][t] == cha) {
  204. while(t<sz[] && t2<sz[]) {
  205. if(t2+<sz[] && t+<sz[] && (v[][t+]-v[][t]!=v[][t2+]-v[][t2])
  206. && mj(v[][t2+],v[][t]) > mj(v[][t2],v[][t]))
  207. t2++;
  208. else break;
  209. }
  210. //printf("[%d,%d] [%d,%d]\n",v[0][t2].first,v[0][t2].second,v[1][t].first,v[1][t].second);
  211. cha=v[][t2]-v[][t];
  212. t++;
  213. t2++;
  214. }
  215. mz(b);
  216. cnt2=;
  217. fail=;
  218. t--,t2--;
  219. while(t>=st){
  220. if(v[][t2]-v[][t] != cha){fail=;break;}
  221. int rx=v[][t].first , lx=v[][t2].first;
  222. int ry=v[][t2].second , ly=v[][t].second;
  223. FOR(i,lx,rx) {
  224. FOR(j,ly,ry) {
  225. if(a[i][j]!='X') {
  226. fail=;
  227. break;
  228. }
  229. if(b[i][j])continue;
  230. b[i][j]=;
  231. cnt2++;
  232. }
  233. if(fail)break;
  234. }
  235. if(fail)break;
  236. t--;
  237. t2--;
  238. }
  239.  
  240. if(!fail && cnt2==cnt) {
  241. // printf("!2\n");
  242. re.pb((abs(cha.first)+)*(abs(cha.second)+));
  243. }
  244. sort(re.begin(),re.end());
  245. if(re.size()!=)return re[];
  246. else return -;
  247. }
  248.  
  249. int main() {
  250. int i,j;
  251. cnt=;
  252. RD2(n,m);
  253. REP(i,n) {
  254. REP(j,m) {
  255. scanf(" %c",&a[i][j]);
  256. if(a[i][j]=='X')cnt++;
  257. }
  258. }
  259. printf("%d\n",farm());
  260. return ;
  261. }

codeforces CF475 ABC 题解的更多相关文章

  1. Codeforces Round #556 题解

    Codeforces Round #556 题解 Div.2 A Stock Arbitraging 傻逼题 Div.2 B Tiling Challenge 傻逼题 Div.1 A Prefix S ...

  2. Codeforces Round #569 题解

    Codeforces Round #569 题解 CF1179A Valeriy and Deque 有一个双端队列,每次取队首两个值,将较小值移动到队尾,较大值位置不变.多组询问求第\(m\)次操作 ...

  3. Codeforces Round #557 题解【更完了】

    Codeforces Round #557 题解 掉分快乐 CF1161A Hide and Seek Alice和Bob在玩捉♂迷♂藏,有\(n\)个格子,Bob会检查\(k\)次,第\(i\)次检 ...

  4. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  5. Codeforces Round #312 (Div. 2) ABC题解

    [比赛链接]click here~~ A. Lala Land and Apple Trees: [题意]: AMR住在拉拉土地. 拉拉土地是一个很漂亮的国家,位于坐标线.拉拉土地是与著名的苹果树越来 ...

  6. Codeforces 1323 div2题解ABC

    A. Even Subset Sum Problem 签到题 #include <bits/stdc++.h> using namespace std; template <type ...

  7. Codeforces Round #542 题解

    Codeforces Round #542 abstract I决策中的独立性, II联通块染色板子 IIIVoronoi diagram O(N^2 logN) VI环上距离分类讨论加取模,最值中的 ...

  8. Codeforces Choosing Laptop 题解

    这题实在是太水了,具体看注释 蒟蒻的方法是一边找过时的电脑一边比大小 蒟蒻不才,只会C++ 其实还会free basic,但它已经过时了 附: 本题洛谷网址 Codeforces网址 希望蒟蒻的题解能 ...

  9. Codeforces 380 简要题解

    ABC见上一篇. 感觉这场比赛很有数学气息. D: 显然必须要贴着之前的人坐下. 首先考虑没有限制的方案数.就是2n - 1(我们把1固定,其他的都只有两种方案,放完后长度为n) 我们发现对于一个限制 ...

随机推荐

  1. golang学习之旅:官方文档汇总

    The Go Programming Language Specification:http://localhost:8080/ref/spec学习Constants.Variables.Types. ...

  2. linux下进程权限分析

    转自http://blog.chinaunix.net/uid-27105712-id-3349522.html 在linux下,关于文件权限,大部分人接触比较多,也比较熟悉了解.但是对进程权限一般知 ...

  3. 【BZOJ-3876】支线剧情 有上下界的网络流(有下界有源有汇最小费用最大流)

    3876: [Ahoi2014]支线剧情 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 821  Solved: 502[Submit][Status ...

  4. 代理IP收集

    做系统攻击和防守时都有用! http://www.xicidaili.com/ http://www.data5u.com/ http://ip.zdaye.com/ http://www.youda ...

  5. SqlServerException:拒绝对表对象的select,insert权限解决(新建账号导致的问题)

    继上一篇文章所述的问题,这次又出现了不能插入的问题.经过定位,也是由于我多选择了一个数据库用户角色的权限导致的,下面是详细的操作步骤 SqlServerException:拒绝了对对象 '...'(数 ...

  6. Git: 一些基本命令

    1.快速获取远程项目 1) git clone xxx.git // 如:git clone git://git.kernel.org/pub/scm/git/git.git 2) git clone ...

  7. AngularJs angular.element

    angular.element 将DOM元素或者HTML字符串一包装成一个jQuery元素. 格式:angular.element(element); element:包装成jquery对象的html ...

  8. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  9. Android中的Binder机制的简要理解

    转载自:http://www.linuxidc.com/Linux/2012-07/66195.htm http://blog.csdn.net/sunxingzhesunjinbiao/articl ...

  10. Linux 中 17 个 tar 命令实用示例

    Tar(Tape ARchive,磁带归档的缩写,LCTT 译注:最初设计用于将文件打包到磁带上,现在我们大都使用它来实现备份某个分区或者某些重要的目录)是类 Unix 系统中使用最广泛的命令,用于归 ...