写博客cf第一天~
B. Making Towers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
题目大意,给你一个序列,序列里的数告诉你有一个颜色为c_i的方块,然后让你叠塔,只能往上左右叠,不能往下叠,问你每一个颜色的塔的最高的高度是多少(可以叠多次)。
一开始没看懂题目,就看了很久才知道这个塔的每一个元素都必须相同(mad纠结了好久),然后这就是个贪心的dp问题,看久了可以发现
当前位置的元素跟它前面最近的相同元素的位置相差的数是偶数
那么可以直接加上这个元素(叠塔),如果这个不是偶数的话,我们发现,如果用这个元素来叠塔,那么前一个元素就不能叠塔,
并且因为这两个元素相差的数为奇数的话,那么跟下一个相同元素的距离的奇偶性是不变的(好好想想这点,1 2 1 4 1 为例子),所以我们可以直接放弃前一个元素,用后一个元素来叠塔。
解法:定义一个cnt数组用来存放塔的最大高度,sum数组用来存放塔的最高位置的下标
如果相差是偶数可以直接cnt++,如果为奇数就只改变下标的值(奇偶都要改)。
然后直接输出cnt的值就可以了(最后记得初始化,用memset我超时了,只好用朴素的qwq)
最后代码
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
#include<cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include<vector>
#include<queue>
#include<map>
#define ll long long
using namespace std;
const int N=1000000+100;
int n ,m,h;
ll s[N];
int cnt[N];
int sum[N];
int main()
{
int t ;
cin>>t;
while(t--)
{
int n;
scanf("%d",&n);
for(int i = 1 ; i <= n ; i++){
scanf("%d",&s[i]);
cnt[i]=sum[i]=0;
}
for(int i = 1 ; i <= n ; i++)
{
if(sum[s[i]]==0){
cnt[s[i]]++;
sum[s[i]]=i;
continue;
}
if((i-sum[s[i]]-1)%2==0)
cnt[s[i]]++;
sum[s[i]]=i;
}
for(int i = 1 ; i <= n ; i++)
{
int res=cnt[i];
printf("%d ",res);
}
puts("");
}
return 0;
}