The Longest Straight

Description

ZB is playing a card game where the goal is tom make straights. Each card in the deck has a number between 1 and M, inclusive. A straight is a sequence of cards with consecutive values. Values do not wrap around, so 1 does not come after M. In addition to regular cards, the deck also contains jokers. Each joker can be used as any valid number (between 1 and M, inclusive).

You will be giben N integers card[1]...card[n] referring to the cards in your hand. Jokers are represented by zeros, and other cards are represented by their values. ZB wants to know the number of cards in the longest straight that can be formed using one or more cards from his hand.

Input

The first line contains an integer T, meaning the number of the cases.

For each test case:

The first line there are two integers N and M in the first line (1 <= N, M <= 105), and the second line contains N integers card[i](0 <= card[i] <= M).

Output

For each test case, output a single integer a line -- the longest straight ZB can get.

Sample Input

2
7 11
0 6 5 3 0 10 11
8 1000
100 100 100 101 100 99 97 103

Sample Output

5
3



#include <stdio.h>  
#include <string.h>  
int main(){  
    int T;  
    int a[100001], N, M, flag[100001], joker;  
    scanf("%d", &T);  
    while(T--){  
        joker = 0;  
        memset(a, 0, sizeof(a));  
        scanf("%d %d", &N, &M);  
        for(int i = 0; i < N; i++){  
            int temp;  
            scanf("%d", &temp);  
            if(temp)  
                a[temp] = 1;  
            else
                ++joker;  
        }  
        flag[0] = 0;  
        for(int i = 1; i <= M; i++){  
            if(a[i]){  
                flag[i] = flag[i-1];  
            }  
            else{  
                flag[i] = flag[i-1] + 1;  
            }  
        }  
        int ans = 0;  
        for(int i = 0; i <= M; i++){  
            int left = i, right = M, mid;  
            while(left <= right){  
                mid = (left + right) / 2;  
                if(flag[mid] - flag[i] > joker){  
                    right = mid - 1;  
                }  
                else{  
                    left = mid + 1;  
                }  
            }  
            if(right - i > ans){  
                ans = right - i;  
            }  
        }  
        printf("%d\n", ans);  
    }  
    return 0;  
}  

以下是一个使用OpenCV进行卷纸带缠绕圆柱体的整齐性检测的示例程序: 1. 导入必要的库和模块: ```python import cv2 import numpy as np ``` 2. 加载图像并进行预处理: ```python # Load image img = cv2.imread('paper_roll.jpg') # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to remove noise blur = cv2.GaussianBlur(gray, (5, 5), 0) # Apply Canny edge detection to detect edges edges = cv2.Canny(blur, 50, 150) ``` 3. 检测圆柱体: ```python # Find contours in the edge map contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Loop through the contours for cnt in contours: # Find the smallest enclosing circle (x, y), radius = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) radius = int(radius) # Draw the circle on the original image cv2.circle(img, center, radius, (0, 255, 0), 2) ``` 4. 检测卷纸带: ```python # Apply Hough transform to detect straight lines lines = cv2.HoughLines(edges, 1, np.pi/180, 100) # Loop through the lines for line in lines: rho, theta = line[0] a, b = np.cos(theta), np.sin(theta) x0, y0 = a*rho, b*rho x1, y1 = int(x0 + 1000*(-b)), int(y0 + 1000*(a)) x2, y2 = int(x0 - 1000*(-b)), int(y0 - 1000*(a)) # Draw the lines on the original image cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) ``` 5. 判断卷纸带是否整齐: ```python # Calculate the angle between the two longest lines angle1 = np.arctan2(y2-y1, x2-x1) * 180 / np.pi angle2 = np.arctan2(y4-y3, x4-x3) * 180 / np.pi angle_diff = abs(angle1 - angle2) if angle_diff < 10: print("The paper roll is neatly wrapped.") else: print("The paper roll is not neatly wrapped.") ``` 完整代码: ```python import cv2 import numpy as np # Load image img = cv2.imread('paper_roll.jpg') # Convert to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Apply Gaussian blur to remove noise blur = cv2.GaussianBlur(gray, (5, 5), 0) # Apply Canny edge detection to detect edges edges = cv2.Canny(blur, 50, 150) # Find contours in the edge map contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Loop through the contours for cnt in contours: # Find the smallest enclosing circle (x, y), radius = cv2.minEnclosingCircle(cnt) center = (int(x), int(y)) radius = int(radius) # Draw the circle on the original image cv2.circle(img, center, radius, (0, 255, 0), 2) # Apply Hough transform to detect straight lines lines = cv2.HoughLines(edges, 1, np.pi/180, 100) # Loop through the lines for line in lines: rho, theta = line[0] a, b = np.cos(theta), np.sin(theta) x0, y0 = a*rho, b*rho x1, y1 = int(x0 + 1000*(-b)), int(y0 + 1000*(a)) x2, y2 = int(x0 - 1000*(-b)), int(y0 - 1000*(a)) # Draw the lines on the original image cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) # Calculate the angle between the two longest lines angle1 = np.arctan2(y2-y1, x2-x1) * 180 / np.pi angle2 = np.arctan2(y4-y3, x4-x3) * 180 / np.pi angle_diff = abs(angle1 - angle2) if angle_diff < 10: print("The paper roll is neatly wrapped.") else: print("The paper roll is not neatly wrapped.") # Show the image cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值