UPC Blink && Luxury River Cruise(循环节)

Blink中文题面友链 传送门

Blink

题目描述
Unhappy with the dim lighting in his barn, Farmer John has just installed a fancy new chandelier consisting of N (3 <= N <= 16) lights bulbs arranged in a circle.

The cows are fascinated by this new light fixture, and enjoy playing the following game: at time T, they toggle the state of each light bulb if its neighbor to the left was turned on at time T-1. They continue this game for B units of time (1 <= B <= 10^15). Note that B might be too large to fit into a standard 32-bit integer.

Given the initial states of the light bulbs, please determine their final states after B units of time have elapsed.
输入

  • Line 1: Two space-separated integers, N and B.
  • Lines 2…1+N: Line i+1 contains the initial state of bulb i, either 0 (off) or 1 (on).
    输出
  • Lines 1…N: Line i should contain the final state of bulb i, either 0 (off) or 1 (on).
    样例输入 Copy
    5 6
    1
    0
    0
    0
    0
    样例输出 Copy
    1
    1
    1
    0
    1
    提示
    There are five light bulbs. The first is initially on, and the others are off.The light bulb states are as follows:
    Time T=0: 1 0 0 0 0
    Time T=1: 1 1 0 0 0
    Time T=2: 1 0 1 0 0
    Time T=3: 1 1 1 1 0
    Time T=4: 1 0 0 0 1
    Time T=5: 0 1 0 0 1
    Time T=6: 1 1 1 0 1

题意:
给定一个操作和初始状态,问经过b个操作后的状态。
思路:
先看一眼题目,好了暴力模拟。
看一眼数据范围,退出比赛(手动滑稽)

其实想想就知道这么多操作肯定是有规律(循环)的,我们要做的就是找出循环节。因为每一次给的初始状态不一样,所以每次循环节的开头也不一样。当我们还未找到循环节但是已经进行了b个操作后,可以停止找循环节的过程,直接输出。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
int st[550][20];//储存每次灯的状态 
bool last[20];//判断上次灯的状态 
int n,t=1,s=0;//灯的数量,循环次数,周期,循环起始位置 
ll m;
void check(int a,int b){
	bool flag=1;
	for(int i=0;i<n;i++)
		if(st[a][i]!=st[b][i]) flag=0;
	if(flag) s=a,t=b-a;
}
void AC(){
   n=read();scanf("%lld",&m);
   for(int i=0;i<n;i++) st[0][i]=read();
   for(int i=1;i<=500;i++){
   		memset(last,0,sizeof last);
   		for(int j=0;j<n;j++){
   			if(st[i-1][j]) last[j]=1;
			st[i][j]=st[i-1][j];//转移上次状态	
		}
		for(int j=0;j<n;j++)
			if(last[j]) st[i][(j+1)%n]^=1;
		for(int j=1;j<i;j++){
			check(j,i);
			if(t>1)  break;
		}
		if(i==m){
			for(int j=0;j<n;j++){
				out(st[m][j]);
				puts("");
			}
			return ;
		}
   } 
   for(int i=0;i<n;i++){
   		out(st[(m-s)%t+s][i]);
   		puts("");
   }
}
int main(){
    AC();
    return 0;
}

Luxury River Cruise

题目描述
Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with N ports (1 <= N <= 1,000) labeled 1…N, and Bessie starts at port 1. Each port has exactly two rivers leading out of it which
lead directly to other ports, and rivers can only be sailed one way.

At each port, the tour guides choose either the “left” river or the “right” river to sail down next, but they keep repeating the same choices over and over. More specifically, the tour guides have chosen a short sequence of M
directions (1 <= M <= 500), each either “left” or “right”, and have repeated it K times (1 <= K <= 1,000,000,000). Bessie thinks she is going in circles – help her figure out where she ends up!

输入

  • Line 1: Three space-separated integers N, M, and K.
  • Lines 2…N+1: Line i+1 has two space-separated integers, representing the number of the ports that port i’s left and right rivers lead to, respectively.
  • Line N+2: M space-separated characters, either ‘L’ or ‘R’. ‘L’ represents a choice of ‘left’ and ‘R’ represents a choice of ‘right’.
    输出
  • Line 1: A single integer giving the number of the port where Bessie’s cruise ends.
    样例输入 Copy
    4 3 3
    2 4
    3 1
    4 2
    1 3
    L L R
    样例输出 Copy
    4
    提示
    The port numbers are arranged clockwise in a circle, with ‘L’ being a clockwise rotation and ‘R’ being a counterclockwise rotation. The sequence taken is LLRLLRLLR.After the first iteration of the sequence of directions, Bessie is at port 2 (1 -> 2 -> 3 -> 2); after the second, she is at port 3 (2 -> 3 -> 4 -> 3), and at the end she is at port 4 (3 -> 4 -> 1 -> 4).

题意:
思路: 有了上一题的思路想着这题就很容易了~还是一个找循环节的过程

代码:
未完待续

参考资料:
P2203 Blink 题解 - 洛谷 | 计算机科学教育新生态

发布了47 篇原创文章 · 获赞 6 · 访问量 2285

猜你喜欢

转载自blog.csdn.net/weixin_45675097/article/details/104440346
UPC