Codeforces Global Round 2

A

# include <bits/stdc++.h>
using namespace std;

pair<int,int> b[300100];
pair<int,int> e[300100];
int main()
{
	int n;
	int a;
	
	cin>>n;
	for(int i=1;i<=n;i++){
		b[i].first=0,e[i].first=0;
		b[i].second=i,e[i].second=i;
	}
	for(int i=1;i<=n;i++){
		cin>>a;
		if(b[a].first==0){
			b[a].first=i;
		}
		if(b[a].first!=0&&i>e[a].first){
			e[a].first=i;
		}
	} 
	
//	for(int i=1;i<=n;i++){
//		cout<<b[i].first<<" "<<b[i].second<<endl;
//		cout<<e[i].first<<" "<<e[i].second<<endl;
//	}
//	
	sort(b+1,b+n+1);
	sort(e+1,e+n+1,greater<pair<int,int> >());
//	cout<<endl;
//	for(int i=1;i<=n;i++){
//		cout<<b[i].first<<" "<<b[i].second<<endl;
//		//cout<<e[i].first<<" "<<e[i].second<<endl;
//	}
	for(int i=1;i<=n;i++){
		if(b[i].first!=0){
			if(b[i].second!=e[1].second){
			//	cout<<e[1].first<<" "<<b[i].first<<endl;
				cout<<e[1].first-b[i].first;
				break;
			}
		}
	}
	
	return 0;
}

B

#  include <bits/stdc++.h>
using namespace std;


int main()
{
	int n,h,d,flag=0;
	int sum=0;
	int a[1010];
	int b[1010];
	int f[1010];
	
	cin>>n>>h;
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	
	b[0]=a[0];
	h=h-a[0];
	d=0;
	sum++;
	for(int i=1;i<n;i++){
		for(int j=0;j<=d;j++){
			if(a[i]<=b[j]&&f[j]==0){
				sum++;
				f[j]=1;
				flag=1;
				break;
			}
		}
		if(flag==0){
			d++;
			if(h>a[i]){
				sum++;
				h=h-a[i];
				b[d]=a[i];
			}else{
				cout<<sum;
				break;
			}
		}
	}
	
	return 0;
}

posted @ 2022-02-27 00:13  fengzlj  阅读(20)  评论(0)    收藏  举报