F - Tournament(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

F - Tournament(The 2018 ACM-ICPC Asia Qingdao Regional Contest)(找规律+构造)

Time limit:1000 ms
Memory limit:65536 kB
judge:
ZOJ - 4063
vjudge

Description

DreamGrid, the king of Gridland, is making a knight tournament. There are n n n knights, numbered from 1 to n n n, participating in the tournament. The rules of the tournament are listed as follows:

The tournament consists of k k k rounds. Each round consists of several duels. Each duel happens between exactly two knights.
Each knight must participate in exactly one duel during each round.
For each pair of knights, there can be at most one duel between them during all the k k k rounds.
Let 1 ≤ i , j ≤ k 1 \le i, j \le k 1i,jk, i ≠ j i \ne j i=j, and 1 ≤ a , b , c , d ≤ n 1 \le a, b, c, d \le n 1a,b,c,dn, a , b , c , d a, b, c, d a,b,c,d be four distinct integers. If
Knight a a a fights against knight b b b during round i i i, and
Knight c c c fights against knight d d d during round i i i, and
Knight a a a fights against knight c c c during round j j j,
then knight b b b must fight against knight d d d during round j j j.
As DreamGrid’s general, you are asked to write a program to arrange all the duels in all the k k k rounds, so that the resulting arrangement satisfies the rules above.

Input

There are multiple test cases. The first line of the input is an integer T T T, indicating the number of test cases. For each test case:

The first and only line contains two integers n n n and k k k ( 1 ≤ n , k ≤ 1000 1 \le n, k \le 1000 1n,k1000), indicating the number of knights participating in the tournament and the number of rounds.

It’s guaranteed that neither the sum of n n n nor the sum of k k k in all test cases will exceed 5000.

Output

For each test case:

If it’s possible to make a valid arrangement, output k k k lines. On the i i i-th line, output n n n integers c i , 1 , c i , 2 , … , c i , n c_{i, 1}, c_{i, 2}, \dots, c_{i, n} ci,1,ci,2,,ci,n separated by one space, indicating that in the i i i-th round, knight j j j will fight against knight c i , j c_{i, j} ci,j for all 1 ≤ j ≤ n 1 \le j \le n 1jn.

If there are multiple valid answers, output the lexicographically smallest answer.

Consider two answers A A A and B B B, let’s denote a i , j a_{i, j} ai,j as the j j j-th integer on the i i i-th line in answer A A A, and b i , j b_{i, j} bi,j as the j j j-th integer on the i i i-th line in answer B B B. Answer A A A is lexicographically smaller than answer B B B, if there exists two integers p p p ( 1 ≤ p ≤ k 1 \le p \le k 1pk) and q q q ( 1 ≤ q ≤ n 1 \le q \le n 1qn), such that

for all 1 ≤ i < p 1 \le i < p 1i<p and 1 ≤ j ≤ n 1 \le j \le n 1jn, a i , j = b i , j a_{i, j} = b_{i, j} ai,j=bi,j, and
for all 1 ≤ j < q 1 \le j < q 1j<q, a p , j = b p , j a_{p, j} = b_{p, j} ap,j=bp,j, and finally a p , q < b p , q a_{p, q} < b_{p, q} ap,q<bp,q.
If it’s impossible to make a valid arrangement, output “Impossible” (without quotes) in one line.
Please, DO NOT output extra spaces at the end of each line, or your answer may be considered incorrect!

Sample Input

2
3 1
4 3

Sample Output

Impossible
2 1 4 3
3 4 1 2
4 3 2 1

题意

n n n 个骑士要参加 k k k 场比赛,每回合他们两两对决,对决过的两个骑士之后不能再次对决。但是这里有个要求:如果再某回合 A A A 对决 B B B C C C 对决 D D D ,那么当某回合A对决 C ( D ) C(D) CD 时, B B B 要对决 D ( C ) D(C) DC

如果能安排比赛,就输出对决安排,否则就输出Impossible

注意,输出的对决安排需要是字典序最小的。

题解

这道题需要找规律。下面给出n为8的表:

在这里插入图片描述
仔细观察可以发现发生调换的区间宽度刚好是 i i i l o w b i t ( i ) ∗ 2 lowbit(i) * 2 lowbit(i)2
在这里插入图片描述
而且发现每次调换都是区间内对称的,这样就不难写出代码了。

而一个 n n n 可以构造出的全部的组合是 l o w b i t ( n ) − 1 lowbit(n)-1 lowbit(n)1 ,如果所给的 k k k 大于这个值,就意味着这么多的安排是不存在的。

代码

#include <bits/stdc++.h>
#define maxn 1005
#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define sc(x) scanf("%d", &x)
#define lowerbit(x) (x & (-x))
using namespace std;

int T, n, k, a[maxn];

void sol() {
	if (k > lowerbit(n) - 1) {
		printf("Impossible\n");
		return;
	}
	_rep(i, 1, n) a[i] = i;
	for (int i = 1; i <= k; ++i) {
		int len = lowerbit(i);
		for (int j = 1; j <= n; j += (len << 1)) {
			_for(l, len) {
				swap(a[j + l], a[j + len * 2 - 1 - l]);
			}
		}
		_rep(j, 1, n) printf("%d%s", a[j], j == n ? "\n" : " ");
	}
}

int main() {
	while (cin >> T) {
		_for(i, T) {
			sc(n), sc(k);
			sol();
		}
	}
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that! Here's one way you could implement a genetic algorithm to find the maximum value of the given function: 1. Define the fitness function: In this case, our fitness function will simply be the value of the function f(x) for a given value of x. 2. Generate an initial population: Create a set of random values of x within the range [-2, 2]. These values will be used as the starting point for the genetic algorithm. 3. Evaluate fitness: For each member of the population, calculate the fitness by evaluating the fitness function (i.e. calculate the value of f(x) for the given value of x). 4. Selection: Select the fittest individuals from the population to be the parents of the next generation. One simple way to do this is to use tournament selection: randomly select a few individuals from the population and choose the fittest one as a parent. Repeat this process until you have selected enough parents to create the next generation. 5. Crossover: Create new individuals for the next generation by combining the genes (i.e. values of x) of the selected parents. One common way to do this is to use single-point crossover: choose a random point in the genes and swap the genes from that point onwards between the two parents. 6. Mutation: Introduce random mutations into the genes of the new individuals to increase genetic diversity. One simple way to do this is to randomly select a gene and replace it with a new random value within the range [-2, 2]. 7. Repeat: Repeat steps 3-6 until a stopping criteria is met (e.g. a maximum number of generations is reached, the fitness of the best individual reaches a certain threshold, etc.). 8. Output: Once the genetic algorithm has completed, output the best individual (i.e. the one with the highest fitness) and its corresponding value of x. Here's some sample Python code to implement the genetic algorithm: ```python import random import math # Define the fitness function def fitness(x): return 0.4 + math.sin(4*x)/4 + 1.1*math.sin(4*x+2)/4.4 + \ 0.8*math.sin(x-2)/2.4 + 0.7*math.sin(6*x-4)/2.8 # Generate an initial population POPULATION_SIZE = 100 population = [random.uniform(-2, 2) for _ in range(POPULATION_SIZE)] # Genetic algorithm parameters NUM_GENERATIONS = 1000 TOURNAMENT_SIZE = 5 MUTATION_RATE = 0.1 # Main loop for generation in range(NUM_GENERATIONS): # Evaluate fitness fitness_scores = [fitness(x) for x in population] # Selection parents = [] for _ in range(POPULATION_SIZE): tournament = random.sample(range(POPULATION_SIZE), TOURNAMENT_SIZE) winner = max(tournament, key=lambda i: fitness_scores[i]) parents.append(population[winner]) # Crossover new_population = [] for i in range(0, POPULATION_SIZE, 2): parent1 = parents[i] parent2 = parents[i+1] crossover_point = random.randint(0, POPULATION_SIZE-1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] new_population.extend([child1, child2]) # Mutation for i in range(POPULATION_SIZE): if random.random() < MUTATION_RATE: gene_index = random.randint(0, POPULATION_SIZE-1) new_population[i][gene_index] = random.uniform(-2, 2) # Replace old population with new population population = new_population # Output best individual best_fitness = max(fitness_scores) best_index = fitness_scores.index(best_fitness) best_individual = population[best_index] print("Best individual found:", best_individual) print("Fitness:", best_fitness) ``` This code should give you an idea of how to implement a genetic algorithm to solve this problem. Note that the genetic algorithm is not guaranteed to find the global maximum of the function, as it may get stuck in a local optimum. Therefore, it's a good idea to run the algorithm multiple times with different random seeds to increase the chance of finding the global maximum.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值