NKOJ-0613考试总结

这次考试共五个题。
1.三角塔:
在这里插入图片描述

本题不难,通过等差数列即可求出每层的个数。
解决时间:15分。

2.修改数字:
在这里插入图片描述

通过统计每个数字出现次数,优先将出现次数少的数字修改,即可得出答案。
代码:

#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[200010],vis[200010];
int ans;
int main(){
	ios::sync_with_stdio(false);
	//输入及记录数据
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]+=1;
	}
	//排序
	sort(vis,vis+200000,greater<int>());
	//统计
	while(vis[k]!=0){
		ans+=vis[k];
		k+=1;
	}
	cout<<ans;
	return 0;
}

解决时间:30分
3:移动骑士。
在这里插入图片描述

一道有手就行 很简单的BFS,八个方向,管你会不会,搜就完了~~
如果你不会就看一下我的代码:

#include<bits/stdc++.h>
using namespace std;
int n,x1,y1,x2,y2,vis[1000][1000];
int dx[8]={1,2,2,1,-1,-2,-2,-1};
int dy[8]={2,1,-1,-2,2,1,-1,-2};
struct node{
	int x_num;
	int y_num;
	int step;
	node(int x_num_,int y_num_,int step_){
		x_num=x_num_;
		y_num=y_num_;
		step=step_;
	}
};
queue<node> s;
int f(int sx,int sy){
	node a = node(sx,sy,0);
	s.push(a);
	vis[sx][sy]=1;
	while(!s.empty()){
		node now=s.front();
		s.pop();
		for(int i=0;i<8;i++){
		
			int nx=now.x_num+dx[i];
			int ny=now.y_num+dy[i];
			if(nx==x2&&ny==y2){
				return now.step+1;
			}
			if(nx>=0&&nx<n){
				if(ny>=0&&ny<n){
					if(vis[nx][ny]==0){
						vis[nx][ny]=1;
						s.push(node(nx,ny,now.step+1));
					}
				}
			}
		}
	}
}
int main(){
	cin>>n>>x1>>y1>>x2>>y2;
	cout<<f(x1,y1);
	return 0;
}

解决时间:30分。
还有45分钟。

4:数糖纸。
数糖纸

99%的人一看到这道题就一定会想到用暴力解。
那么:
评测说明 : 1s 256MB
再看数据:
对于100%的数据: 1 < = N < = 1 0 6 , 0 < = C i < = 1 0 9 1<=N<= 10^6 , 0<=C_i<= 10^9 1<=N<=1060<=Ci<=109

暴力解法时间复杂度: O ( n 3 ) O(n^3) O(n3)
所以不能用暴力解,会TLE。

正确方法应该用队解,每输入一个数就判断它是不是出现在队里,如果是,就将它前面的数出队,否则就将它入队,每进行一次操作就将答案与队列长度比较如果队列长度大于答案就将队列长度赋值给答案。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n;
long long a[1000009];
bool vis[1000000009];
queue<int> s;
int ans=0;
int p;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(vis[a[i]]){
			while(s.front()!=a[i]){
				vis[s.front()]=0;
				s.pop();
			}
			vis[s.front()]=0;
			s.pop();
		}
		s.push(a[i]);
		vis[a[i]]=1;
		p = s.size();
		ans = max(ans,p);
	}
	cout<<ans;
	return 0;
}

5:曲线难题。
在这里插入图片描述

此题为魔王中的魔王,十分难做。
但是但是但是,你要知道,

两端点都不在边上的曲线一定不会相交!!!

所以只用考虑边上的曲线就行了!

AC代码:

#include<bits/stdc++.h>
using namespace std;
int r,c,n;
struct node{
	int x,y,id,col;
};
bool cmp(node x,node y){
	if(x.id==y.id){
		if(x.id==1){
			return x.y<y.y;
		}
		if(x.id==2){
			return x.x<y.x;
		}
		if(x.id==3){
			return x.y>y.y;
		}
		if(x.id==4){
			return x.x>y.x;
		}
	}
	return x.id<y.id;
}
int get(int x,int y){
	if(x==0){
		return 1;
	}
	if(y==c){
		return 2;
	}
	if(x==r){
		return 3;
	}
	if(y==0){
		return 4;
	}
}
bool check(int a,int b){
	return a==0||b==0||a==r||b==c;
}
node a[200005];
stack<int> s;
int main(){
	ios::sync_with_stdio(false);
	int cnt=0;
	cin>>r>>c>>n;
	for(int i=0;i<n;i++){
		int x1,y1,x2,y2;
		cin>>x1>>y1>>x2>>y2;
		if(check(x1,y1)&&check(x2,y2)){
			a[cnt].x=x1;
			a[cnt].y=y1;
			a[cnt].col=i;
			a[cnt].id=get(x1,y1);
			cnt++;
			a[cnt].x=x2;
			a[cnt].y=y2;
			a[cnt].col=i;
			a[cnt].id=get(x2,y2);
			cnt++;
		}
	}
	sort(a,a+cnt,cmp);
	for(int i=0;i<cnt;i++){
		if(!s.empty()&&a[i].col==s.top()){
			s.pop();
		}
		else{
			s.push(a[i].col);
		}
	}
	if(s.empty()){
		cout<<"YES"<<endl;
	}
	else{
		cout<<"NO"<<endl;
	}
	return 0;
}

小结:速度不够 快 ⇒ \Overrightarrow{快} ,要是速度快可以A四道题。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值