B. Dima and To-do List

B. Dima and To-do List
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You helped Dima to have a great weekend, but it's time to work. Naturally, Dima, as all other men who have girlfriends, does everything wrong.

Inna and Dima are now in one room. Inna tells Dima off for everything he does in her presence. After Inna tells him off for something, she goes to another room, walks there in circles muttering about how useless her sweetheart is. During that time Dima has time to peacefully complete k - 1 tasks. Then Inna returns and tells Dima off for the next task he does in her presence and goes to another room again. It continues until Dima is through with his tasks.

Overall, Dima has n tasks to do, each task has a unique number from 1 to n. Dima loves order, so he does tasks consecutively, starting from some task. For example, if Dima has 6tasks to do in total, then, if he starts from the 5-th task, the order is like that: first Dima does the 5-th task, then the 6-th one, then the 1-st one, then the 2-nd one, then the 3-rd one, then the 4-th one.

Inna tells Dima off (only lovingly and appropriately!) so often and systematically that he's very well learned the power with which she tells him off for each task. Help Dima choose the first task so that in total he gets told off with as little power as possible.

Input

The first line of the input contains two integers n, k (1 ≤ k ≤ n ≤ 105). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 103), where ai is the power Inna tells Dima off with if she is present in the room while he is doing the i-th task.

It is guaranteed that n is divisible by k.

Output

In a single line print the number of the task Dima should start with to get told off with as little power as possible. If there are multiple solutions, print the one with the minimum number of the first task to do.

Sample test(s)
input
6 2
3 2 1 6 5 4
output
1
input
10 5
1 3 5 7 9 9 4 1 8 5
output
3
Note

Explanation of the first example.

If Dima starts from the first task, Inna tells him off with power 3, then Dima can do one more task (as k = 2), then Inna tells him off for the third task with power 1, then she tells him off for the fifth task with power 5. Thus, Dima gets told off with total power 3 + 1 + 5 = 9. If Dima started from the second task, for example, then Inna would tell him off for tasks 2, 4 and 6 with power 2 + 6 + 4 = 12.

Explanation of the second example.

In the second example k = 5, thus, Dima manages to complete 4 tasks in-between the telling off sessions. Thus, Inna tells Dima off for tasks number 1 and 6 (if he starts from 1 or 6), 2 and 7 (if he starts from 2 or 7) and so on. The optimal answer is to start from task 3 or 8, 3 has a smaller number, so the answer is 3.

 

数据保证n能整除K应该让这道题好做了一些。

只要找出从前K个数中的哪个数开始每隔K个数求和结果最小,这个数的位置就是答案。

水题。

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; #define ll long long //不要用define typedef long long LL; 变量类型名称用大写方便区分这是变量还是类型 int main() { const int N = 1E5+5; // 申请全局,不要再main函数里面做这种事情 ll a[N],sum,mmin=88889999999; // mmin 这种类似inf的东西,也要const int n,k,ans; scanf("%d %d",&n,&k); for (int i = 1;i <= n;i++) scanf("%I64d",&a[i]); int tmp; // 缩进有问题 for (int i = 1;i <= k;i++) // for ( int i = 1 ; i <= k ; i++ ) { tmp = i; sum=0; while (tmp<=n) // while ( tmp <= n ) { sum = sum + a[tmp]; tmp = tmp + k; } if (sum<mmin) // if ( sum < mmin ) { mmin=sum; ans=i; } } printf("%d",ans); return 0; }

 

 

 1 #include <iostream>  //author:Dawn

 2 #include <algorithm>

 3 #include <cstring>

 4 #include <cstdio>

 5 

 6 using namespace std;

 7 typedef long long LL;

 8 const int N = 1e5 + 9;

 9 const LL INF = 1e15;

10 LL a[N] , sum , mmin;

11 int main()

12 {

13     int n,k,ans;

14     mmin = INF;

15     scanf("%d %d",&n,&k);

16     for (int i = 1 ; i <= n ; i++)

17         scanf("%I64d",&a[i]);

18         

19     // 一个特殊的要求,代码也是成段的,比如读入模块,计算模块等等。模块之间用空行分割,这样能让自己看的清楚让队友也能理解

20     int tmp;

21     for (int i = 1 ; i <= k ; i++)  

22     {

23         tmp = i;

24         sum = 0;

25         while ( tmp <= n )          // while ( tmp <= n )

26         {

27             sum = sum + a[tmp];

28             tmp = tmp + k;

29         }

30         if (sum < mmin)           // if ( sum < mmin )

31         {

32             mmin = sum;

33             ans = i;

34         }

35     }

36     printf("%d\n",ans);

37     return 0;

38 }

 

你可能感兴趣的:(list)