题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163

题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di (1≤ i <n)。然后是 m 个人从他的出发点到他想去的终点(都是车站),约定轮到处理第 i 个人的时候,车站的出发点满足:((i-1) mod n) + 1 ,假设车每走一个单位的距离花费一个单位的时间,车开始的时候是从左到右开的。如果车走到最右的车站即车站 n 会调头,此时方向为从右到左开;如果车走到最左即车站 1,方向转为从左到右开。求出每个人从他出发点到终点需要的最少时间是多少。注意,这个人的出发点不一定和车的出发点相同,此时他要候车,候车时间也计算在内。

赛中过于浮躁,来不及写完......

  赛后各种 wa,各种wa.......

思路是对的,就是分六种情况,通过画图,写出公式

1. s≤x<y,      2. x<s<y,     3. x<y≤s,      4. s≤y<x,      5. y<s<x,       6. y<x≤s

通过公式的化简,发现有些情况是可以合并的。当 x < y,只要分成两种情况即可(st 即 s ): s <= x(ans = sum[y]-sum[st];) 和 x < s < y, s > y(ans = 2*sum[n] + sum[y] - sum[st];);  当 x > y,公式只有一条(前提是先交换 x , y 的值,保证x, y的大小,方便sum[]的处理):ans = 2*sum[n] - sum[x] - sum[st];

(sum[y]: 从第一个站到第 y 个站的距离,这个是求解时的优化,如果一个一个距离加,会TLE!还有就是由于求解的时候数据可能很大,要用 long long )

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef __int64 ll;
const int maxn = 1e5 + ;
ll d, sum[maxn]; int main()
{
int n, m, t;
while (scanf("%d", &t) != EOF) {
while (t--) {
scanf("%d%d", &n, &m);
memset(sum, , sizeof(sum));
for (int i = ; i < n; i++) {
scanf("%lld", &d);
sum[i+] = d + sum[i];
}
int x, y;
for (int i = ; i <= m; i++) {
scanf("%d%d", &x, &y);
int st = (i-)%n + ;
ll ans = ;
if (x < y) { // 左--> 右
if (st <= x) // s<=x<y
ans = sum[y]-sum[st];
else
ans = *sum[n] + sum[y] - sum[st]; // x<s<y, x<y<s
}
// 右--> 左
else { // x<y, 题目说x!=y
swap(x, y);
ans = *sum[n] - sum[x] - sum[st]; // s<y<x, y<s<x, y<x<s
}
printf("%I64d\n", ans);
}
}
}
return ;
}

原始版(比较详细而且很长,未合并公式前),原来没有覆盖所有情况呀,不过已经改好了, 痛苦改 bug 路 。呜呜呜呜呜呜~~~~! __ !

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; typedef __int64 ll;
const int maxn = 1e5 + ;
ll d, sum[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int n, m, t;
while (scanf("%d", &t) != EOF) {
while (t--) {
scanf("%d%d", &n, &m);
memset(sum, , sizeof(sum));
for (int i = ; i < n; i++) {
scanf("%I64d", &d);
sum[i+] = d + sum[i];
}
int st, end;
for (int i = ; i <= m; i++) {
scanf("%d%d", &st, &end);
int beg = (i-)%n + ; ll ans = ;
if (st < end) {
if (beg <= st) // s <= x < y
ans = sum[end]-sum[beg];
else if (beg > st && beg < end) // x < s < y
ans = *(sum[n]-sum[end]) + (sum[end]-sum[beg]) + *sum[end];
else if (beg >= end) // x < y < s
ans = *(sum[n]-sum[beg]) + (sum[beg]-sum[end]) + *sum[end];
}
else if (st > end) {
swap(st, end);
if (beg <= st) { // s <= y < x
ans = *(sum[n]-sum[st]) + sum[st]-sum[beg];
}
else if (beg > st && beg < end) { // y < s < x
ans = *(sum[n]-sum[beg]) + sum[beg]-sum[st];
}
else if (beg >= end) { // y < x <= s
ans = *(sum[n]-sum[beg]) + sum[beg]-sum[st];
}
}
printf("%I64d\n", ans);
}
}
}
return ;
}

BestCoder27 1002.Taking Bus(hdu 5163) 解题报告的更多相关文章

  1. BestCoder18 1002.Math Problem(hdu 5105) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...

  2. BestCoder12 1002.Help him(hdu 5059) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...

  3. BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...

  4. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  5. BestCoder17 1002.Select(hdu 5101) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

  6. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  7. BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...

  8. BestCoder20 1002.lines (hdu 5124) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

  9. BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...

随机推荐

  1. vijos1740 聪明的质监员 (二分、区间求和)

    http://www.rqnoj.cn/problem/657 https://www.vijos.org/p/1740 P1740聪明的质检员 请登录后递交 标签:NOIP提高组2011[显示标签] ...

  2. 分词工具ICTCLAS5.0使用心得

    接触自然语言处理有一年多了,最基本的一些自然是分词,词性标注,命名实体识别之类的知识,有些应用知道原理是一回事,自己动手做起来又是另外一回事了.最近又开始重操旧业:分词.分词最著名的自然就是中科院的分 ...

  3. VTK初学一,a Mesh from vtkImageData—球冠

    #ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...

  4. Runas命令:能让域用户/普通User用户以管理员身份运行指定程序。

    注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 在某些情况下,为了安全起见,大部分公司都会使用域控制器或只会给员工电脑user的用户权限,这样做能大大提高安全性和可控性,但由此也带 ...

  5. codevs4096 删数问题

    题目描述 Description 键盘输入一个高精度的正整数N,去掉其中任意S个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对给定的N 和S,寻找一种方案使得剩下的数字组成的新数最小. 输入 ...

  6. WCF基础知识

    根据微软官方的解释,WCF(之前的版本名为“Indigo”)是使用托管代码建立和运行面向服务(Service Oriented)应用程序的统一框架.它使得开发者能够建立一个跨平台的安全.可信赖.事务性 ...

  7. Thank you for your resubmission. Performance - 2.3.10 We noticed that your app or its metadata includes irrelevant third-party platform information. Specifically, Android logo is mentioned in the

    被拒很多次,各种修改,最后发现是提交的时候,含有安卓的图标!欲哭无泪呀! Thank you for your resubmission. Performance - 2.3.10 We notice ...

  8. 第六天 做的app不会改变什么

    app包括资源和功能,做完之后没有改变什么

  9. github 上传至远程的过程

    参考网址:http://luolei.org/dotfiles-tutorial/ http://www.ruanyifeng.com/blog/2014/06/git_remote.html     ...

  10. python 环境安装

    wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz  tar zxf Python-2.7.3.tgz  cd Python-2. ...