题目链接: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. php 快速排序法

    function quicksort(array $arr = array()){ $len = count($arr); if ($len > 1) { $key = $arr[0]; $l_ ...

  2. 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?

    就不说他的概念,如果你想建立一个电脑教室,首先要有一个房间, 房间里面要有N台电脑,有N个桌子, N个椅子, 白板, 投影机等等,这些是什么,刚才咱们说了, 这就是对象,能看到的一个个的实体,可以说这 ...

  3. Linux 信号量大全

    编号 信号名称 缺省动作 说明 1 SIGHUP 终止 终止控制终端或进程 2 SIGINT 终止 键盘产生的中断(Ctrl-C) 3 SIGQUIT dump 键盘产生的退出 4 SIGILL du ...

  4. php导出csv数据在浏览器中输出提供下载或保存到文件的示例

    来源:http://www.jb51.net/article/49313.htm 1.在浏览器输出提供下载 /** * 导出数据到CSV文件 * @param array $data 数据 * @pa ...

  5. 关于viewport

    最近无聊的很,买了本教材,学习响应式网站设计. 因为有多年css的编程基础,前面的媒介查询学的很顺利.当学到viewport这个mata标签的时候,教程讲的比较简单. 今天,百度了不少资料,基本搞清楚 ...

  6. Unity手游之路<十>自动寻路Navmesh之跳跃,攀爬,斜坡

    http://blog.csdn.net/janeky/article/details/17598113 在之前的几篇Blog总,我们已经系统学习了自动寻路插件Navmesh的相关概念和细节.然而,如 ...

  7. OpenCV高斯模型

    int main(int argc, char** argv) { //std::string videoFile = "E:\\C_VC_code\\Text_Photo\\dingdan ...

  8. am335x 电容屏驱动添加。

    参考:http://www.cnblogs.com/helloworldtoyou/p/5530422.html 上面可以下载驱动. 解压后驱动有如下目录: 我们要选择的是: eGTouchARM/e ...

  9. BZOJ 1044: [HAOI2008]木棍分割

    Description 求 \(n\) 根木棍长度为 \(L\) ,分成 \(m\) 份,使最长长度最短,并求出方案数. Sol 二分+DP. 二分很简单啊,然后就是方案数的求法. 状态就是 \(f[ ...

  10. 项目:BluetoothChat

    代码在github: https://github.com/Viyu/BluetoothChat 蓝牙聊天核心是Android Demo里的,我加上了类似微信的界面. 我觉得这个应用要是能推广的话,有 ...