CodeForces - 244A C - Dividing Orange(STL ---- set)

CodeForces244A Dividing Orange(STL+思维)

One day Ms Swan bought an orange in a shop. The orange consisted of n·k segments, numbered with integers from 1 to n·k.

There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided to divide it between them. For that each child took a piece of paper and wrote the number of the segment that he would like to get: the i-th (1 ≤ i ≤ k) child wrote the number ai (1 ≤ ai ≤ n·k). All numbers ai accidentally turned out to be different.

Now the children wonder, how to divide the orange so as to meet these conditions:

each child gets exactly n orange segments;
the i-th child gets the segment with number ai for sure;
no segment goes to two children simultaneously.
Help the children, divide the orange and fulfill the requirements, described above.

Input
The first line contains two integers n, k (1 ≤ n, k ≤ 30). The second line contains k space-separated integers a1, a2, …, ak (1 ≤ ai ≤ n·k), where ai is the number of the orange segment that the i-th child would like to get.

It is guaranteed that all numbers ai are distinct.

Output
Print exactly n·k distinct integers. The first n integers represent the indexes of the segments the first child will get, the second n integers represent the indexes of the segments the second child will get, and so on. Separate the printed numbers with whitespaces.

You can print a child’s segment indexes in any order. It is guaranteed that the answer always exists. If there are multiple correct answers, print any of them.

Examples
Input
2 2
4 1
Output
2 4
1 3
Input
3 1
2
Output
3 2 1

  • 题目大意:
    有n*k瓣橘子,编号分别是1–n * k,有k个孩子,每个孩子都有一个想要的橘子瓣的编号,然后 要求每个人的要求都要满足。每个人都要分得n瓣橘子,让你输出一种分配的方式。
  • 解题思路:
    每个人都有一个想要的,就把这些个被人预定的橘子都给放进 set 里面。然后从第一个人开始,先把他想要的那个给他安排上,然后在从第一个开始,往后给他安排上n-1 个 ,每次判断一下,这个是不是在set里,在俩面就跳过去,然后下一个人继续往下安排。
  • 代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
set<int>s;
int a[100];
int main()
{
	int n,k;
	cin>>n>>k;
	for(int i=1;i<=k;i++)
	{
		cin>>a[i];
		s.insert(a[i]);
	}
	int cnt=1;
	for(int i=1;i<=k;i++)
	{
		cout<<a[i];
		int num=1;
		for(int j=cnt;j<=n*k;j++)
		{
			if(s.count(j)==0)
			{
				cout<<" "<<j;
				num++;
			}
			if(num==n)
			{
				cout<<endl;
				cnt=j+1;
				break;
			}
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43179892/article/details/83796531