BestCoder27 1002.Taking Bus(hdu 5163) 解题报告
题目链接: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) 解题报告的更多相关文章
- 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,使得 ...
- BestCoder12 1002.Help him(hdu 5059) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...
- BestCoder3 1002 BestCoder Sequence(hdu 4908) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目意思:给出 一个从1~N 的排列你和指定这个排列中的一个中位数m,从这个排列中找出长度为奇数 ...
- hdu 1002.A + B Problem II 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...
- BestCoder17 1002.Select(hdu 5101) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...
- BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...
- 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,问是否在每个数 ...
- BestCoder20 1002.lines (hdu 5124) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
随机推荐
- Lnmp的安装、配置
一.首先在本地安装好虚拟机,在虚拟机上安装centos6.5,由于习惯问题,不喜欢直接在虚拟机上操作linux系统,习惯了ssh过去,直接用xshell操作,这完全是个人习惯问题: 1. 用xshe ...
- git checkout -b 的详细讲解
创建分支: $ git branch mybranch 切换分支: $ git checkout mybranch 创建并切换分支: $ git checkout -b mybranch 更新mast ...
- 关于IE7 默认有边框的解决方案
这个问题出现在IE7中,因为body有默认的border.这个原因是由于声明引起的. 加了这个头就可以了 * {border:0;} 以上的 CSS 在 XHTML 下是无效果的,将 DOCTYPE ...
- 什么是SEM?
SEM是Search Engine Marketing的英文缩写,其中文意思就是搜索引擎营销.台湾和香港.澳门也称为搜寻销售,意思都差不多.SEM更多强调的是综合手段在搜索引擎上的企业传播和促进和销售 ...
- 【Solr】copy字段的应用
目录 界面查询应用 熟悉Schema.xml copy域的应用 回到顶部 界面查询应用 添加一个文档 查询添加的文档 以上详细介绍了query里面的参数详解. 当不输入任何条件时,进行查询,看看返回结 ...
- Todd's Matlab讲义第3讲:牛顿法和for循环
方程数值求解 下面几讲,我们将聚集如下方程的解法: \begin{equation} f(x)=0 \tag{3.1}\label{3.1} \end{equation} 在微积分课程中,我们知道,许 ...
- 【PHP面向对象(OOP)编程入门教程】3.什么是面向对象编程呢?
就不说他的概念,如果你想建立一个电脑教室,首先要有一个房间, 房间里面要有N台电脑,有N个桌子, N个椅子, 白板, 投影机等等,这些是什么,刚才咱们说了, 这就是对象,能看到的一个个的实体,可以说这 ...
- maven之window安装
1.下载:apache-maven-3.3.9-bin.zip 2.解压下载的maven文件到任意指定文件夹 3.配置maven 右键“我的电脑” -> "属性" 在打开的属 ...
- IDEA之google style配置(IDEA)
一.window下IDEA配置谷歌编码规范xml 1.首先下载文件:intellij-java-google-style.xml(文件详细内容见附件) 2.找到该路径(C:\Users\自己的登录名 ...
- js搜索框输入提示(高效-ys8)
<style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...