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;

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <algorithm>

 4 #define maxn 200010

 5 using namespace std;

 6 const int inf=1<<29;

 7 

 8 int n,k;

 9 int a[maxn];

10 bool vis[maxn];

11 

12 int main()

13 {

14     while(scanf("%d%d",&n,&k)!=EOF)

15     {

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

17         {

18             scanf("%d",&a[i]);

19         }

20         __int64 min1=inf;

21         int c;

22         for(int j=0; j<k; j++)

23         {

24             __int64 sum=(__int64)a[j];

25             int i=j;

26             while(1)

27             {

28                 if(i+k>=n)

29                 {

30                     i=(i+k)%n;

31                 }

32                 else i=i+k;

33                 if(i==j) break;

34                 sum+=a[i];

35             }

36             if(sum<min1)

37             {

38                 min1=sum;

39                 c=j;

40             }

41         }

42         printf("%d\n",c+1);

43     }

44     return 0;

45 }
View Code

 

你可能感兴趣的:(list)