列表

详情


NC51112. Stars in Your Window

描述

Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting. 
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently. 
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert. 
Farewell, my princess! 
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together. 

Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed. 

输入描述

There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow, with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point. 

There are at least 1 and at most 10000 stars in the sky.

输出描述

For each test case, output the maximum brightness in a single line.

示例1

输入:

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

输出:

5
6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 20ms, 内存消耗: 2408K, 提交时间: 2020-04-25 21:34:19

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int u=20010;
struct rec{unsigned int x,l,r; int c;}a[u];
struct{int p,l,r,dat,add;}t[u*4];
unsigned int b[u],c[u],x,y,w,h;
int n,m,p,i,ans,z;

bool cmp(rec a,rec b)
{
	return a.x<b.x||a.x==b.x&&a.c<b.c;
}

void build(int p,int l,int r)
{
	t[p].l=l,t[p].r=r,t[p].dat=t[p].add=0;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
}

void spread(int p)
{
	if(t[p].add)
	{
		t[p*2].dat+=t[p].add,t[p*2].add+=t[p].add;
		t[p*2+1].dat+=t[p].add,t[p*2+1].add+=t[p].add;
		t[p].add=0;
	}
}

void change(int p,int l,int r,int c)
{
	if(l<=t[p].l&&r>=t[p].r)
	{
		t[p].dat+=c,t[p].add+=c;
		return;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) change(p*2,l,r,c);
	if(r>mid) change(p*2+1,l,r,c);
	t[p].dat=max(t[p*2].dat,t[p*2+1].dat);
}

int main()
{
	while(cin>>n>>w>>h)
	{
		for(m=p=0,i=1;i<=n;i++)
		{
			scanf("%u%u%d",&x,&y,&z);
			a[i].l=a[n+i].l=y,a[i].r=a[n+i].r=y+h-1;
			a[i].x=x,a[n+i].x=x+w,a[i].c=z,a[n+i].c=-z;
			b[++m]=y,b[++m]=y+h-1;
		}
		//离散化 
		sort(b+1,b+m+1);
		for(i=1;i<=m;i++)
			if(i==1||b[i]!=b[i-1]) c[++p]=b[i];
			
		for(n*=2,i=1;i<=n;i++)
		{
			a[i].l=lower_bound(c+1,c+p+1,a[i].l)-c;
			a[i].r=lower_bound(c+1,c+p+1,a[i].r)-c;
		}
		sort(a+1,a+n+1,cmp);
		build(1,1,p);
		for(ans=0,i=1;i<=n;i++)
		{
			change(1,a[i].l,a[i].r,a[i].c);
			ans=max(ans,t[1].dat);
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 26ms, 内存消耗: 3264K, 提交时间: 2022-08-30 09:42:43

#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,m,tot,cnt,a[N],f[N];
long long ans;
map<int,int>val;
struct xx{
	int l,r;
	long long v,bj;
}w[4*N];
struct pp{
	int x,l,r,v;
}h[N];
bool cmp(pp p,pp q)
{
	return p.x<q.x;
}
void build(int p,int l,int r)
{
	w[p].l=l;
	w[p].r=r;
	w[p].v=w[p].bj=0;
	if(l==r)	return ;
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
}
void down(int p)
{
	if(w[p].bj)
	{
		w[p*2].v+=w[p].bj;
		w[p*2+1].v+=w[p].bj;
		w[p*2].bj+=w[p].bj;
		w[p*2+1].bj+=w[p].bj;
		w[p].bj=0;
	}
}
void change(int p,int ll,int rr,int v)
{
	int l=w[p].l;
	int r=w[p].r;
	if(ll<=l&&r<=rr)
	{
		w[p].v+=v;
		w[p].bj+=v;
		return ;
	}
	down(p);
	int mid=(l+r)/2;
	if(ll<=mid)	change(p*2,ll,rr,v);
	if(mid<rr)	change(p*2+1,ll,rr,v);
	w[p].v=max(w[p*2].v,w[p*2+1].v);
}
long long ask(int p)
{
	return w[p].v;
}
int main()
{
	while(cin>>tot>>n>>m)
	{
		cnt=ans=0;
		for(int i=1;i<=tot;i++)	
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			a[++cnt]=y;
			h[cnt]={x,y,y+m-1,z};
			a[++cnt]=y+m-1;
			h[cnt]={x+n,y,y+m-1,-z};
		}
		sort(a+1,a+cnt+1);
		sort(h+1,h+cnt+1,cmp);
		val.clear();
		int v=0;
		for(int i=1;i<=cnt;i++)
		{
			if(i==1||a[i]!=a[i-1])
			{
				val[a[i]]=++v;
				f[v]=a[i];
			}
		}
		build(1,1,v);
		for(int i=1;i<=cnt;i++)
		{
			change(1,val[h[i].l],val[h[i].r],h[i].v);
			ans=max(ans,ask(1));
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++ 解法, 执行用时: 20ms, 内存消耗: 1660K, 提交时间: 2022-01-14 18:49:37

#include<bits/stdc++.h>
#define lb lower_bound
using namespace std;
long long xs[10000],ys[10000],X[20000],Y[20000];
int cs[10000],nv[80000],nm[80000],n,W,H;
pair<pair<int,int>,pair<int,int> > et[20000];
void ad(int fm,int to,int v,int i,int l,int r){
	if(fm<=l&&r<=to){nv[i]+=v;nm[i]+=v;return;}
	int m=(l+r)/2;
	if(fm<=m)ad(fm,to,v,i*2,l,m);
	if(m<to)ad(fm,to,v,i*2+1,m+1,r);
	nm[i]=max(nm[i*2],nm[i*2+1])+nv[i];
}
int main(){
	while(cin>>n>>W>>H){
		int ans=0;
		for(int i=0;i<n;i++)cin>>xs[i]>>ys[i]>>cs[i],xs[i]*=2,ys[i]*=2;
		for(int i=0;i<n;i++)X[i*2]=xs[i]-W,X[i*2+1]=xs[i]+W,Y[i*2]=ys[i]-H,Y[i*2+1]=ys[i]+H-1;
		sort(X,X+2*n);
		sort(Y,Y+2*n);
		for(int i=0;i<n;i++){
		et[i*2]=make_pair(make_pair(lb(X,X+n*2,xs[i]-W)-X,cs[i]),make_pair(lb(Y,Y+n*2,ys[i]-H)-Y,lb(Y,Y+n*2,ys[i]+H-1)-Y));
		et[i*2+1]=make_pair(make_pair(lb(X,X+n*2,xs[i]+W)-X,-cs[i]),make_pair(lb(Y,Y+n*2,ys[i]-H)-Y,lb(Y,Y+n*2,ys[i]+H-1)-Y));
		}
		sort(et,et+2*n);
		for(int i=0;i<2*n;i++)ad(et[i].second.first,et[i].second.second,et[i].first.second,1,0,2*n),ans=max(ans,nm[1]);
		cout<<ans<<"\n";
	}
	return 0;
}

上一题