记录编号 24621 评测结果 AAAAAAAAAA
题目名称 拯救奶牛贝希 最终得分 100
用户昵称 Gravatar苏轼 是否通过 通过
代码语言 C++ 运行时间 0.040 s
提交时间 2011-04-12 21:12:51 内存使用 0.33 MiB
显示代码纯文本
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <algorithm>
  4. using namespace std;
  5. const int MAXM=10005;
  6.  
  7. pair<int,int> v[MAXM];
  8.  
  9. inline int caldis(int x1,int y1,int x2,int y2)
  10. {
  11. //x1<=x2
  12. //number from 0
  13. int nre;
  14. if (x1>x2)
  15. {
  16. swap(x1,x2);
  17. swap(y1,y2);
  18. }
  19. if (x1==x2)
  20. nre=abs(y1-y2);
  21. else if (y1/2==y2/2)
  22. nre=(x2-x1-1)*2+((y1&1)?2:1)+((y2&1)?0:1);
  23. else if (y1<y2)
  24. {
  25. nre=(y2/2-y1/2-1)*2+((y2&1)?2:1);
  26. int xx=x2-(y2/2-y1/2)+((y2&1)==0);
  27. if (xx<=x1)
  28. {
  29. if (y2&1)
  30. y2--,x2--,nre=1;
  31. else
  32. nre=0;
  33. int yy=(y2/2-(x2-x1))*2;
  34. nre+=yy-y1+(y2/2-yy/2)*2;
  35. }
  36. else
  37. nre+=(xx-x1)*2-((y1&1)==0);
  38. }
  39. else if (y1>y2)
  40. nre=(y1/2-y2/2-1)*2+((y2&1)?1:2)+(x2-x1)*2+(y1&1);
  41. return nre;
  42. }
  43.  
  44. int main()
  45. {
  46. freopen("rescuea.in","r",stdin);
  47. freopen("rescuea.out","w",stdout);
  48. int N,M;
  49. int nx,ny;
  50. scanf("%d%d",&N,&M);
  51. scanf("%d%d",&nx,&ny);
  52. nx--,ny--;
  53. for(int i=0;i<M;i++)
  54. {
  55. scanf("%d%d",&v[i].first,&v[i].second);
  56. v[i].first--;
  57. v[i].second--;
  58. }
  59. sort(v,v+M);
  60. int re=~0u>>1,rx,ry;
  61. for(int i=0;i<M;i++)
  62. {
  63. int x1=v[i].first,y1=v[i].second;
  64. int x2=nx,y2=ny;
  65. int nre=caldis(x1,y1,x2,y2);
  66. if (nre<re)
  67. re=nre,rx=v[i].first,ry=v[i].second;
  68. }
  69. printf("%d %d\n",rx+1,ry+1);
  70. printf("%d\n",re+1);
  71. return 0;
  72. }