UVA - 11572 Unique Snowflakes(唯一的雪花) : 滑动窗口

题目点此跳转

思路

 题目意思是输入一个长度为n(n≤106)的序列A,找到一个尽量长的连续子序列 ALAR ,使得该序列中没有相同的元素。

 首先定义L和R表示要找的结果的左右端点,一开始L = R = 0;
 然后我们不断地增加R,直到不能增加了为止,不能增加是因为再增加的话就有相同的元素了;
 这个时候我们维护一下最大值,然后开始增加L,直到将那个使R不能再增加的元素去掉为止;
 接着我们就又能继续增加R了,直到R到序列尾。

 我们会有一个直观的感觉, 这里的[L, R] 就像一个窗口一样, 一开始在最左边,然后不断地向右边滑动, 而且不会滑回来,因为R是不断增加的,L也是不断增加的。这大概就是滑动数组了吧。

 还有一点,我们可以用map来预处理出每一位元素上一次出现的位置, 这样L往后跳的时候就不用一位一位地找了。

代码

#include <algorithm>
#include <iostream>
#include <sstream>
#include <utility>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#define met(a,b) memset(a, b, sizeof(a));
#define IN freopen("in.txt", "r", stdin);
#define OT freopen("out.txt", "w", stdout);
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int maxn = 1e6 + 100;
const int INF = 0x7fffffff;
const int dir[5][2] = {0,0,-1,0,1,0,0,-1,0,1};
const LL MOD = 1e9 + 7;

int n, a[maxn];
int pre[maxn];
map<int, int> m;

int main() {
    #ifdef _LOCAL
    IN; //OT;
    #endif // _LOCAL

    int t; cin >> t;
    while(t--) {
        scanf("%d", &n); m.clear();
        for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
        for(int i = 1; i <= n; ++i) {
            if(!m.count(a[i])) pre[i] = -1;
            else pre[i] = m[a[i]];
            m[a[i]] = i;
        }
        int L = 1, R = 1, ans = 0;
        while(R <= n) {
            while(R+1 <= n && pre[R+1] < L) ++R;
            ans = max(ans, R-L+1); ++R;
            if(R > n) break;
            L = pre[R]+1;
        }
        printf("%d\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现雪花飘落特效可以使用opencv-python库。下面是一个简单的实现方法: 1. 导入所需的库: ```python import cv2 import numpy as np ``` 2. 创建一个空白图像作为背景: ```python width, height = 800, 600 # 设置背景尺寸 background = np.zeros((height, width, 3), dtype=np.uint8) # 创建一个黑色背景图像 ``` 3. 创建一组雪花图像: ```python num_snowflakes = 100 # 雪花数量 snowflakes = [] for _ in range(num_snowflakes): center = (np.random.randint(0, width), np.random.randint(0, height)) # 随机设置雪花中心点 radius = np.random.randint(3, 8) # 随机设置雪花半径 snowflake = cv2.circle(np.zeros_like(background), center, radius, (255, 255, 255), -1) # 创建雪花图像 snowflakes.append(snowflake) ``` 4. 开始雪花飘落效果循环: ```python while True: for snowflake in snowflakes: # 随机设置雪花的飘落速度和方向 x_speed = np.random.randint(-5, 5) y_speed = np.random.randint(1, 5) # 更新雪花的位置 x, y = np.where(snowflake[:, :, 0] == 255) # 获得雪花的位置 snowflake[y, x] = [0, 0, 0] # 清空之前的位置 snowflake[y + y_speed, x + x_speed] = [255, 255, 255] # 更新位置 # 将雪花放置在背景上 background = cv2.bitwise_or(background, snowflake) # 显示背景图像 cv2.imshow("Snowfall Effect", background) if cv2.waitKey(30) == ord('q'): break cv2.destroyAllWindows() ``` 通过以上代码,我们可以实现一个简单的雪花飘落特效。需要注意的是,以上代码只是一种简单实现方法,你可以根据自己的需求进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值