相邻的两个才会形成弟弟配合 最多n-1个 ci>ai+1FF坐飞机easy版 贪心排序 我竟然不会
Input

输入描述:先给出两个正整数n和t,代表买了n张机票,所有飞机晚点时间和为t。然后接下来n行,每行给出两个时间描述一个飞机的起飞和降落时间。(1<n<=1e5,1<=t,a,b<=1e9)

Output

输出一行一个整数代表经过分配晚点时间后,最多的弟弟配合有多少组。
3 2
1 2
2 3
3 4

2
由输入得,总晚点时间为2,可以将晚点时间各分配给第一,第二航班各1,则实际起飞和降落时间分别为[1,3],[2,4],[3,4].则有两组弟弟配合。即第[1,2]个航班,和第[2,3]个航班.若将晚点时间全部分给第一个航班,则总共只有一组弟弟配合,即[1,2],因为1,3航班不相邻,不会形成弟弟配合(请仔细阅读定义)

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.precision(0);
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
const int maxn=1e5+5;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
ll pow_mod(ll a,ll b,ll mod){ll res=1;while(b!=0){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}
struct node
{
	ll s;
	ll e;
}a[maxn];
ll cha[maxn];
int main()
{
	IO;
	int n;
	ll t;
	cin>>n>>t;
	//怎么分 看都差多少之后再分 尽可能的多所以要排序 
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].s>>a[i].e;
	}
	for(int i=1;i<=n-1;i++)
	{
		cha[i]=a[i+1].s-a[i].e+1;//补差值+1才能超过下一个的起点 
	}
	sort(cha+1,cha+n);
	ll sum=0;
	int cnt=0;
	for(int i=1;i<=n-1;i++)//对差值 补时间 
	{
		sum+=cha[i];
		if(sum>t)//<=t 
			break;
		cnt++;
	}//n=3 t=2  
	//1 2   
	//2 3   
	//3 4 正常结束 
	cout<<cnt<<endl;
	return 0;
}

自造样例
n=3 t=2
1 3
4 5
7 8
+2 +3
1次
对差值的前缀和 本质一样

#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.precision(0);
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const ll inf=0x3f3f3f3f3f3f3f3f;
const int INF=0x3f3f3f3f; 
const ll mod=1e9+7;
const int maxn=2e5+5;
ll gcd(ll a,ll b){return b!=0?gcd(b,a%b):a;}
ll pow_mod(ll a,ll b,ll mod){ll res=1;while(b!=0){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}
struct node
{
	ll s;
	ll e;
}a[maxn];
ll cha[maxn],pre[maxn];
int main()
{
	IO;
	int n;
	ll t;
	cin>>n>>t;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].s>>a[i].e;
	}
	// ci>ai+1

	for(int i=1;i<=n-1;i++)
	{
		cha[i]=a[i+1].s-a[i].e+1;
	}
	sort(cha+1,cha+n);
	pre[0]=0;
	for(int i=1;i<=n-1;i++)
	{
		pre[i]=pre[i-1]+cha[i];
	}
	
	int cnt=0;
	for(int i=1;i<=n-1;i++)
	{
		if(pre[i]>t)
			break;
		cnt++;
	}
	cout<<cnt<<endl;
	
	return 0;
}
/*
	 n=3 t=2
	 1 2
	 2 3
	 3 4
	 n=3 t=2
	 1 2
	 3 4
	 5 8
	*/

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-29
  • 2021-07-17
  • 2021-08-13
  • 2021-11-21
猜你喜欢
  • 2021-08-19
  • 2022-02-11
  • 2021-12-09
  • 2022-12-23
  • 2021-06-10
  • 2021-05-30
相关资源
相似解决方案