CF Dima and To-do List
1 second
256 megabytes
standard input
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 6 tasks 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.
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.
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.
6 2
3 2 1 6 5 4
1
10 5
1 3 5 7 9 9 4 1 8 5
3
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.
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int a[];
void solve(int n,int m)
{
int i,j,k,Min=,wz=;
for(i=;i<=m;i++)
{
for(j=i,k=;j<=n;j=j+m)
k=k+a[j];
if(Min>k)
{
Min=k;
wz=i;
}
}
printf("%d\n",wz);
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
if(m==)
{
printf("1\n");
continue;
}
solve(n,m);
}
return ;
}
CF Dima and To-do List的更多相关文章
- CF Dima and Salad 01背包
C. Dima and Salad time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- cf C. Inna and Dima
http://codeforces.com/contest/374/problem/C 记忆化搜索,题意:求按照要求可以记过名字多少次,如果次数为无穷大,输出Poor Inna!,如果不经过一次输出P ...
- cf D. Dima and Trap Graph
http://codeforces.com/contest/366/problem/D 遍历下界,然后用二分求上界,然后用dfs去判断是否可以. #include <cstdio> #in ...
- cf 366D D. Dima and Trap Graph (计算所有线段共同覆盖的某段区间)
http://codeforces.com/problemset/problem/366/D 题意:给出n个点,m条边,a,b,ll,rr分别代表点a,点b相连,点a和点b的区间范围(ll,rr),然 ...
- CF#214 C. Dima and Salad 01背包变形
C. Dima and Salad 题意 有n种水果,第i个水果有一个美味度ai和能量值bi,现在要选择部分水果做沙拉,假如此时选择了m个水果,要保证\(\frac{\sum_{i=1}^ma_i}{ ...
- CF 366E Dima and Magic Guitar(最远哈密顿距离)
题目链接:http://codeforces.com/problemset/problem/366/E 题意:给出一个n*m的数字矩阵A,每个矩阵元素的范围[1,K].给出一个长度为s的数字串B,B的 ...
- cf E. Dima and Magic Guitar
http://codeforces.com/contest/366/problem/E |x1-x2|+|y1-y2|有四种情况 1.-(x1-x2)+(y1-y2); 2.(x1-x2)-(y1-y ...
- cf C. Dima and Salad
http://codeforces.com/contest/366/problem/C 转化为背包问题,可以将a[i]-b[i]*k看成重量,a[i]为价值: 因为a[i]-b[i]*k可以为负数,所 ...
- cf B. Dima and To-do List
http://codeforces.com/contest/366/problem/B 从0到k枚举起点,然后i+k判断是不是i+k>=n如果是i=(i+k)%n;否则i=i+k; #inclu ...
随机推荐
- [AIR] AS3读取txt文档
package { import flash.display.Sprite; import flash.events.Event; import flash.filesystem.File; impo ...
- 重置root密码
一. ubuntu忘记密码解决方法 1. 开机按shift 2. 选择高级选项,进入后选择恢复模式(不要按回车),按e进入编辑模式 3. 修改linux命令中的recovery nomodest 为 ...
- miniui表单验证守则总结
1,页面效果图 2,代码实现 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- 面试笔试(C++部分)
1.define 和const,inline的区别 define的缺点: 1.边界效应(必须加括号,才能避免边界效应) #define MUL(A,B) A*B 而在使用的时候,这样的调用: ,b=, ...
- leetcode-812-Largest Triangle Area
题目描述: You have a list of points in the plane. Return the area of the largest triangle that can be fo ...
- leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)
题目描述: You are given a string representing an attendance record for a student. The record only contai ...
- 转一个财务方面常用到的数字金额转成汉字大写金额 php类
系统里有牵扯到财务.合同等方面的处理时,常常需要把数字金额转成汉字大写金额(貌似这样正规),转一个转换的php class吧!<?php// 诸海加(ALPHA .z)// 2000-7-19 ...
- 大数据-hive安装
1.下载Hive需要的版本 我们选用的是hive-3.1.0 将下载下来的hive压缩文件放到/opt/workspace/下 2.解压hive-3.1.0.tar.gz文件 [root@master ...
- mfix中更改time dependent VTK filename的最大时间步数的容量
默认是0000四位,有可能保存文件多了以后不够用,可以在源码中修改,修改以后效果: 源码位置: 把 I4. 改成 I5.
- MyEclipse配置,每次打开server中都没有weblogic
最近在myeclipse新配了个weblogic,结果每次打开myeclipse在server中都看不到weblogic,得重新去配置页面走一遭才能出现.很麻烦. 后来在网上找了找,找到一个办法: 在 ...