POJ1063 Flip and Shift(思维)
链接:https://ac.nowcoder.com/acm/problem/105660
来源:牛客网
题目描述
This puzzle consists of a random sequence of m black disks and n white disks on an oval-shaped track, with a turnstile capable of flipping (i.e., reversing) three consecutive disks. In Figure 1, there are 8 black disks and 10 white disks on the track. You may spin the turnstile to flip the three disks in it or shift one position clockwise for each of the disks on the track (Figure 1).
The goal of this puzzle is to gather the disks of the same color in adjacent positions using flips and shifts. (Figure 2)
You are to write a program which decides whether a given sequence can reach a goal or not. If a goal is reachable, then write a message "YES"; otherwise, write a message "NO".
输入描述:
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input file. Each of the next T lines gives a test case. A test case consists of an integer, representing the sum of m and n, and a sequence of m+n 0s and 1s, representing an initial sequence. A 0 denotes a white disk and a 1 denotes a black disk. The sum of m and n is at least 10 and does not exceed 30. There is a space between numbers.
输出描述:
The output should print either "YES" or "NO" for each test case, one per line.
示例1
输入
[复制](javascript:void(0)😉
2
18 0 0 1 0 1 1 1 1 0 1 0 0 1 0 0 0 0 1
14 1 1 0 0 1 1 1 0 0 1 1 0 1 0
输出
[复制](javascript:void(0)😉
YES
NO
题意相当于可以任取首位相连的序列的相邻三个数reverse,问能否把黑色白色分别凑到一起。
n为奇数一定可以,因为这样奇数位置的数就能换到偶数位置,偶数位置的数也能换到奇数位置。
n为偶数的话偶数位置的数只能在内部换,奇数位置同理,这种情况下由于偶数位置奇数位置个数是一样的,必须要求奇数位置的黑色和偶数位置的黑色相差不超过1,如下所示:
1 2 3 4 5 6 7 8
1 1 1 1 0 0 0 0
1 2 3 4 5 6 7 8
1 1 1 0 0 0 0 0
1 2 3 4 5 6 7 8
0 1 1 1 0 0 0 0
而下面这种相差为2的情况尽力凑的结果也是不行的(偶数位置的1已经尽力贴近奇数位置的一个1了):
1 2 3 4 5 6 7 8
1 1 0 1 0 0 0 1
#include <iostream>
#include <cmath>
using namespace std;
bool a[100005];
int main() {
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
if(n & 1) cout << "YES" << endl;
else {
int b1 = 0, b2 = 0;
for(int i = 1; i <= n; i++) {
if(a[i]) {
if(i & 1) b1++;
else b2++;
}
}
int tmp = b1 - b2;
if(tmp < 0) tmp = -tmp;
if(tmp <= 1) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-05-04 洛谷P3275 [SCOI2011]糖果(缩点+拓扑序DP)