题目链接: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. Git命令家底儿及Git数据通信原理详解

    http://geek.csdn.net/news/detail/72807?utm_source=tuicool&utm_medium=referral

  2. LINUX下搭建VPN

    一.准备 需要 dkms-2.0.17.5-1.noarch.rpm.ppp-2.4.5-33.0.rhel6.x86_64.rpm.pptpd-1.4.0-1.el6.x86_64.rpm,并依次安 ...

  3. 清北暑假模拟day1 生活

    /* 数字三角形,要求第K大的值,可以推知,如果得知k的范围,那么一定是在上一行可转移状态的对应范围内(反证法可以证明),这个在背包九讲里也有提及 */ #include<cstdio> ...

  4. 【C语言入门教程】4.8 指针数组

    指针数组是一种特殊的数组,这类数组存放的全部是同一数据类型的内存地址.指针数组的定义形式为: 数据类型 *数组名[长度]; 例如: const char *c[4] = { "China&q ...

  5. xcode7 NSAppTransportSecurity

    在Info.plist中添加  NSAppTransportSecurity 类型  Dictionary Dictionary 下添加  NSAllowsArbitraryLoads 类型 Bool ...

  6. 45个实用的JavaScript技巧、窍门和最佳实践

    在这篇文章中,我将分享一组JavaScript的技巧.窍门和最佳实践,这些都是JavaScript程序员应该知晓的,不管他们是使用在浏览器/引擎上,还是服务器端(SSJS——Service Side ...

  7. ubutu之Navicat安装

    1.下载navicat111_premium_cs.tar.gz压缩包 2.进入压缩包所在目录,执行一下命令解压 tar -zxvf navicat111_premium_cs.r.gz 3.进入解压 ...

  8. ii7安装php

    http://www.jb51.net/article/22372.htm 我们知道php配置有几种: 1.CGI方式加载PHP环境,通常就是IIS里面配置解释器为php.exe,早期比较常见,目前使 ...

  9. 最简单的Android教程之自定义控件

    新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...

  10. 单链表的类的c++实现

    #include<iostream> using namespace std;template <class T>struct linkNode{ T data; linkNo ...