AtCoder Beginner Contest 273 CDE题解

C

感觉题目描述的巨绕,看了好久都很迷,(也可能是我的问题)。其实题意很简单,但样例就很快能明白。

思路:直接统计,然后根据题意输出。

code:

#include<bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=2e5+10;
const double eps=1e-4;
typedef pair<int,int> pii;
int a[N];
map<int,int> mp;
vector<int> v;

void work()
{
	int n,k;
	cin>>n;
	for(int i=1;i<=n;i++) {
		cin>>a[i]; mp[a[i]]++;
		v.push_back(a[i]);
	}
	
	sort(v.begin(),v.end());
	v.erase(unique(v.begin(),v.end()),v.end());
	int cnt=v.size();
	for(int i=n;i>0;i--){
		if(cnt>=0) cout<<mp[v[cnt-1]]<<endl;
		else cout<<0<<endl; 
		cnt--;
	}
	
}
signed main()
{
	ios;
	int t;
	//cin>>t;
	t=1;
	while(t--)
	{
		work();
	}
	
	return 0;
}

D
题目大意:有个n*m的网格,有N个格子有障碍物,你初始位置在(x,y)。接下来有Q次指令,每次指令为"URLD"的一种,还有一个数x表示向哪个方向移动多少次。不可以穿过障碍物和网格外面。问在每次指令后,你所处的位置坐标。

思路:我们可以把每一行和每一列的障碍物都记录下来,然后使用二分查找,来模拟走的过程。重点就是怎么模拟,为了方便,可以插入两个哨兵,避免对边界进行分类讨论。
此题有点卡常。我一开始是这样写的:

map<int,vector<int> > r,c;

这样的话有4个测试点过不去,换成:

map<int,set<int> > r,c;

这样就好了,直接使用set的二分查找就好。

code:

#include<bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//#define int long long
#define x first
#define y second
const int N=2e5+10;
const double eps=1e-4;
typedef pair<int,int> pii;

int n,m,k;
map<int,set<int> > r,c;
void work()
{
	scanf("%d%d",&n,&m); 
	//cin>>n>>m;
	int x,y;
	//cin>>x>>y;
	scanf("%d%d",&x,&y);
	//cin>>k;
	scanf("%d",&k);
	for(int i=1;i<=k;i++){
		int x1,y1;
		//cin>>x1>>y1;
		scanf("%d%d",&x1,&y1);
		r[x1].insert(y1); c[y1].insert(x1);
		
	}
	//sort(a.begin(),a.end()); sort(b.begin(),b.end());
	int q;
	//cin>>q;
	scanf("%d",&q);
	while(q--)
	{
		char op[2]; int cnt;
		//cin>>op>>cnt;
		scanf("%s",op); scanf("%d",&cnt);
		if(*op=='R'){
			if(r[x].size()) {
				r[x].insert(0);
				r[x].insert(m+1);
				
				auto id=r[x].upper_bound(y);
				int ans=*id;
				if(ans>y+cnt) y+=cnt;
				else y=ans-1;
				y=min(y,m);
			}
			else y=min(y+cnt,m);
		}
		if(*op=='D'){
			if(c[y].size()) {
				c[y].insert(0);
				c[y].insert(n+1);
				
				auto id=c[y].upper_bound(x);
				int ans=*id;
				if(ans>x+cnt) x+=cnt;
				else x=ans-1;
				x=min(x,n);
			}
			else x=min(x+cnt,n);
		}
		if(*op=='L'){
			if(r[x].size()) {
				r[x].insert(0);
				r[x].insert(m+1);
				
				auto id=r[x].upper_bound(y); id--;
				int ans=*id;
				if(ans<y-cnt) y-=cnt;
				else y=ans+1;
				y=max(y,1);
			}
			else y=max(y-cnt,1);
		}
		if(*op=='U'){
			if(c[y].size()) {
				c[y].insert(0);
				c[y].insert(n+1);
				
				auto id=c[y].upper_bound(x); id--;
				int ans=*id;
				if(ans<x-cnt) x-=cnt;
				else x=ans+1;
				x=max(x,1);
				//cout<<ans<<"###"<<endl;
			}
			else x=max(x-cnt,1);
		}
		//cout<<x<<" "<<y<<endl;
		printf("%d %d\n",x,y);
	}
}
signed main()
{
	int t;
	//cin>>t;
	t=1;
	while(t--)
	{
		work();
	}
	return 0;
}

E
题目大意:有一个序列A,和一本书。初始A和书都为空。有下面四种操作:
ADD x:给序列A的的末尾加上x
DELETE :若A不为空,则删除A的末尾元素
SAVE x:把书的第x页用A来替代
LOAD x:把A用书的第x页来替代

输出每次操作后,A的最后一个元素为多少。

思路:用链表来模拟插入和删除的操作即可。

code:

#include<bits/stdc++.h>

using namespace std;
#define endl '\n'
#define ios std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define x first
#define y second
const int N=1e6+10;
const double eps=1e-4;
typedef pair<int,int> pii;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

int n,m;
int last,cnt;
map<int,int> mp;
int a[N],fa[N];
void work()
{
	int q;
	cin>>q;
	a[0]=-1;
	while(q--)
	{
		string s;
		int x;
		cin>>s;
		if(s=="ADD"){
			cin>>x;
			a[++cnt]=x;
			fa[cnt]=last;
			last=cnt;
		}
		else if(s=="DELETE"){
			last=fa[last];
		}
		else if(s=="SAVE"){
			cin>>x;
			mp[x]=last;
		}
		else {
			cin>>x;
			last=mp[x];
		}
		cout<<a[last]<<" ";
	}
	
}  
signed main()
{
	ios;
	int t;
	//cin>>t;
	t=1;
	while(t--)
	{
		work();
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值