6058 Kanade's sum

Kanade's sum

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1789    Accepted Submission(s): 728


Problem Description
Give you an array A[1..n] of length n .

Let f(l,r,k) be the k-th largest element of A[l..r] .

Specially , f(l,r,k)=0 if rl+1<k .

Give you k , you need to calculate nl=1nr=lf(l,r,k)

There are T test cases.

1T10

kmin(n,80)

A[1..n] is a permutation of [1..n]

n5105
 

Input
There is only one integer T on first line.

For each test case,there are only two integers n , k on first line,and the second line consists of n integers which means the array A[1..n]
 

Output
For each test case,output an integer, which means the answer.
 

Sample Input
  
1 5 2 1 2 3 4 5
 

Sample Output
  
30
 

Source

2017 Multi-University Training Contest - Team 3 

自己做的时候想不通 看了别人的题解 怎么人家这么聪明呢qaq 

直通车:http://blog.youkuaiyun.com/yjf3151731373/article/details/76559550


题意:A 中所有区间内第K大的数求和

分析:遍历A中所有元素 令当前元素为第K大 往右找 上限 再往左找下限 具体看代码qaq


AC:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
typedef long long LL;
using namespace std;
const int N=1e6;
const LL mod=1e9+7;
int a[N]={0};
int b[N]={0};
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
       int n,k;scanf("%d%d",&n,&k);
       for(int i=1;i<=n;i++)scanf("%d",&a[i]);
       LL ans=0;
       //当前为第i大;
       for(int i=1;i<=n;i++)
       {
           int x=a[i],num=0,d=0,j;
           LL sum=0;

           b[++d]=i;//记位置
           for(j=i+1;j<=n&&d<k;j++)if(a[j]>x)b[++d]=j;

           if(d==k)//右边存a[j]>x使x成为第k大的数
           {
                num=1; //b当前区间算一个区间
                //算后面比它小的数量
                for(;j<=n;j++){
                    if(a[j]<x)num++;
                    else break;
                }

                sum+=num;
                //算前面比它小的数和比它大的数(前遇到一个比它大的数 后面比它大的数前进一个)

                for(j=i-1;j>=1&&d>=1;j--)
                {
                    if(a[j]<x)sum+=num;  //因为x是第二大的数区间不能跑到x左边 所以加上x右边的num
                    else
                    {
                        if(d==1)break;//b[1]就是本身 此时在右边界 不能左移了;

                        num=b[d]-b[d-1]; //右边比它大的数去掉一个区间
                        d--;
                        sum+=num;
                    }
                }
           }
           else{ //右边不存在
                for(j=i-1;j>=1&&d<k;j--)if(a[j]>x)b[++d]=j;
                if(d==k)
                {
                    sort(b+1,b+d+1);//
                    num=n-b[d]+1; //n-最大位置+1 = 右边比它大的最后一个位置右边比它小(a[j]<x)的数量;
                    sum+=num;
                    for(;j>=1&&b[d]>=i;j--)
                    {
                        if(a[j]<x)sum+=num;
                        else
                        {
                            if(b[d]==i)break;// i为右边界
                            num=abs(b[d]-b[d-1]);
                            d--;
                            sum+=num;
                        }
                    }
                }
           }
           ans+=x*sum;//sum为区间数
       }
       printf("%lld\n",ans);
    }
    return 0;
}


好的,以下是一个简单的示例代码,仅供参考: ```python import cv2 import numpy as np # 读取视频 cap = cv2.VideoCapture('video.mp4') # 设置参数 params = cv2.SimpleBlobDetector_Params() params.filterByArea = True params.minArea = 50 params.maxArea = 5000 detector = cv2.SimpleBlobDetector_create(params) lk_params = dict(winSize=(15, 15), maxLevel=4, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # 初始化变量 prev_gray = None prev_pts = None cur_pts = None displacements = [] # 循环处理帧 while True: ret, frame = cap.read() if not ret: break # 转换为灰度图 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测圆心标靶 keypoints = detector.detect(gray) # 提取测点位置 cur_pts = np.float32([kp.pt for kp in keypoints]).reshape(-1, 1, 2) if prev_gray is not None and prev_pts is not None and cur_pts.shape[0] > 0: # 计算光流 cur_pts, status, err = cv2.calcOpticalFlowPyrLK(prev_gray, gray, prev_pts, None, **lk_params) # 筛选有效点 good_pts = cur_pts[status == 1] good_prev_pts = prev_pts[status == 1] if good_pts.shape[0] > 0: # 计算位移 disp = good_pts - good_prev_pts displacements.append(disp) # 绘制轨迹 for i, pt in enumerate(good_pts): x, y = pt.ravel() cv2.circle(frame, (x, y), 3, (0, 255, 0), -1) # 更新变量 prev_gray = gray.copy() prev_pts = cur_pts # 显示结果 cv2.imshow('frame', frame) if cv2.waitKey(1) == ord('q'): break # 计算位移曲线 displacements = np.array(displacements) displacements = np.mean(displacements, axis=1) displacements = np.sqrt(np.sum(displacements ** 2, axis=1)) time = np.arange(displacements.shape[0]) / cap.get(cv2.CAP_PROP_FPS) # 绘制位移曲线 import matplotlib.pyplot as plt plt.plot(time, displacements) plt.xlabel('Time (s)') plt.ylabel('Displacement (pixels)') plt.show() # 释放资源 cap.release() cv2.destroyAllWindows() ``` 需要注意的是,这只是一个简单的示例代码,要想实现更准确和稳定的测点跟踪,需要对参数进行调整和优化,还需要考虑到光照变化、遮挡、噪声等因素的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值