Snatch Groceries

Problem Description

“SNATCH GROCERIES first, then get a covid test” has quickly become an anthem for the lockdown that started suddenly in Shanghai in the early hours of March 28th. We can describe scenes of panic buying—qiang cai, or snatching groceries—and the threat of being locked out of one’s home amid a frenzied bid to control an outbreak of covid-19 in China’s main business and finance hub. Here is the question, how does the server determine who succeeded When millions of people press the order button on their mobile phones at the same time.

Processing such a large number of requests requires a distributed system. In a distributed system, time is a tricky business, because communication is not instantaneous: it takes time for a message to travel across the network from one machine to another. The time when a message is received is always later than the time when it is sent, but due to variable delays in the network, we don’t know how much later. This fact sometimes makes it difficult to determine the order in which things happened when multiple machines are involved

Moreover, each machine on the network has its own clock, which is an actual hardware device: usually a quartz crystal oscillator. These devices are not perfectly accurate, so each machine has its own notion of time, which may be slightly faster or slower than on other machines. It is possible to synchronize clocks to some degree: the most commonly used mechanism is the Network Time Protocol (NTP), which allows the computer clock to be adjusted according to the time reported by a group of servers. The servers in turn get their time from a more accurate time source, such as a GPS receiver.

Modern computers have at least two different kinds of clocks: a time-of-day clock and a monotonic clock. A time-of-day clock does what you intuitively expect of a clock: it returns the current date and time according to some calendar (also known as wall-clock time). For example, clock_gettime(CLOCK_REALTIME) on Linux return the number of seconds (or milliseconds) since the epoch: midnight UTC on January 1, 1970, according to the Gregorian calendar, not counting leap seconds. Some systems use other dates as their reference point. Time-of-day clocks are usually synchronized with NTP,

You may be able to read a machine’s time-of-day clock with microsecond or even nanosecond resolution. But even if you can get such a fine-grained measurement, that doesn’t mean the value is actually accurate to such precision. In fact, it most likely is not—as mentioned previously, the drift in an imprecise quartz clock can easily be several milliseconds, even if you synchronize with an NTP server on the local network every minute. With an NTP server on the public internet, the best possible accuracy is probably to the tens of milliseconds, and the error may easily spike to over 100 ms when there is network congestion.

Thus, it doesn’t make sense to think of a clock reading as a point in time—it is more like a range of times, within a confidence interval: for example, a system may be 95% confident that the time now is between 10.3 and 10.5 seconds past the minute, but it doesn’t know any more precisely than that. If we only know the time +/– 100 ms, the microsecond digits in the timestamp are essentially meaningless.

An interesting exception is Google’s TrueTime API in Spanner, which explicitly reports the confidence interval on the local clock. When you ask it for the current time, you get back two values: [earliest, latest], which are the earliest possible and the latest possible timestamp. Based on its uncertainty calculations, the clock knows that the actual current time is somewhere within that interval. The width of the interval depends, among other things, on how long it has been since the local quartz clock was last synchronized with a more accurate clock source.

TL;DR: Spanner implements snapshot isolation across data centers in this way. It uses the clock’s confidence interval as reported by the TrueTime API, and is based on the following observation: if you have two confidence intervals, each consisting of an earliest and latest possible timestamp (A=[Aearliest,Alatest],B=[Bearliest,Blatest]), and those two intervals do not overlap (i.e.,Aearliest<Alatest<Bearliest<Blatest), then B definitely happened after A——there can be no doubt. Only if the intervals overlap are we unsure in which order A and B happened.

Now we use Spanner as a solution, there are millions of people snatching groceries, and everyone is given the clock’s confidence interval. The server executes each request in chronological order, and terminate in case of intervals overlap. Here is the question, how many people can get their food before the server is terminated.

Input

First line has one integer T, indicating there are T test cases. In each case:

First line has one integers n, indicating there are n people.

For next n lines, each line has 2 integers earliest,latest, indicates the clock’s confidence interval.

T≤10,1≤n≤10^5,0≤earliesti<latesti≤10^9

Output

In each case, print one integer, indicates the answer.

Sample Input

2
3
1 2
3 4
5 6
3
1 2
2 3
1 5

Sample Output

3
0

题解:题目很长,看完就会,很简单,时间没有重叠可以一直加,有重叠就停止,直接输出答案,重叠的不算。

认识:

vector的使用:

初始化:vector<int> t;

t.resize(n+1):将t现有元素个数调整至N+1个,多则删,少则补;

sort(t.begin(),t.end()):对t中的t.begin()(包括它)到t.end()(不包括它)的元素进行从小到大的排列;该代码中由于pair的使用,排列顺序按照t.first的大小,以方便接下来的比较。

signed main()的应用:

#define int long long 可以使用;

减少超时的可能性。

pair的一些用法:

功能:pair将一对值(t1和t2)组合成一个值,分别用pair的两个公有函数first和second访问;

初始化:pair<T1,T2> p1(创建一个空的pair对象,两个元素分别是T1、T2类型);

make_pair(v1,v2):以v1、v2两个值创建一个新的pair对象,其元素类型分别是v1、v2;

p1.first/second:返回对象p1中名为first/second的共有数据成员。

#include<bits/stdc++.h>
using namespace std;
signed main()
{
	int n,T;
	vector<pair<int,int>> t;
	cin>>T;
	while(T--)
	{
		cin>>n;
		t.resize(n+1);
		for(int i=0;i<n;i++)
		{
			cin>>t[i].first>>t[i].second;
		}
		t[n]=make_pair(1234567890,0);
		sort(t.begin(),t.end());
		int ans=0;
		for(int i=0;i<n;i++)
		{
			if(t[i].second>=t[i+1].first)
				break;
			++ans;
		}
		cout<<ans<<endl;
	}
	return 0;
}                                   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TherAndI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值