zoukankan      html  css  js  c++  java
  • codeforces B. Marathon 解题报告

    题目链接:http://codeforces.com/problemset/problem/404/B

    题目意思:Valera 参加马拉松,马拉松的跑道是一个边长为a的正方形,要求Valera从起点(0,0)出发,每经过距离d就给他一杯drink。求出n个位置,即Valera每经过d距离的position。

          一开始我是直接模拟的,每次算出一个position就记录离该边最末还剩多少距离,假设为k,然后更新初始已有的距离k,又开始加a的距离直到到达下一个点...代码复杂之余,还超时了。

          比较好的作法是:既然要求position,那么这个position一定是经过 i*d(i  = 1 ~ n) 的距离的。那么问题关键就是定位!也就是坐标位置!这时需要知道Valera 经过多少条长度为a的边i*d/a (k)。接着就是确定具体是那条边了,可以通过对 k mod 4来求得。下边:0 ;右边:1; 上边:2; 左边:3。另外引入t,它表示经过id距离之后,与经过相应的a边的长度之差,即t = id - ka。

          至于具体的坐标点则根据这些边的不同而有所不同。由于一直是逆时针跑的,因此下边的x坐标是(t,0),右边是(a, t), 上边是(a-t, a)(求的时候是逆时针方向的,也就是该点离右边的距离,我们需要求的是离左边的距离),左边(0, a-t)(与上边同理)

           还有一个要注意的地方,就是要取 int64了,10^5 * 10^5 !

          

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     double a, d, t;
     9     __int64 k, n, i;
    10     while (scanf("%lf%lf%I64d", &a, &d, &n) != EOF)
    11     {
    12         double zero = 0.0;
    13         for (i = 1; i <= n; i++)
    14         {
    15             k = i * d / a;
    16             t = i * d - k * a;
    17         //    printf("k = %d, t = %lf
    ", k, t);
    18             if (k % 4 == 0)
    19                 printf("%lf %lf
    ", t, zero);
    20             else if (k % 4 == 1)
    21                 printf("%lf %lf
    ", a, t);
    22             else if (k % 4 == 2)
    23                 printf("%lf %lf
    ", a-t, a);
    24             else
    25                 printf("%lf %lf
    ", zero, a-t);
    26         }
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    织梦开发——相关阅读likeart应用
    织梦标签教程
    织梦专题调用代码
    HIT 2543 Stone IV
    POJ 3680 Intervals
    HIT 2739 The Chinese Postman Problem
    POJ 1273 Drainage Ditches
    POJ 2455 Secret Milking Machine
    SPOJ 371 Boxes
    HIT 2715 Matrix3
  • 原文地址:https://www.cnblogs.com/windysai/p/3617290.html
Copyright © 2011-2022 走看看