ybtoj:贪心算法

A:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,a,b;
priority_queue <int> q;
int sum=0;

int main()
{
	scanf("%d%d%d",&n,&a,&b);
	
	//cin>>n>>a>>b;
	for(int i=1;i<=n;i++)
	{
		int x;
		
		cin>>x;
		q.push(x);
		
	}
	
	while(q.top()-sum*a>0)
	{
		sum++;
		int x=q.top();
		x-=b;
		q.pop();
		
		q.push(x);
		
		
	}
	cout<<sum<<endl;
	return 0;
} 

B:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1003;
int x[N],y[N],n,d,ans;

struct part{
	double l,r;
	
}a[N];

bool operator<(part a,part b){
	return a.r<b.r;
}

inline void work(){
	for(int i=1;i<=n;i++){
		cin>>x[i]>>y[i];
	}
	for(int i=1;i<=n;i++){
		if(d<y[i]){
			ans=-1;
			return;
		}
		a[i].l=x[i]-sqrt(1ll*d*d-1ll*y[i]*y[i]);
		a[i].r=x[i]+sqrt(1ll*d*d-1ll*y[i]*y[i]);
	}
	sort(a+1,a+1+n);
	ans=1;
	double pos=a[1].r;
	for(int i=2;i<=n;i++){
		if(a[i].l<=pos && pos<=a[i].r) continue;
		else ans++,pos=a[i].r;
	}
}
int main(){


		cin>>n>>d;

		if(n==d && d==0){
			return 0;
		}
		work();
		printf("%d\n",ans);
	return 0;
}

C:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=50005;
struct cow{
	int l,r;
	int id;
	bool operator<(const cow &a) const{
		return l<a.l;
	}
}a[N];

int ans[N];
struct Node{
	int r;
	int id;
	Node(int a,int b):r(a),id(b){}
	bool operator<(const Node &a) const{
		return a.r<r;
	}
	
};
int n;

int main(){
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i].l>>a[i].r,a[i].id=i;
	sort(a+1,a+n+1);
	priority_queue<Node> q;
	for(int i=1;i<=n;i++){
		if(!q.size() || q.top().r>=a[i].l){
			ans[a[i].id]=q.size()+1;
			q.push((Node){a[i].r,q.size()+1});
			
		}else{
			ans[a[i].id]=q.top().id;
			q.pop();
			q.push((Node){a[i].r,ans[a[i].id]});
			
		}

	}
	cout<<q.size()<<"\n";
	for(int i=1;i<=n;i++){
		cout<<ans[i]<<"\n";
	}
	return 0;
}

D:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct node{
	ll w,h;
	node(){w=0,h=0;}
	node(ll w,ll h):w(w),h(h){}
	bool operator <(const node &a) const {return a.w==w ? h>a.h : w>a.w;}
	
};
ll ans;
priority_queue<node> q;

int main(){
	ll n,k;
	ans=0;
	scanf("%lld%lld",&n,&k);
	for(int i=1;i<=n;i++){
		ll w;
		scanf("%lld",&w);
		q.push(node(w,1));
		
	}
	while((q.size()-1)%(k-1)!=0) q.push(node(0,1));
	while(q.size()>=k){
		ll h=-1;ll w=0;
		for(int i=1;i<=k;i++){
			node t=q.top();
			q.pop();
			h=max(h,t.h);
			w+=t.w;
		}
		ans+=w;
		q.push(node(w,h+1));
	}
	printf("%lld\n%lld\n",ans,q.top().h-1);
	
	return 0;
}

E:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
struct w{
	int n,v;
}ww[1003];
bool cmp(w a,w b)
{
	return a.v<b.v;
	
}
int main()
{
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		cin>>ww[i].v;
		ww[i].n=i;
		
	}
	
	sort(ww+1,ww+n+1,cmp);
//	for(int i=1;i<=n;i++)
//	{
//		cout<<ww[i].n<<endl;
//		
//	}
	double t=0;
	for(int i=1;i<=n;i++)
	{
		cout<<ww[i].n<<" ";
		t+=(ww[i].v)*(n-i);
		//cout<<t<<"    ";
	}
	printf("\n%.2lf",1.0*t/n);
	
	return 0;
}

F:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n,sum=0;
struct tr{
	int hei;
	int seat;
	
}tree[100005];
bool cmp(tr a,tr b){
	return a.seat<b.seat;
	
}
int main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>tree[i].seat>>tree[i].hei;
		
	}
	sort(tree+1,tree+n+1,cmp);
	for(int i=1;i<=n;i++){
		if(i==1) sum+=max(0,tree[i].hei-(tree[i+1].seat-tree[i].seat));
		else if(i==n) sum+=max(0,max(0,tree[i].hei-(tree[i].seat-tree[i-1].seat)));
		
		else sum+=max(0,max(tree[i].hei-(tree[i].seat-tree[i-1].seat),tree[i].hei-(tree[i+1].seat-tree[i].seat)));
		
	}
	cout<<sum<<endl;
	return 0;
}

G:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n;
const int N=1e6+1;
int a[N];
int b[N];
int x=0;
int sta[N];

int main(){
	
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	b[n]=a[n];
	for(int i=n-1;i>=1;i--){
		b[i]=a[i];
		b[i]=max(b[i+1],b[i]);
	}
//	for(int i=1;i<=n;i++){
//		cout<<b[i]<<" ";
//	}
	int wh=0;
	for(int i=1;i<=n;i++){
		sta[++wh]=a[i];
		while(sta[wh]>b[i+1]){
			printf("%d ",sta[wh--]);
		}
	}
	while(wh) printf("%d ",sta[wh--]);
	
	

	return 0;
}

H:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
	int f=1,lzx=0;
	char c=getchar();
	while(c>'9' || c<'0'){
		if(c=='-'){
			f=-f;
		}
		c=getchar();
	}	
	//c=getchar();
	while(c<='9' && c>='0'){
		lzx=lzx*10+c-'0';
		c=getchar();
	}
	return lzx*f;
	
}
const int N=1e6+10;
int a[N],sum[N],flag[N];

int main(){//
	int n=read(),k=read(),ans1=0,ans2;
	//cout<<n;
	for(int i=1;i<=n;i++){
		a[i]=read();
		ans1 |= a[i];
	}
	for(int i=30;i>=0;i--){
		int op=0;
		for(int j=1;j<=n;j++){
			sum[j]=0;
		}
		for(int j=1;j<=n;j++){
			if((a[j] & (1<<i))==0){
				sum[max(j-k+1,1)]++;
				sum[j+1]--;
			}
		}
		for(int j=2;j<=n;j++){
			sum[j]+=sum[j-1];
		}
		for(int j=1;j+k-1<=n;j++){
			if(flag[j]) continue ;
			if(!sum[j]) op++ ;
		}
		for(int j=1;j+k-1<=n;j++){
			if(flag[j]) continue ;
			if(op && sum[j]) flag[j]=1;
		}
	}
	
	for(int i=1;i+k-1<=n;i++){
		if(!flag[i]){
			ans2=a[i];
			for(int j=i+1;j<=i+k-1;j++){
				ans2 &= a[j];
			}
			break;
		}
	}
	
	cout<<ans1<<" "<<ans2<<"\n";
	return 0;
}

I:

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+4;
vector<int> g[N];
int n,m,ans,q;
bool bo[N];
struct point{
	int l,r;
	bool operator<(const point &x) const{
		return r<x.r;
	}
	
}a[N];
int x,y,i,j,k;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	while(m--){
		cin>>x>>y;
		if(x>y) swap(x,y);
		if(y==x+1) bo[x]=1;
		g[x].push_back(y);
	}
	for(i=1;i<n;i=j+1){
		if(!bo[i]){
			j=i;
			continue;
		}
		j=i;
		while(bo[j+1] && j<n){
			j++;
		}
		int cnt=j-i+1;
		q=0;
		for(k=i;k<=j;k++){
			int len=g[k].size();
			for(int h=0;h<len;h++){
				int v=g[k][h];
				if(v<=j+1 && v!=k+1) a[++q]=(point){k,v};
			}
		}
		sort(a+1,a+q+1);
		int r=0;
		for(k=1;k<=q;k++){
			if(a[k].l>=r){
				r=a[k].r;
				cnt++;
			}
			ans=max(ans,cnt);
		}
		
	}
	cout<<ans<<"\n";
	return 0;
}

//抄袭绝非良策,理解才是正道

posted @ 2024-11-14 21:07  cathyzro  阅读(36)  评论(0)    收藏  举报