Codeforces Round #480 (Div. 2) C. Posterized (贪心)

Codeforces Round #480 (Div. 2) C. Posterized (贪心)

思路:每次贪心地选从左到右选 a [ j ] = − 1 a[j]=-1 a[j]=1 a [ j ] = j a[j]=j a[j]=j的颜色。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define il inline
int a[256],n,k;
int main(){
	scanf("%d%d",&n,&k);
	mst(a,-1);
	for(int i=1;i<=n;i++){
		int r;scanf("%d",&r);
		if(a[r]==-1){
			int l=max(0,r-k+1);
			for(int j=l;j<=r;j++){
				if(a[j]==-1||a[j]==j){
					for(int x=j;x<=r;x++) a[x]=j;
					break;
				}
			}
		}
		printf("%d ",a[r]);
	}
	return 0;
}
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页