AtCoder Beginner Contest 247 (A ~ D)

A - Move Right

题意:

小学水平题...刚开始还没读懂(废

分析:

给定一个长度为4的字符串,让新字符串下标为 1,2,31, 2, 3 的字符为给定字符串下标为 0,1,20, 1, 2 的字符,新字符串下标为 00 的字符为 0'0'

代码:

#include <iostream>
using namespace std;
int main()
{
	string s;
	cin >> s;
	
	cout << ("0" + s.substr(0, 3));
	
	return 0;
}

B - Unique Nicknames

题意:

给定 nn 个人的两个名字,可以任选一个名字作为一个人的昵称,要求这个人的昵称不可以是其他人两个名字中的任意一个,求是否每个人都能拥有自己的昵称

分析:

multisetmultiset 维护所有人的名字,如果一个人的两个名字中的任意一个名字都在集合中出现了不止一次,且这个人的两个名字不同,则说明这个人不能有自己的昵称

代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

string a[N], b[N];

int main()
{
	multiset<string> s;
	
	int n;
	cin >> n;
	for (int i = 1;i <= n;i ++) 
	{
	    cin >> a[i] >> b[i];
	    s.insert(a[i]);
	    s.insert(b[i]);
	}
	
	bool flag = false;
	for (int i = 1;i <= n;i ++)
	{
	    if (a[i] != b[i] && s.count(a[i]) > 1 && s.count(b[i]) > 1) flag = true;
	}
	
	if (flag) cout << "No";
	else cout << "Yes";
	
	return 0;
}

C - 1 2 1 3 1 2 1

题意:

找规律

1 -> 1

2 -> 1 2 1

3 -> 1 2 1 3 1 2 1

4 -> 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1

...

输入 NN ,求对应的序列

分析:

很不错的一道递归题

代码:

#include <bits/stdc++.h>
using namespace std;

const int N = 110;

void f(int x)
{
    if (x == 0) return;
    f(x - 1);
    cout << x << ' ';
    f(x - 1);
}

int main()
{
	int n;
	cin >> n;
	
	f(n);
	
	return 0;
}

D - Cylinder

题意:

有一个队列,有两种操作:

  • 1 从队尾插入 cc 个值为 xx 的小球
  • 2 从队头取出 cc 个小球,并输出 cc 个小球值的总和

分析:

用二元队列模拟一下即可,一个一个插入必超时,可直接用二元组的方式存储

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
	queue<pair<LL, LL>> q;
	
	int n;
	cin >> n;
	for (int i = 1;i <= n;i ++)
	{
	    int t;
	    cin >> t;
	    if (t == 1)
	    {
	        int x, c;
	        cin >> x >> c;
	        q.push({x, c});
	    }
	    else
	    {
	        int c;
	        cin >> c;
	        LL sum = 0, cnt = 0;
	        while (cnt < c)
	        {
	            if (cnt + q.front().second <= c)
	            {
	                cnt += q.front().second;
	                sum += q.front().second * q.front().first;
	                q.pop();
	            }
	            else
	            {
	                q.front().second -= (c - cnt);
	                sum += (c - cnt) * q.front().first;
	                cnt += (c - cnt);
	            }
	            if (cnt == c) cout << sum << endl;
	        }
	    }
	}
	
	return 0;
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务