当前位置: 首页 > news >正文

【ad-hoc】# P12414 「YLLOI-R1-T3」一路向北|普及+

本文涉及知识点

ad-hoc

P12414 「YLLOI-R1-T3」一路向北

题目描述

给定 n n n 个队列,每个队列中有 m m m 个正整数,这些数均小于等于 n n n,第 i i i 个队列的第 j j j 个元素为 a i , j a_{i,j} ai,j a i , 1 a_{i,1} ai,1 为队首, a i , m a_{i,m} ai,m 为队尾。

现在你的手中拿着一个数字 0 0 0,你要选择一个队列将 0 0 0 放到其队尾,并把其队首拿到手中。

接下来你将重复进行一个操作直到再次把 0 0 0 拿回手中:

  • 设你手中的数字为 p p p,将其放到第 p p p 个队列的队尾,并把第 p p p 个队列的队首拿到手中。

现在小 Y 想知道,在无限的时间中,你是否可以不再拿回 0 0 0?如果可以,则输出 Yes,否则输出 No

输入格式

本题有多组测试数据。

第一行一个整数 T T T,表示数据组数。

对于每组数据:

第一行两个正整数 n , m n,m n,m

接下来 n n n 行,每行 m m m 个正整数,第 i i i 行第 j j j 个数表示 a i , j a_{i,j} ai,j

输出格式

对于每组数据,输出一行:

在无限的时间中,若你可以不再拿回 0 0 0,则输出 Yes,否则输出 No

输入输出样例 #1

输入 #1

1
3 2
2 2
3 3
1 1

输出 #1

No

输入输出样例 #2

输入 #2

1
3 2
2 1
3 3
2 2

输出 #2

Yes

说明/提示

【样例解释#1】

以下模拟一开始将 0 0 0 放到第 1 1 1 个队列的情况。

//手中数字:
0
//队列数字:(左边第一个为队首,右边第一个为队尾)
2 2
3 3
1 1
//手中数字:
2
//队列数字:
2 0
3 3
1 1
//手中数字:
3
//队列数字:
2 0
3 2
1 1
//手中数字:
1
//队列数字:
2 0
3 2
1 3
//手中数字:
2
//队列数字:
0 1
3 2
1 3
//手中数字:
3
//队列数字:
0 1
2 2
1 3
//手中数字:
1
//队列数字:
0 1
2 2
3 3
//手中数字:
0
//队列数字:
1 1
2 2
3 3
【样例解释#2】

通过模拟可以发现当且仅当一开始把 0 0 0 放到第 1 1 1 个队列时,才可以不再拿回 0 0 0。因为在经过了若干轮后第 2 2 2 个队列会被 2 2 2 填满,并且手中的数字也是 2 2 2,所以将在第 2 2 2 个队列一直循环。

【数据范围】

本题采用捆绑测试。

  • Subtask 1(20 pts): n ≤ 2 n\le2 n2
  • Subtask 2(10 pts): ∀ a i , j = i \forall a_{i,j}=i ai,j=i
  • Subtask 3(20 pts): n × m ≤ 1000 n\times m \le 1000 n×m1000
  • Subtask 4(50 pts):无特殊限制。

对于全部数据,保证 1 ≤ T ≤ 10 1\le T\le 10 1T10 1 ≤ n × m ≤ 1 0 5 1\le n\times m\le 10^5 1n×m105 1 ≤ a i , j ≤ n 1\le a_{i,j}\le n 1ai,jn

ad-hoc

性质一:如果死循环,则只会在一个队列循环。因为每次切换队列都有一个x回到本队列。故多队列最多循环 n m nm nm次。
性质二:如果x的总数量<m,则队列不会进入死循环或取到0。每次取走一个再回来必定带回一个x。带不会x,就无法回到队列。
性质三:如果x的总数量是m,0不在x队列,则x队列不会死循环。
取走最后一个非x时,队列种有m个x。 ⟺ \iff 非x队列没有x ⟺ \iff 无法回到x。

实现

