Codeforces Round 65 C. Round Table Knights(71)

https://codeforces.com/contest/71/problem/C

这道题,比较难,现在还没有完全吃透,参考别人的代码,大概意思

1)将圆桌n个骑士,分成 i (>3) 部分,每一份 step 个骑士。

2)然后每一部分执行step步,寻找是否=0,如果最后一个是0,那么结果就是0

注意:代码还没有完全理解,我参考别人的代码,理解到这里,还是不完全理解,暂时存放这里,方便手机查看和再思考。

C. Round Table Knights

代码

分析代码

#include <iostream>

using namespace std;

int main()

{

int n;

bool mood_good = true;

int step = 0;

int *list;

cin >> n;

list=new int[n];

for (int i = 0; i < n; i++)

{

cin >> list[i];

}

for (int i = 3; i <= n; i++)

{

//You must make N elements split i equally into parts. 将元素分i份。

if (n % i == 0)

{

//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;

step = n / i;//每一份step个

//cout <<"i="<< i << ",step= " << step << endl;

//every part: to check

for (int start = 0; start < step; start++)

{ //然后在每份的元素里操作

mood_good = true;

//cout << "start=" << start << endl;

// i is i parts

//这里是每份的元素。

for (int k = 0; k < i; k++)

{

int pos = start + k * step;

//cout << "list[pos("<<pos<<")]=" << list[pos] << endl;

if (pos >= n)

{

pos -= n;

//cout << "restart begin pos=" << pos << endl;

}

if (1!=list[pos])

{

//cout << "bad" << endl;

mood_good = false;

}

}

if (true == mood_good)

{

cout <<"YES"<<endl;

i = n + 10000; //to exit

break;

}

}

}

}

if (mood_good == false)

{

cout << "NO" << endl;

}

delete[] list;

return 0;

}

cpp 复制代码
/*
ref code:DhvanitVaghani

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];

for (int d = 3; d <= n; d++) {
if (n % d == 0) {
int step = n / d;
for (int start = 0; start < step; start++) {
bool ok = true;
for (int i = 0; i < d; i++) {
int pos = start + i * step;
if (pos >= n) pos -= n;
if (a[pos] != 1) ok = false;
}
if (ok) {
cout << "YES\n";
return 0;
}
}
}
}
cout << "NO\n";
return 0;
}
*/
#include <iostream>
using namespace std;

int main() 
{
	int n;
	bool mood_good = true;
	int step = 0;
	int *list;
	
	
	cin >> n;

	list=new int[n];
	
	for (int i = 0; i < n; i++)
	{
		cin >> list[i];
	}

	for (int i = 3; i <= n; i++) 
	{
		//You must make N elements split i equally into parts.
		if (n % i == 0) 
		{ 
			//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;
			step = n / i;
			
			//cout <<"i="<< i << ",step= " << step << endl;

			//every part: to check
			for (int start = 0; start < step; start++) 
			{
				mood_good = true;
				//cout << "start=" << start << endl;
				// i is i parts
				for (int k = 0; k < i; k++) 
				{
					int pos = start + k * step;
					//cout << "list[pos("<<pos<<")]=" << list[pos] << endl;

					if (pos >= n)
					{
						pos -= n;
						//cout << "restart begin pos=" << pos << endl;
					}
					
					if (1!=list[pos])
					{
						//cout << "bad" << endl;
						mood_good = false;
					}
				}

				if (true == mood_good)
				{
					cout <<"YES"<<endl;
					i = n + 10000; //to exit
					break;
				}
			}
		}
	}
	if (mood_good == false)
	{
		cout << "NO" << endl;
	}
	delete[] list;
	return 0;
}

/*
Examples
InputCopy
3
1 1 1
OutputCopy
YES
InputCopy
6
1 0 1 1 1 0
OutputCopy
YES
InputCopy
6
1 0 0 1 0 1
OutputCopy
NO


*/
相关推荐
特立独行的猫a12 分钟前
OpenHarmony平台移植 gifsicle:C/C++ 三方库适配实践(Lycium / tpc_c_cplusplus)
c语言·c++·harmonyos·openharmony·三方库适配·lycium
TImCheng060920 分钟前
内容运营岗位适合考哪个AI证书,与算法认证侧重点分析
人工智能·算法·内容运营
吴声子夜歌21 分钟前
ES6——对象的扩展详解
开发语言·javascript·es6
aq553560027 分钟前
编程语言对比:从汇编到PHP的四大层级解析
开发语言·汇编·php
赵域Phoenix30 分钟前
混沌系统是什么?
人工智能·算法·机器学习
kyle~32 分钟前
工程数学---Eigen库(C++唯一标配线性代数库)
开发语言·c++·线性代数
CoderCodingNo35 分钟前
【GESP】C++五、六级练习题 luogu-P1886 【模板】单调队列 / 滑动窗口
开发语言·c++·算法
paeamecium36 分钟前
【PAT甲级真题】- All Roads Lead to Rome (30)
数据结构·c++·算法·pat考试·pat
好家伙VCC41 分钟前
**发散创新:基于Rust的轻量级权限管理库设计与开源许可证实践**在现代分布式系统中,**权限控制(RBAC
java·开发语言·python·rust·开源
Cando学算法42 分钟前
双指针之快慢指针
算法