「BZOJ1303」[CQOI2009] 中位数图(中位数)

2024-04-16 02:38
文章标签 中位数 cqoi2009 bzoj1303

本文主要是介绍「BZOJ1303」[CQOI2009] 中位数图(中位数),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Description
给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后,位于中间的数。
Input
第一行为两个正整数n和b ,第二行为1~n 的排列。
Output
输出一个整数,即中位数为b的连续子序列个数。
Sample Input
7 4
5 7 2 4 3 1 6
Sample Output
4
Hint
第三个样例解释:{4}, {7,2,4}, {5,7,2,4,3}和{5,7,2,4,3,1,6}
N<=100000
Source

思路: 我们只需要考虑其他数字与中位数的大小关系,那么更大的赋值为1,更小的赋为-1.跑前缀和和后缀和就可以了。用了map的写法,貌似常数很大···

ACNEW

#include <cstdio>using namespace std;int sum[200005],l[200005],r[200005],a[100005];
int main()
{int n,b;scanf("%d%d",&n,&b);int pos = 1;for(int i = 1;i <= n;i++){scanf("%d",&a[i]);if(a[i] > b)a[i] = 1;else if(a[i] < b)a[i] = -1;else {a[i] = 0;pos = i;}}l[n] = 1;r[n] = 1;int ans = 0;for(int i = pos - 1;i >= 1;i--){sum[i] = sum[i + 1] + a[i];l[sum[i] + n]++;}for(int i = pos + 1;i <= n;i++){sum[i] = sum[i - 1] +a[i];r[sum[i] + n]++;}for(int i = 1;i < 2 * n;i++)ans += l[i] * r[2 * n - i];printf("%d\n",ans);return 0;
}
#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;map<int,int>mpl1,mpl2,mpr1,mpr2;
int a[100005];int main()
{int n,b;scanf("%d%d",&n,&b);int pos = 1;for(int i = 1;i <= n;i++){scanf("%d",&a[i]);if(a[i] > b)a[i] = 1;else if(a[i] < b)a[i] = -1;else{pos = i;a[i] = 0;}}int ans = 1;int tmp = 0;for(int i = pos + 1;i <= n;i++){tmp += a[i];if((i - pos) & 1){mpr1[tmp]++;}else{mpr2[tmp]++;}}tmp = 0;for(int i = pos - 1;i >= 1;i--){tmp += a[i];if((pos - i) & 1){mpl1[tmp]++;}else{mpl2[tmp]++;}}ans += mpl1[0] * mpr1[0] + mpl1[0] + mpr1[0];ans += mpl2[0] * mpr2[0] + mpl2[0] + mpr2[0];for(int i = 1;i <= n;i++){ans += mpl1[i] * mpr1[-i];ans += mpl1[-i] * mpr1[i];ans += mpl2[i] * mpr2[-i];ans += mpl2[-i] * mpr2[i];}printf("%d\n",ans);
}

这篇关于「BZOJ1303」[CQOI2009] 中位数图(中位数)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/907605

相关文章

【SGU】 114. Telecasting station 中位数

传送门:【SGU】 114. Telecasting station 题目分析:一个位置多个城市可以看成多个城市在同一位置,然后就可以求中位数了,易知最优的位置一定在中位数上。 代码如下 : #include <map>#include <vector>#include <cstdio>#include <cstring>#include <iostream>

算法/编程练习:两个有序数组的中位数

算法/编程练习:两个有序数组的中位数 题目来自LeetCode: https://leetcode-cn.com/problems/median-of-two-sorted-arrays/ 题目: 给定两个大小为 n1 和 n2 的有序(升序)数组 nums1 和 nums2 ,找出这两个有序数组的中位数mid。要求算法的时间复杂度为 O(log(m + n))。例如,输入: nu

SQL进阶技巧:给定数字的频率查询中位数 | 中位值计算问题

目录 0 需求描述 1 数据准备 2 问题分析 方法1:按照频率将num值展开,转换成明细表,利用中位值公式 求解      abs(rn - (cnt+1)/2) < 1  方法2:中位值定义 3 小结 0 需求描述 num表: Column NameTypenumintfrequencyint num 是这张表的主键(具有唯一值的列)。这张表的每一行表示某个数字

算法--------------------寻找两个有序数组的中位数

题目描述 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。示例 1:nums1 = [1, 3]nums2 = [2]则中位数是 2.0示例 2:nums1 = [1, 2]nums2 = [3, 4]则中位数是 (2 + 3)/

leetcode 4:两个排序数组的中位数

寻找中位数,当m+n是奇数时,中位数为第(m+n+1)/2个数,当m+n为偶数时,中位数为第(m+n+1)/2和(m+n+2)/2的平均数 根据整数的取整,我们可以统一为中位数为第(m+n+1)/2和(m+n+2)/2的平均数。 如何使用二分法求两个有序数组的第k个数 首先将数组A和数组B分为left_A(下标为0~k/2-1),right_A,left_B(下标为0~k/2-1),ri

NumPy(五):数组统计【平均值:mean()、最大值:max()、最小值:min()、标准差:std()、方差:var()、中位数:median()】【axis=0:按列运算;axis=0:按列】

统计运算 np.max()np.min()np.median()np.mean()np.std()np.var()np.argmax(axis=) — 最大元素对应的下标np.argmin(axis=) — 最小元素对应的下标 NumPy提供了一个N维数组类型ndarray,它描述了 相同类型 的“items”的集合。(NumPy provides an N-dimensional array

opencv中求图像像素值中位数

话不多说,直接上源码: int GetMidValue(Mat& input){int rows = input.rows;int cols = input.cols;float histogram[256] = { 0 };//先计算图像的直方图for (int i = 0; i < rows; ++i){///获取i行首像素的指针const uchar *p = input.ptr<uch

查找有序二维数组的中位数

题目:给定 n×n 的实数矩阵,每行和每列都是递增的,求这 n^2 个数的中位数 代码如下: package com.cb.java.algorithms.programmingmethod.search;/*** 给定 n×n 的实数矩阵,每行和每列都是递增的,求这 n^2 个数的中位数* * @author 36184**/public class SearchMedian {/***

第四题:求两个有序数组的中位数(Median of Two Sorted Arrays)

题目描述: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2,请你找出这两个有序数组的中位数。 示例: 输入:nums1 = [1, 3], nums2 = [2] 输出:2.0 输入:nums1 = [1, 2], nums2 = [3, 4] 输出:2.5 要求: 你必须在对数时间复杂度 O(log(min(m, n))) 内解决这个问题。 解题思路 二分

排序算法刷题【leetcode:04题,寻找两个正序数组的中位数。leetcode:219题,存在重复的元素 】

代码如下所示: #include <iostream>#include <vector>#include <algorithm>using namespace std;/* leetcode04题:寻找两个正序数组的中位数 */class Solution {public:double findMedianSortedArrays(vector<int>& nums1, vec