Codeforces Round #707 (Div. 2, based on Moscow Open Olympiad in Informatics) A--C

传送门

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
inputCopy
2
2
2 4
10 12
0 2
5
1 4
7 8
9 10
13 15
19 20
1 2 3 4 5
outputCopy
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.

模拟

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e8;
const int N=2*1e6+5;//?????????? 4e8
int n,m;
int a[N],b[N],c[N];
void solve()
{
	int ans=0,cnt=0;
    cin>>n;
    for(int i=1;i<=n;i++)  scanf("%d%d",&a[i],&b[i]);
    for(int i=1;i<=n;i++)  scanf("%d",&c[i]);
    for(int i=1;i<=n;i++)
    {
    	ans=c[i]+ans+(a[i]-b[i-1]);
    	int flag=0;
    	if((b[i]-a[i])&1)  flag=1;
    	int ret=(b[i]-a[i])/2+flag;
    	if(i!=n)  ans=max(ans+ret,b[i]);
//    	cout<<ans<<endl;
	}
	cout<<ans<<endl;
}
signed main()
{
	int T;
	cin>>T;
    while(T--)  solve();
    return 0;
}

B. Napoleon Cake

This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can’t make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead.

To bake a Napoleon cake, one has to bake n dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps n times:

place a new cake layer on the top of the stack;
after the i-th layer is placed, pour ai units of cream on top of the stack.
When x units of cream are poured on the top of the stack, top x layers of the cake get drenched in the cream. If there are less than x layers, all layers get drenched and the rest of the cream is wasted. If x=0, no layer gets drenched.

The picture represents the first test case of the example.
Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don’t.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤20000). Description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of layers in the cake.

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤n) — the amount of cream poured on the cake after adding each layer.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, print a single line with n integers. The i-th of the integers should be equal to 1 if the i-th layer from the bottom gets drenched, and 0 otherwise.

Example
inputCopy
3
6
0 3 0 0 1 3
10
0 0 0 1 0 5 0 0 0 2
3
0 0 0
outputCopy
1 1 0 1 1 1
0 1 1 1 1 1 0 0 1 1
0 0 0

前缀和

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e8;
const int N=2*1e5+5;//?????????? 4e8
int n;
int a[N],sum[N];
void solve()
{
	cin>>n;
	for(int i=0;i<=n+1;i++)  a[i]=0;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(!x)  continue;
		a[i+1]--;
		a[max(1,i-x+1)]++;
	}
//	for(int i=1;i<=n;i++)  cout<<a[i]<<" ";
//	cout<<endl;
	for(int i=1;i<=n;i++)  a[i]+=a[i-1];
	for(int i=1;i<=n;i++)
		if(a[i])  printf("1 ");
		else  printf("0 ");
	puts("");
}
signed main()
{
	int T;
	cin>>T;
    while(T--)  solve();
    return 0;
}

C. Going Home

It was the third month of remote learning, Nastya got sick of staying at dormitory, so she decided to return to her hometown. In order to make her trip more entertaining, one of Nastya’s friend presented her an integer array a.

Several hours after starting her journey home Nastya remembered about the present. To entertain herself she decided to check, are there four different indices x,y,z,w such that ax+ay=az+aw.

Her train has already arrived the destination, but she still hasn’t found the answer. Can you help her unravel the mystery?

Input
The first line contains the single integer n (4≤n≤200000) — the size of the array.

The second line contains n integers a1,a2,…,an (1≤ai≤2.5⋅106).

Output
Print “YES” if there are such four indices, and “NO” otherwise.

If such indices exist, print these indices x, y, z and w (1≤x,y,z,w≤n).

If there are multiple answers, print any of them.

Examples
inputCopy
6
2 1 5 2 7 4
outputCopy
YES
2 3 1 6
inputCopy
5
1 3 1 9 20
outputCopy
NO
Note
In the first example a2+a3=1+5=2+4=a1+a6. Note that there are other answer, for example, 2 3 4 6.

In the second example, we can’t choose four indices. The answer 1 2 2 3 is wrong, because indices should be different, despite that a1+a2=1+3=3+1=a2+a3

这道题让我明白了我与大佬的差距。

其实两重循环可以过,但有一组hack数据

200000

1 1 1 1 1 1 1 1…………(全是一)

所以这样的代码就会陷入死循环,然后TLE

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
int n,flag;
int a[N];
struct node
{
    int first,second;
};
vector < node > v[M];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)  for(int j=i+1;j<=n;j++)
	{
		int t=a[i]+a[j];
		for(auto k:v[t])  if(k.first!=i&&k.first!=j&&k.second!=i&&k.second!=j)
		{
			puts("Yes");
			printf("%d %d %d %d\n",k.first,k.second,i,j);
			return;
		}
		v[t].push_back((node){i,j});
	}
	puts("No");
}
signed main()
{
	solve();
    return 0;
}

赛后看了看大佬们写的代码,用的一个极其巧妙地方法化解了这个问题。

请注意第二层循环的写法

#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-8
//#define mod 1e9+7
#define pi acos(-1.0)
using namespace std;
const int mod=1e9+7;
const int M=5e6+5;
const int N=4*1e5+5;//?????????? 4e8
int n,flag;
int a[N];
struct node
{
    int first,second;
};
vector < node > v[M];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)  for(int j=1;j<=n-i;j++)
	{
		int t=a[i+j]+a[j];
		for(auto k:v[t])  if(k.first!=i+j&&k.first!=j&&k.second!=i+j&&k.second!=j)
		{
			puts("Yes");
			printf("%d %d %d %d\n",k.first,k.second,i+j,j);
			return;
		}
		v[t].push_back((node){i+j,j});
	}
	puts("No");
}
signed main()
{
	solve();
    return 0;
}

刚才有人问我O(n2)问什么可以过,我默默的把所有的数据截了个图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值