首页 > 其他分享 >AtCoder Beginner Contest 241 (Sponsored by Panasonic) D~F 题解

AtCoder Beginner Contest 241 (Sponsored by Panasonic) D~F 题解

时间:2024-09-08 21:18:25浏览次数:1  
标签:AtCoder le Beginner 10 int 题解 d% auto include

D - Sequence Query

题目大意

我们有一个空序列\(A\)。请依次处理\(Q\)个命令,每个命令有三种类型,每种类型的格式如下:

  • 1 x:将\(x\)加入\(A\)(不去重
  • 2 x k:求在\(A\)的\(\le x\)的元素中,第\(k\)大的值。
  • 3 x k:求在\(A\)的\(\ge x\)的元素中,第\(k\)小的值。

\(1\le Q\le 2\times 10^5\)
\(1\le x\le 10^{18}\)
\(1\le k\le 5\)

分析

注意题面中的\(1\le k\le 5\),我们可以用multiset解决问题。
multiset顾名思义,就是不去重的set,支持二分查找操作。关于multiset的具体用法,请看这里
对于每个查询,我们作如下处理:

  1. 1 x:直接加入multiset
  2. 2 x k:先upper_bound,再将iterator向前移动\(k\)步
  3. 3 x k:先lower_bound,再将iterator向后移动\(k\)步

前面提到,因为\(k\)的值很小,所以移动iterator的时间复杂度可以忽略不计。
因此,总时间复杂度最优为\(\mathcal O(Q)\),平均\(\mathcal O(Q\log Q)\),最坏\(\mathcal O(Q\log Q)\)。

代码

#include <cstdio>
#include <set>
using namespace std;

int main()
{
	multiset<long long> s;
	int q;
	scanf("%d", &q);
	while(q--)
	{
		int op;
		long long x;
		scanf("%d%lld", &op, &x);
		if(op == 1) s.insert(x);
		else
		{
			int k;
			scanf("%d", &k);
			if(op == 2)
			{
				bool bad = false;
				auto it = s.upper_bound(x);
				for(; k--; --it)
					if(it == s.begin())
					{
						bad = true;
						break;
					}
				if(bad) puts("-1");
				else printf("%lld\n", *it);
			}
			else
			{
				auto it = s.lower_bound(x);
				for(; --k; ++it)
					if(it == s.end())
						break;
				if(it == s.end()) puts("-1");
				else printf("%lld\n", *it);
			}
		}
	}
	return 0;
}

E - Putting Candies

题目大意

给定长度为\(N\)的序列\(A=(A_0,A_1,\dots,A_{N-1})\)。
有一个空盘子。Takahashi每次会在其中加入\(A_{(X\bmod N)}\)颗糖果(\(X\)是当前盘子中糖果的数量)。
求\(K\)次操作后的糖果总数。

\(2\le N\le 2\times 10^5\)
\(1\le K\le 10^{12}\)
\(1\le A_i\le 10^6\)

分析

根据鸽笼原理(又称抽屉原理),\(A_{(X\bmod N)}\)的结果在最多\(N\)次操作后一定会重复。
因此,这道题可以看作数学上的一道周期问题。(又是数学题?!
我们只需分别记录结果对应的时间和时间对应的结果即可。
最终总时间复杂度\(\mathcal O(n)\),空间复杂度\(\mathcal O(n)\)。

代码

代码参考:AtCoder官方题解

#include <cstdio>
#define maxn 200005
using namespace std;

using LL = long long;
LL A[maxn], S[maxn];
int pre[maxn];

int main()
{
	int n;
	LL k;
	scanf("%d%lld", &n, &k);
	for(int i=0; i<n; i++)
		scanf("%lld", A + i);
	for(int i=1; i<n; i++)
		pre[i] = -1;
	int time, s;
	for(int i=0; i<n; i++)
	{
		S[i + 1] = S[i] + A[S[i] % n];
		if(pre[S[i + 1] % n] != -1)
		{
			s = pre[S[i + 1] % n];
			time = i + 1;
			break;
		}
		pre[S[i + 1] % n] = i + 1;
	}
	if(k <= s) printf("%lld\n", S[k]);
	else
	{
		int p = time - s;
		LL X = S[time] - S[s], t = k - s - 1;
		printf("%lld\n", S[s + t % p + 1] + t / p * X);
	}
	return 0;
}

F - Skate

题目大意

有一个\(H\times W\)的网格。网格上有\(N\)个障碍物,第\(i\)个的位置是\((X_i,Y_i)\)。
我们从\((s_x,s_y)\)开始,每一步向上、下、左、右中的一个方向行走,直到撞上障碍物,停在它前面的方格中。求到达\((g_x,g_y)\)所用的最少步数。若无法到达终点,输出-1

\(1\le H,W\le 10^9\)
\(1\le N\le 10^5\)
\(1\le s_x,g_x,X_i\le H\)
\(1\le s_y,g_y,Y_i\le W\)
\((s_x,g_x)\ne(g_x,g_y)\ne(X_i,Y_i)\)
\((X_i,Y_i)\ne(X_j,Y_j)\)(\(i\ne j\))

分析

这道题看似数据范围很大,实则不然。因为\(N\)只有\(10^5\),所以我们很容易想到使用\(\text{BFS}\),用map存储每行每列,对于每个坐标,二分查找当前行/列中的位置即可。

代码

写代码时注意事项主要有两点:

  1. 行和列的坐标一定要排序,也可以用set
  2. 注意二分边界情况
#include <cstdio>
#include <queue>
#include <set>
#include <unordered_map>
using namespace std;

using LL = long long;
unordered_map<int, set<int>> row, col;
unordered_map<LL, int> dis;

inline LL pack(LL x, int y) { return x << 31LL | y; }
inline void unpack(const LL& b, int& x, int& y) { x = b >> 31LL, y = b & 0x7fffffff; }

int main()
{
	int h, w, n, sx, sy, gx, gy;
	scanf("%d%d%d%d%d%d%d", &h, &w, &n, &sx, &sy, &gx, &gy);
	for(int i=0; i<n; i++)
	{
		int x, y;
		scanf("%d%d", &x, &y);
		row[x].insert(y);
		col[y].insert(x);
	}
	LL target = pack(gx, gy);
	queue<pair<LL, int>> q;
	q.emplace(pack(sx, sy), 0);
	while(!q.empty())
	{
		auto [p, d] = q.front(); q.pop();
		if(!dis.emplace(p, d).second) continue;
		if(p == target) { printf("%d\n", d); return 0; }
		int x, y;
		unpack(p, x, y), ++d;
		if(row.count(x))
		{
			auto& s = row[x];
			auto it = s.lower_bound(y);
			if(it != s.end()) q.emplace(pack(x, *it - 1), d);
			if(it != s.begin()) q.emplace(pack(x, *--it + 1), d);
		}
		if(col.count(y))
		{
			auto& s = col[y];
			auto it = s.lower_bound(x);
			if(it != s.end()) q.emplace(pack(*it - 1, y), d);
			if(it != s.begin()) q.emplace(pack(*--it + 1, y), d);
		}
	}
	puts("-1");
	return 0;
}

标签:AtCoder,le,Beginner,10,int,题解,d%,auto,include
From: https://www.cnblogs.com/stanleys/p/18403480/abc241

相关文章

  • AtCoder Beginner Contest 242 C~E 题解
    C-1111galpassword题目大意给定正整数\(N\),求符合下列条件的整数\(X\)的个数,对\(998244353\)取模:\(X\)是\(N\)位的正整数\(X\)的每一位数都在\([1,9]\)之间(0不行);\(X\)的相邻两位数之差的绝对值不超过\(1\)。\(2\leN\le10^6\)输入格式\(N\)输出格式输出答案。样......
  • AtCoder Beginner Contest 245 A~E 题解
    A-Goodmorning题目大意在同一天里,Takahashi在\(A\)时\(B\)分起床,Aoki在\(C\)时\(D\)分\(1\)秒起床,请问谁起床更早?\(0\leA,C<24\)\(0\leB,D<60\)输入格式\(A~B~C~D\)输出格式输出起得更早的人的名字(Takahashi或Aoki)。样例\(A\)\(B\)\(C\)\(D\)输出\(7\)......
  • AtCoder Beginner Contest 244 D~F 题解
    D-SwapHats题目大意有\(3\)个Takahashi,他们帽子的颜色分别为\(S_1,S_2,S_3\)。我们现在想通过正好\(10^{18}\)次操作,使得\(S_i=T_i\)。每次操作如下:选择\((i,j)\),交换\(S_i\)和\(S_j\)。试问能否达成目标?输入格式\(S_1~S_2~S_3\)\(T_1~T_2~T_3\)输出格式如果能达......
  • ARC138 B - 01 Generation 题解
    ARC138B-01Generation思路考虑逆向思维,很容易想到可以优先从后面删掉0(操作B的逆向操作),然后如果前面是0则删掉它并将序列翻转(操作A的逆向操作),一直重复这两个步骤直到字符串为空。如果中途无法操作,输出No,否则输出Yes。下面我们来证明这个方法的正确性:首先,假设有一个序列\(A......
  • AtCoder题解集锦
    注:本文原发表于CSDN,现已停止更新。原文如下:AtCoder题解集锦自己从全网整理的一些优质AtCoder题解,目前只有ABC(AtCoderBeginnerContest)的C~F。不定期更新。如您有更多需求,欢迎私信我或在评论区留言!\(\rarr\)题解列表传送门用法表格查找找到对应比赛的行找到对应......
  • AtCoder Beginner Contest 250 C~E 题解
    C-AdjacentSwaps题目大意\(N\)个球从左到右排成一列。开始时,从左往右的第\(i\)个球上写着数字\(i\)。请执行\(Q\)个操作,第\(i\)个操作如下:令\(j=~N\)个球中写着数字\(x_i\)的球的位置如果\(j=N\),将其与第\(j-1\)个球交换;否则,与第\(j+1\)个球交换。求所有操作后的球上分......
  • UNIQUE VISION Programming Contest 2022(AtCoder Beginner Contest 248)C~D 题解
    C-DiceSum题目大意有多少个整数序列\(A=(A_1,\dots,A_N)\)符合如下条件:\(1\leA_i\leM\)\(\sum\limits_{i=1}^NA_i\leK\)输出答案,对\(998244353\)取模。\(1\leN,M\le50\)\(N\leK\leNM\)输入格式\(N~M~K\)输出格式输出答案,对\(998244353\)取模。分析艹C题......
  • AtCoder Beginner Contest 252 A~G 题解
    前言这是我第一次写7题(A~G)的ABC题解,若有写得不好或者不到位的地方请多多指教,我将万分感激,感谢大家的支持!A-ASCIIcode题目大意给定正整数\(N\),输出ASCII码是\(N\)的字母。\(97\leN\le122\)输入格式\(N\)输出格式输出ASCII码是\(N\)的字母。分析注意a对应\(97\)......
  • AtCoder Beginner Contest 192 A~D 题解
    A-Star题目大意下一个大于\(X\)的\(100\)的倍数与\(X\)的差是多少?\(1\leX\le10^5\)输入格式\(X\)输出格式输出答案。样例\(X\)输出\(140\)\(60\)\(1000\)\(100\)分析下一个大于\(X\)的\(100\)的倍数是\((\lfloorX/100\rfloor+1)\times100\)。所......
  • AtCoder Beginner Contest 191 A~D 题解
    A-VanishingPitch题目大意一个球的速度是\(V~\text{m/s}\),它飞了\(T\)秒后会隐形,飞了\(S\)秒时会接触隐形。球在飞了\(D\)米后,人能看见它吗?输出Yes或者No。\(1\leV\le1000\)\(1\leT<S\le1000\)\(1\leD\le1000\)输入格式\(V~T~S~D\)输出格式输出答案。样例......