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,第二长递增子序列是 ...
随机推荐
- SQL Server2005主从复制实现
转自:http://blog.csdn.net/gaojier1000/article/details/5805814 一. 准备工作:1 .在发布服务器上建立一个共享目录,作为发布快照文件的 ...
- java 读取pdf、word、Excel文件
用到的jar: itextpdf-5.5.8.jar (PDF) poi.jar public class FileUtils { /** * 判断文件是否存在 * * @Title: isExc ...
- NHibernate概念
SessionFactory (NHibernate.ISessionFactory) 对属于单一数据库的编译过的映射文件的一个线程安全的,不可变的缓存快照.它是Session的工厂,是Connect ...
- Android在TextView中实现RichText风格
参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...
- C++ 错误总结
1.出现不完全的类型‘class CJdThread’的非法使用或前向声明 src/../include/ComCommon.h:37:27: 错误:对不完全的类型‘class CJdThread’的 ...
- linux查找某一进程并杀死
1. 查找redis进程 ps -ef|grep redis-server 2.打印第二个参数,因为上面第二列是进程号 3.这两个进程号有一个是grep进程号,所以要去掉,反选 grep ps ...
- springMVC之国际化
1.工程结构 2.jar包 3.配置文件spring-config.xml,springMVC配置文件 <?xml version="1.0" encoding=" ...
- centos systemctl指令
# systemctl #输出已激活单元 # systemctl list-units #输出已激活单元 # systemctl --failed #输出运行失败的单元 # systemctl lis ...
- vncserver和Ubuntu Xfce4远程桌面环境的配置,解决不显示图形界面
vncserver和Ubuntu Xfce4远程桌面环境的配置 参考的http://blog.163.com/thinki_cao/blog/static/8394487520130301453180 ...
- Java设计模式 之 代理模式
所谓的代理模式就是为其它类或对象提供一个代理以控制对这个对象的访问.那么常见的代理有远程代理,虚拟代理,保护代理,智能代理. 1. 远程代理:为一个不同地址空间的对象提供一个本地代理对象. 2. 虚拟 ...