Codeforces Round #532 (Div. 2)(A. Roman and Browser)

									A. Roman and Browser
								time limit per test1 second
							 memory limit per test256 megabytes
									 inputstandard input
									outputstandard output

This morning, Roman woke up and opened the browser with n opened tabs numbered from 1 to n. There are two kinds of tabs: those with the information required for the test and those with social network sites. Roman decided that there are too many tabs open so he wants to close some of them.

He decided to accomplish this by closing every k-th (2≤k≤n−1) tab. Only then he will decide whether he wants to study for the test or to chat on the social networks. Formally, Roman will choose one tab (let its number be b) and then close all tabs with numbers c=b+i⋅k that satisfy the following condition: 1≤c≤n and i is an integer (it may be positive, negative or zero).

For example, if k=3, n=14 and Roman chooses b=8, then he will close tabs with numbers 2, 5, 8, 11 and 14.

After closing the tabs Roman will calculate the amount of remaining tabs with the information for the test (let’s denote it e) and the amount of remaining social network tabs (s). Help Roman to calculate the maximal absolute value of the difference of those values |e−s| so that it would be easy to decide what to do next.

Input
The first line contains two integers n and k (2≤k<n≤100) — the amount of tabs opened currently and the distance between the tabs closed.

The second line consists of n integers, each of them equal either to 1 or to −1. The ii-th integer denotes the type of the i-th tab: if it is equal to 1, this tab contains information for the test, and if it is equal to −1, it’s a social network tab.

Output
Output a single integer — the maximum absolute difference between the amounts of remaining tabs of different types |e−s|.

Examples

input
4 2
1 1 -1 1
output
2

input
14 3
-1 1 -1 -1 1 -1 -1 1 -1 -1 1 -1 -1 1
output
9

思路:从1到n一次搜索,每次从当前i(1<=i<=n)开始向后搜索到n,当为k时跳过忽略,其余的为1则e++,-1则s++,每个i都会求出一个|e-s|,与max比较即可,注意,i还应向前搜寻,当搜到为k的倍数个时则忽略,其余的为1则e++,-1则s++,

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
	int n,k,a[105],e=0,s=0,max=0,i,j,t;
	cin>>n>>k;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		for(j=i-1;j>=1;j--)
		{
			if((i-j)%k!=0)
			{
				if(a[j]==1)
					e++;
				else
					s++;
			}
		}
		for(j=i;j<=n;j++)
		{
			if((j-i)%k!=0)
			{
				if(a[j]==1)
					e++;
				else
					s++;
			}
		}
		t=abs(e-s);
		if(t>max)
		{
			max=t;
		}
		e=0;
		s=0;
	}
	cout<<max<<endl;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值