代码求优化(超时了20%不知道怎么优化了,求大佬优化)
  • 板块灌水区
  • 楼主ZYH20190341315
  • 当前回复10
  • 已保存回复10
  • 发布时间2021/5/6 21:24
  • 上次更新2023/11/4 23:36:21
查看原帖
代码求优化(超时了20%不知道怎么优化了,求大佬优化)
400760
ZYH20190341315楼主2021/5/6 21:24

PIPI OJ上的一道题

点这里

求大佬帮忙优化一下,或者给个优化思路,或者新思路orz

#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
const int N=1e5+10;
int next1[4][2]= {1,0,0,1,0,-1,-1,0};
struct st {
	int x,y;
	int num;
	st() {
		num=0;
	}
};
class mp{//排序规则 
	public:
	bool operator()(const pair<int,int> &a,const pair<int,int> &b)const
	{
		if(a.first!=b.first)
			return a.first<b.first;
		if(a.second!=b.second)
			return a.second<b.second;
		return false;
	}
};
inline void read(int& x) 
{
	x = 0;
	char c;
	for (c = getchar(); c < '0' || c > '9'; c = getchar())
		;
	for(; c >= '0' && c <= '9'; c = getchar())
		x = (x << 3) + (x << 1) + c - '0';
}
void out(int a) 
{
	if(a > 9) 
   {
		out(a/10);
	}
	putchar(a%10 + '0');
}
/*
4
1 1
0 1
1 0
1 2
*/
int main() {
	int n;
	int x,y;
	int nx,ny,num=0;
	st s[N];
	pair<int,int>t;
	map<pair<int,int>,int,mp>m;
	read(n);
	for(int i=0; i<n; i++) { //O(n)
		read(x);
		read(y);
		s[i].x=x;
		s[i].y=y;
		t.first=x;
		t.second=y;
		m.insert(make_pair(t,1));
	}
	for(int i=0; i<n; i++)//O(4nlog2n)约等于 
	{
		x=s[i].x;
		y=s[i].y;
		nx=ny=0;
		num=0;
		for(int j=0; j<4; j++) 
		{
			nx=next1[j][0]+x;
			ny=next1[j][1]+y;
			t.first=nx;
			t.second=ny;
			if(m.find(t)!=m.end()) 
			{
				num++;
			}
		}
		s[i].num=num;
	}
	for(int i=0; i<n; i++)
		out(s[i].num),cout<<endl;
	return 0;
}
2021/5/6 21:24
加载中...