A. Alexey and Train

Codeforces Round #707 A

A. Alexey and Train

Alexey is travelling on a train. Unfortunately, due to the bad weather, the train moves slower that it should!

Alexey took the train at the railroad terminal. Let’s say that the train starts from the terminal at the moment 0. Also, let’s say that the train will visit n stations numbered from 1 to n along its way, and that Alexey destination is the station n.

Alexey learned from the train schedule n integer pairs (ai,bi) where ai is the expected time of train’s arrival at the i-th station and bi is the expected time of departure.

Also, using all information he has, Alexey was able to calculate n integers tm1,tm2,…,tmn where tmi is the extra time the train need to travel from the station i−1 to the station i. Formally, the train needs exactly ai−bi−1+tmi time to travel from station i−1 to station i (if i=1 then b0 is the moment the train leave the terminal, and it’s equal to 0).

The train leaves the station i, if both conditions are met:

it’s on the station for at least ⌈bi−ai2⌉ units of time (division with ceiling);
current time ≥bi.
Since Alexey spent all his energy on prediction of time delays, help him to calculate the time of arrival at the station n.

Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains the single integer n (1≤n≤100) — the number of stations.

Next n lines contain two integers each: ai and bi (1≤ai<bi≤106). It’s guaranteed that bi<ai+1.

Next line contains n integers tm1,tm2,…,tmn (0≤tmi≤106).

Output
For each test case, print one integer — the time of Alexey’s arrival at the last station.

Example
input
2
2
2 4
10 12
0 2
5
1 4
7 8
9 10
13 15
19 20
1 2 3 4 5
output
12
32

Example
input
2
2
2 4
10 12
0 2
5
1 4
7 8
9 10
13 15
19 20
1 2 3 4 5
output
12
32

Note
In the first test case, Alexey arrives at station 1 without any delay at the moment a1=2 (since tm1=0). After that, he departs at moment b1=4. Finally, he arrives at station 2 with tm2=2 extra time, or at the moment 12.

In the second test case, Alexey arrives at the first station with tm1=1 extra time, or at moment 2. The train, from one side, should stay at the station at least ⌈b1−a12⌉=2 units of time and from the other side should depart not earlier than at moment b1=4. As a result, the trains departs right at the moment 4.

Using the same logic, we can figure out that the train arrives at the second station at the moment 9 and departs at the moment 10; at the third station: arrives at 14 and departs at 15; at the fourth: arrives at 22 and departs at 23. And, finally, arrives at the fifth station at 32.

link A

分析:
据题意得,到达每一站的时间推迟,并且等候时间 >= [(bi−ai)/ 2 ],推的再次发车时间,列车正常行驶的时间不变,输出到达终点站的时间

傻了吧唧的代码 =。=

#include <iostream>
 
using namespace std;
 
struct two
{
	int a;
	int b;
};
 
int f(int x,int y);
 
int main()
{
   int t;
   cin >> t;
   while(t --)
   {
   	int n;
   	two x[110],c[110];
   	int y[110];
   	cin >> n;
   	for(int i = 0;i < n;i ++)
   	{
   	scanf("%d %d",&x[i].a,&x[i].b);
    c[i] = x[i]; 
	}
   	for(int i = 0;i < n;i ++)
   	cin >> y[i];
   	
   	for(int i = 0;i < n ;i ++)
   	{
   		int kk = f(c[i].a,c[i].b); // 计算 ( [(bi−ai)/ 2 ] )
   	    x[i].a += y[i];   // 更新 a 
   		int ch = x[i].b - x[i].a;  // 等候时间
   		
   		
		if(ch < kk && i + 1 < n)  // 等候时间不符条件&&处理到终点站前
		{
			int temp = x[i + 1].a - x[i].b;   // 行驶时间
			x[i].b = x[i].a + kk;   // 更新 b
			x[i + 1].a = x[i].b + temp;   // 更新下一个 a 
		} 
		
	
   	}
   	 
   	 cout << x[n-1].a << endl;
 
   }
   return 0;
} 
 
int f(int x,int y)  // 计算 ( [(bi−ai)/ 2 ] )
{
	int ans;
	int sub;
    sub = y - x;
	if(sub % 2 == 0)
	ans = sub / 2;
	else ans = sub / 2 + 1;
	return ans;
}

机智的代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
 
int a[105],b[105];
 
int main() {
	int t,n,m,d; scanf("%d",&t);
	while (t--) {
		scanf("%d",&n); m=0;
		for (int i=1;i<=n;++i) 
		scanf("%d%d",&a[i],&b[i]);
		for (int i=1;i<=n;++i) {
			 scanf("%d",&d);
			 m+=a[i]-b[i-1]+d; // 行驶时间 + extra time  
			if (i<n) m=max(b[i],m+((b[i]-a[i]+1)/2)); //更新 b  
		}
		// b +(最后一次) 行驶时间 + extra time   == 到达终点站的时间
		printf("%d\n",m);
	}
	return 0;
}

!!!

(b[i]-a[i]+1)/2) : + 1 取上限 就学到了 学到了 !

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ASPack 2.12是由俄罗斯程序员Alexey Solodovnikov开发的一款压缩软件。该软件能够将常见的可执行程序、DLL库和驱动程序进行压缩,以减小文件大小。ASPack 2.12的压缩率非常高,同时不影响程序的运行速度和稳定性,因此得到了广泛的应用。该软件的使用方法也非常简单,只需将需要压缩的文件拖入软件窗口即可自动进行压缩。ASPack 2.12还支持多线程压缩和加密保护,可以为软件的发布提供更好的保护措施。在Alexey Solodovnikov的努力下,ASPack 2.12成为了一款备受好评的压缩软件。虽然现在已经有了更先进的压缩算法和软件,但ASPack 2.12在其开发时期仍为软件开发者提供了一个有效的压缩解决方案,为软件开发行业做出了贡献。 ### 回答2: ASPack 2.12 是一款功能强大的可执行文件压缩工具,由俄罗斯程序员Alexey Solodovnikov在2001年开发。其压缩技术采用了多种高级算法,可以将可执行文件的大小减小30%至70%不等,同时不影响程序的性能。ASPack 2.12 被广泛应用于软件开发和网络传输中,可以有效地减小文件大小,加快网络传输速度。在使用ASPack 2.12 进行压缩时,可以通过多种选项进行自定义,包括选择压缩率、对齐方式、加密选项等。此外,ASPack 2.12 还支持批量处理,可以一次性压缩多个文件并生成一个安装程序。Alexey Solodovnikov 在1998年开始从事计算机编程工作,目前是一名资深的计算机安全专家,在可执行文件压缩领域有着很高的声誉。ASPack 2.12 的开发是他在计算机安全领域的一次积极尝试,为软件开发和网络传输带来了极大的便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值