第一周题单
E - K-skip Permutation (CodeForces - 103117K)
For a permutation P = p 1 , p 2 , ⋯ , p n P=p_1,p_2,⋯,p_n P=p1,p2,⋯,pn of n n n, let f ( P , k ) f(P,k) f(P,k) be the number of i i i satisfying 1 ≤ i < n 1≤i<n 1≤i<n and p i + k = p i + 1 p_i+k=p_{i+1} pi+k=pi+1.
Given two integers n n n and k k k, your task is to find a permutation P P P of n n n such that f ( P , k ) f(P,k) f(P,k) is maximized.
Recall that in a permutation of n n n, each integer from 1 1 1 to n n n (both inclusive) appears exactly once.
Input
There is only one test case in each test file.
The first and only line contains two integers n n n and k ( 1 ≤ n , k ≤ 1 0 6 ) k(1≤n,k≤10^6) k(1≤n,k≤106).
Output
Output one line containing n n n integers indicating a permutation P P P of n n n that maximizes f ( P , k ) f(P,k) f(P,k). If there are multiple valid answers you can output any of them.
Please, DO NOT output extra spaces at the end of the line, or your answer may be considered incorrect!
Sample1
Input
3 1
Output
1 2 3
Sample2
Input
7 3
Output
2 5 1 4 7 3 6
Sample3
Input
3 7
Output
1 3 2
题目大意
输出数列一个的数列 P = p 1 , p 2 , ⋯ , p n P=p_1,p_2,⋯,p_n P=p1,p2,⋯,pn,数列中的数满足 p i + k = p i + 1 ( 1 ≤ i < n ) p_i+k=p_{i+1}(1≤i<n) pi+k=pi+1(1≤i<n),数列中每个数的大小都在 1 1 1到 n n n之间
题解过程
一开始看不懂题目什么意思,直接从输入输出提示还有样例入手,其实就是从 1 1 1开始,先输出 1 , 1 + k , 1 + 2 k , . . . 1,1+k,1+2k,... 1,1+k,1+2k,...,然后输出 2 , 2 + k , 2 + 2 k , . . . 2,2+k,2+2k,... 2,2+k,2+2k,...,以此类推,直到输出完 1 1 1到 n n n的所有数,比如别忘记最后一位数后面不能加空格,所以用一个计数器记一下数
代码部分(cpp)#include <iostream>
using namespace std;
void solution()
{
int n, k;
cin >> n >> k;
int num = 0;
for (int i = 1; i <= k; i++)
{
for (int j = i; j <= n; j += k)
{
num++;
if (num == n)
cout << j;
else
cout << j << " ";
}
}
}
int main(int argc, const char **argv)
{
solution();
return 0;
}
刚开始学习,欢迎指正