如果任意数目都是m,则根据性质三,非0队列都不会死循环,最终一定能把0取出来。返回No。
否则选择数量最少的x,返回Yes。

代码

核心代码

#include <iostream>
#include <sstream>
#include <vector>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<string>
#include<algorithm>
#include<functional>
#include<queue>
#include <stack>
#include<iomanip>
#include<numeric>
#include <math.h>
#include <climits>
#include<assert.h>
#include<cstring>
#include<list>
#include<array>#include <bitset>
#include <chrono>
using namespace std::chrono;
using namespace std;template<class T1, class T2>
std::istream& operator >> (std::istream& in, pair<T1, T2>& pr) {in >> pr.first >> pr.second;return in;
}template<class T1, class T2, class T3 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t);return in;
}template<class T1, class T2, class T3, class T4 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t);return in;
}template<class T1, class T2, class T3, class T4, class T5 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) ;return in;
}template<class T1, class T2, class T3, class T4, class T5, class T6, class T7 >
std::istream& operator >> (std::istream& in, tuple<T1, T2, T3, T4, T5, T6, T7>& t) {in >> get<0>(t) >> get<1>(t) >> get<2>(t) >> get<3>(t) >> get<4>(t) >> get<5>(t) >> get<6>(t);return in;
}template<class T = int>
vector<T> Read() {int n;cin >> n;vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}
template<class T = int>
vector<T> ReadNotNum() {vector<T> ret;T tmp;while (cin >> tmp) {ret.emplace_back(tmp);if ('\n' == cin.get()) { break; }}return ret;
}template<class T = int>
vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {cin >> ret[i];}return ret;
}template<int N = 1'000'000>
class COutBuff
{
public:COutBuff() {m_p = puffer;}template<class T>void write(T x) {int num[28], sp = 0;if (x < 0)*m_p++ = '-', x = -x;if (!x)*m_p++ = 48;while (x)num[++sp] = x % 10, x /= 10;while (sp)*m_p++ = num[sp--] + 48;AuotToFile();}void writestr(const char* sz) {strcpy(m_p, sz);m_p += strlen(sz);AuotToFile();}inline void write(char ch){*m_p++ = ch;AuotToFile();}inline void ToFile() {fwrite(puffer, 1, m_p - puffer, stdout);m_p = puffer;}~COutBuff() {ToFile();}
private:inline void AuotToFile() {if (m_p - puffer > N - 100) {ToFile();}}char  puffer[N], * m_p;
};template<int N = 1'000'000>
class CInBuff
{
public:inline CInBuff() {}inline CInBuff<N>& operator>>(char& ch) {FileToBuf();while (('\r' == *S) || ('\n' == *S) || (' ' == *S)) { S++; }//忽略空格和回车ch = *S++;return *this;}inline CInBuff<N>& operator>>(int& val) {FileToBuf();int x(0), f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行		return *this;}inline CInBuff& operator>>(long long& val) {FileToBuf();long long x(0); int f(0);while (!isdigit(*S))f |= (*S++ == '-');while (isdigit(*S))x = (x << 1) + (x << 3) + (*S++ ^ 48);val = f ? -x : x; S++;//忽略空格换行return *this;}template<class T1, class T2>inline CInBuff& operator>>(pair<T1, T2>& val) {*this >> val.first >> val.second;return *this;}template<class T1, class T2, class T3>inline CInBuff& operator>>(tuple<T1, T2, T3>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val);return *this;}template<class T1, class T2, class T3, class T4>inline CInBuff& operator>>(tuple<T1, T2, T3, T4>& val) {*this >> get<0>(val) >> get<1>(val) >> get<2>(val) >> get<3>(val);return *this;}template<class T = int>inline CInBuff& operator>>(vector<T>& val) {int n;*this >> n;val.resize(n);for (int i = 0; i < n; i++) {*this >> val[i];}return *this;}template<class T = int>vector<T> Read(int n) {vector<T> ret(n);for (int i = 0; i < n; i++) {*this >> ret[i];}return ret;}template<class T = int>vector<T> Read() {vector<T> ret;*this >> ret;return ret;}
private:inline void FileToBuf() {const int canRead = m_iWritePos - (S - buffer);if (canRead >= 100) { return; }if (m_bFinish) { return; }for (int i = 0; i < canRead; i++){buffer[i] = S[i];//memcpy出错			}m_iWritePos = canRead;buffer[m_iWritePos] = 0;S = buffer;int readCnt = fread(buffer + m_iWritePos, 1, N - m_iWritePos, stdin);if (readCnt <= 0) { m_bFinish = true; return; }m_iWritePos += readCnt;buffer[m_iWritePos] = 0;S = buffer;}int m_iWritePos = 0; bool m_bFinish = false;char buffer[N + 10], * S = buffer;
};class Solution {
public:bool Ans(const int N, vector<int>& v) {vector<int> cnt(N + 1);for (const auto& i : v) { cnt[i]++; }const int iMin = *min_element(cnt.begin() + 1, cnt.end());return iMin < v.size() / N;}
};int main() {
#ifdef _DEBUGfreopen("a.in", "r", stdin);
#endif // DEBUG	ios::sync_with_stdio(0); cin.tie(nullptr);//CInBuff<> in; COutBuff<10'000'000> ob;int T;cin >> T;while (T--){int R, C;cin >> R >> C ;auto v = Read<int>(R * C);
#ifdef _DEBUG	printf("R=%d", R);Out(v, ",v=");//Out(B, ",B=");//Out(que, ",que=");
#endif // DEBUG		Solution slu;auto res = Solution().Ans(R,v);cout << (res?"Yes":"No") << "\n";}return 0;
}

单元测试

int R;vector<int> v;TEST_METHOD(TestMethod11){R = 3, v = { 2,2,3,3,1,1 };Solution slu;auto res = slu.Ans(R, v);AssertEx(false, res);}TEST_METHOD(TestMethod12){R = 3, v = { 2,1,3,3,2,2 };Solution slu;auto res = slu.Ans(R, v);AssertEx(true, res);}

扩展阅读

我想对大家说的话
工作中遇到的问题,可以按类别查阅鄙人的算法文章,请点击《算法与数据汇总》。
学习算法:按章节学习《喜缺全书算法册》,大量的题目和测试用例,打包下载。重视操作
有效学习:明确的目标 及时的反馈 拉伸区(难度合适) 专注
闻缺陷则喜(喜缺)是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛
失败+反思=成功 成功+反思=成功

视频课程

先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771
如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

相关文章:

  • 《弦论视角下前端架构:解构、重构与无限延伸的可能》
  • 商业秘密保护新焦点:企业如何守护核心经营信息?
  • 【硬核数学】2.1 升级你的线性代数:张量,深度学习的多维数据语言《从零构建机器学习、深度学习到LLM的数学认知》
  • STM32——MDK5编译和串口下载程序+启动模式
  • 信创背景下应用软件迁移解析:从政策解读到落地实践方案
  • 详细的说一下什么是Arduino?
  • 【硬核数学】2.5 “价值标尺”-损失函数:信息论如何设计深度学习的损失函数《从零构建机器学习、深度学习到LLM的数学认知》
  • OpenCV学习3
  • 《平行宇宙思维如何让前端错误处理无懈可击》
  • (七)集成学习
  • python 使用 pyenv 管理 python 版本
  • 常用指令合集(DOS/Linux/git/Maven等)
  • 高并发电商返利 APP 架构设计:从淘客佣金模型到分布式导购系统的技术落地
  • [分布式并行] 流水线并行 PP(NaivePP/GPipe/F-then-B/PipeDream/1F1B)
  • CppCon 2017 学习:Type Punning in C++17 Avoiding Pun-defined Behavior
  • 设计模式-外观模式、适配器模式
  • Oracle 递归 + Decode + 分组函数实现复杂树形统计进阶(第二课)
  • 基于Pandas和FineBI的昆明职位数据分析与可视化实现(三)- 职位数据统计分析
  • TCP/IP模型、OSI模型与C# Socket编程详解
  • (LeetCode 每日一题) 1498. 满足条件的子序列数目 (双指针)