我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html

题目传送门:https://www.luogu.org/problemnew/show/P1561

\(Jhonson\)裸题。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=25005;

int n,ans;

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

struct cow {
	int a,b;

	bool operator<(const cow &tmp)const {
		return min(a,tmp.b)<=min(tmp.a,b);
	}
}p[maxn];

int main() {
	n=read();
	for(int i=1;i<=n;i++)
		p[i].a=read(),p[i].b=read();
	sort(p+1,p+n+1);int T=0;
	for(int i=1;i<=n;i++) {
		ans+=p[i].a;
		T=p[i].b+max(T-p[i].a,0);
	}
	printf("%d\n",ans+T);
	return 0;
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-29
  • 2021-08-18
  • 2021-06-20
  • 2021-07-20
  • 2021-12-11
猜你喜欢
  • 2022-12-23
  • 2021-12-29
  • 2021-05-31
  • 2022-03-11
  • 2021-05-17
  • 2022-01-15
  • 2022-01-23
相关资源
相似解决方案