Codeforces Round #687 (Div. 1) Repainting Street (暴力)

There is a street with n houses in a line, numbered from 1 to n. The house i is initially painted in color ci. The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom’s painting capacity is defined by an integer, let’s call it k.

On one day, Tom can do the following repainting process that consists of two steps:

He chooses two integers l and r such that 1≤l≤r≤n and r−l+1=k.
For each house i such that l≤i≤r, he can either repaint it with any color he wants, or ignore it and let it keep its current color.
Note that in the same day Tom can use different colors to repaint different houses.

Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful.

Input
The first line of input contains a single integer t (1≤t≤104), the number of test cases. Description of test cases follows.

In the first line of a test case description there are two integers n and k (1≤k≤n≤105).

The second line contains n space-separated integers. The i-th of these integers represents ci (1≤ci≤100), the color which house i is initially painted in.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
Print t lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case.

Example
inputCopy
3
10 2
1 1 2 2 1 1 2 2 2 1
7 1
1 2 3 4 5 6 7
10 3
1 3 3 3 3 1 2 1 3 3
outputCopy
3
6
2
Note
In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.

In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.

In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3.
分析:
题意是n个房子排成一排,编号1-n,它们的颜色不同,设计师每天可以选择连续的k个房子,给这些房子图上相同的颜色,问最少需要几天才能将所有的房子变成同一种颜色。

刚开始想应该是将所有房子变成本来颜色相同的房子的颜色,但是答案错误,所以不能这样考虑。
之后想把所有出现的颜色都计算一下以这种颜色为准的时间,取最小值。但是又感觉会超时,所以没敢写。但是!!!其实不会超时,所以这道题是道简单的暴力模拟。
所以以后还是先把代码打出来再想优化吧

比赛的时候想到的方法。如果不管原来的颜色,则最长时间是n/k或者是n/k+1。考虑本来的颜色,则实际作用是能使n减少,只有当选定颜色在连续k个房子的第一个,就能减少n。

比赛代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int INF = 0x3f3f3f3f;
int a[N],vis[110],b[110];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,k;
		for(int i=1;i<=100;i++)
		{
			vis[i]=0,b[i]=0;
		}
		scanf("%d%d",&n,&k);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[a[i]]++;
			if((i-vis[a[i]])%k==1)
			{
				vis[a[i]]++;
				
			}
		}
		int ans=INF,sum;
		for(int i=1;i<=100;i++)
		{
			int t=n-vis[i];
			if(t%k==0) sum=t/k;
			else sum=t/k+1;
			ans=min(ans,sum);
		}
		if(k==1)
		{
			int mx=-1;
			for(int i=1;i<=100;i++)
			{
				mx=max(mx,b[i]);
			}
			printf("%d\n",n-mx);
		}
		else printf("%d\n",ans);	
	}
	return 0;
} 

简单暴力代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const int INF = 0x3f3f3f3f;
int a[N],b[N],vis[N];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int n,k;
		for(int i=0;i<=100;i++)
		vis[i]=0;
		scanf("%d%d",&n,&k);
		int p=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			if(!vis[a[i]])
			{
				b[++p]=a[i];
				vis[a[i]]++;
			}
			else vis[a[i]]++;
		}
		int ans=INF,sum;
		for(int j=1;j<=p;j++)
		{
			int t=b[j],sum=0;
			for(int i=1;i<=n;i++)
			{
				if(a[i]!=t)
				{
					sum++;
					i=i+k-1;
				}
			}
			ans=min(ans,sum);
		}
		
		printf("%d\n",ans);
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值