P1003 铺地毯 普及模拟

前面的太简单了,能跳过吗(捂脸),我们从试炼场普及组开始吧

以后可能每个板块只会讲2,3道题,其余的可以自己写,看题解也可以

加油!向着,普及冲吧!!!

这道题的知识点板块已经说明,是一个模拟

但如果暴力模拟的话,会MLE掉

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,a,b,x,y,m,p;
int ditan[2002][2002];
inline int read(){
    int x=0,f=1;
    char ch;
    for(ch=getchar();(ch<'0'||ch>'9') && ch!='-';ch=getchar());
    if(ch=='-'){
	     f=-1;       
		 ch=getchar();   
	}
    while(ch>='0' && ch<='9') {
        x=(x<<3)+(x<<1)+ch-'0';ch=getchar();
    }
    return x*f;
}
inline void write(int x){
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		cin>>a>>b>>x>>y;
		for(int j=a;j<=a+x;j++)
			for(int k=b;k<=b+y;k++)
				ditan[j][k]=i;
	}
	m=read();p=read();
	/*for(int i=1;i<=a+x;i++){
		for(int j=1;j<=b+y;j++)
			write(ditan[i][j]);
		cout<<endl;
	}
	*/
	if(ditan[m][p])
		cout<<ditan[m][p];
	else cout<<-1;
	return 0;
}

只能得到50分

如果想要得全分的话,我们需要优化一下,思路如下:

将所有数据输入后,从上往下(即为从后往前)的查看第n层有无地毯,若有,直接输出,若无,输出-1;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,p;
int a[10001],b[10001],x[10001],y[10001];
inline int read(){
    int x=0,f=1;
    char ch;
    for(ch=getchar();(ch<'0'||ch>'9') && ch!='-';ch=getchar());
    if(ch=='-'){
	     f=-1;       
		 ch=getchar();   
	}
    while(ch>='0' && ch<='9') {
        x=(x<<3)+(x<<1)+ch-'0';ch=getchar();
    }
    return x*f;
}
inline void write(int x){
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
int main(){
	n=read();
	for(int i=1;i<=n;i++){
		a[i]=read();b[i]=read();x[i]=read();y[i]=read();
	}
	m=read();p=read();
	for(int i=n;i>=1;i--)
		if(a[i]<=m&&a[i]+x[i]>=m&&b[i]<=p&&b[i]+y[i]>=p){
			write(i);
			return 0;
		}
	cout<<-1;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42739663/article/details/81142796