hdu4908(中位数)
题意:给一个序列,里面是1~N的排列,给出m,问以m为中位数的奇数长度的序列个数。
分析:先找出m的位置,再记录左边比m大的状态,记录右边比m大的状态,使得左右两边状态平衡(和为0)就是满足的序列。
举例:
7 4
1 5 4 2 6 3 7
ans=8
m的位置pos=3:0
左边:0 1
右边:-1 0 -1 0
那么左边的0可以和右边的两个0组合(<1 5 4 2 4>,<1 5 4 2 6 3 7>).
左边的1和右边的两个-1组合(<5 4 2>,<5 4 2 6 3>).
中间pos可以和左右两边为0的组合还有自己本身(<1 5 4>,<4 2 6>,<4 2 6 3 7>,<4>)
因此总共8个。
5 3
1 2 3 4 5
ans=3
3的位置pos=3:0
左边:-2 -1
右边:1 2
左边-1可以和右边1可以组合(<2 3 4>)
左边-2和可以右边-2组合(<1 2 3 4 5>).
加上自己本身,因此ans=3.
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 60010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline int read()
{
char ch=getchar();int x=,f=;
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch<=''&&ch>=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,pos;
int a[N<<],c[N<<];
int main()
{
while(scanf("%d%d",&n,&m)>)
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==m)pos=i;
}
int res=,ans=;
memset(c,,sizeof(c));
for(int i=pos-;i>=;i--)
{
if(a[i]>m)res++;
else res--;
if(res==)ans++;
c[res+n]++;//记录某种状态数量
}
res=;
for(int i=pos+;i<=n;i++)
{
if(a[i]>m)res++;
else res--;
if(res==)ans++;
ans+=c[n-res];//加上左边记录下来的相反状态之和
}
printf("%d\n",ans);
}
}
hdu4908(中位数)的更多相关文章
- hdu4908 中位数子串
题意: 给你N个数字组成的数列,然后问你这里面有多少个是以M为中位数的子序列. 思路: 首先分四中简单的情况求 (1) 就是只有他自己的那种情况 那么sum+1 ...
- [LeetCode] Find Median from Data Stream 找出数据流的中位数
Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...
- [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- BZOJ1303 [CQOI2009]中位数图
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- 在MySQL中,如何计算一组数据的中位数?
要得到一组数据的中位数(例如某个地区或某家公司的收入中位数),我们首先要将这一任务细分为3个小任务: 将数据排序,并给每一行数据给出其在所有数据中的排名. 找出中位数的排名数字. 找出中间排名对应的值 ...
- AC日记——中位数 洛谷 P1168
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[2], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
- LeetCode 4 Median of Two Sorted Arrays 查找中位数,排除法,问题拓展 难度:1
思路:设现在可用区间在nums1是[s1,t1),nums2:[s2,t2) 1.当一个数组可用区间为0的时候,由于另一个数组是已经排过序的,所以直接可得 当要取的是最小值或最大值时,也直接可得 2. ...
- BZOJ 1303 CQOI2009 中位数图 水题
1303: [CQOI2009]中位数图 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2340 Solved: 1464[Submit][Statu ...
随机推荐
- VB.net数据库编程(03):一个SQLserver连接查询的简单样例
这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System. ...
- How to find variable is empty in shell script
(1). var="" if [ -n "$var" ]; then echo "not empty" else echo ...
- Silverlight技术调查(4)——完成的调查结果
原文 Silverlight技术调查(4)——完成的调查结果 客户端使用Silverlight+DXperience,可以在线编辑各种常见文本及富文本文档(doc.docx.rtf.txt.html… ...
- Unknown database 'DB_NAME'
Cannot create PoolableConnectionFactory (Unknown database 'DB_NAME'): com.mysql.jdbc.exceptions.jdbc ...
- for循环遍历字符串的还有一种方法
遍历字符c,让它各自等于字符串数组chars里面的各个字符.然后运行以下的语句,当c被赋值为chars里面全部字符各一次后.就会退出这个循环. 通常我们遍历字符串数组用 for(int i=0;i&l ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- UML之九图概述
最近看了UML的九种图的讲解,这九种图在我们以后的学习中起着举足轻重的作用,不管是在写文档,还是在对系统的需求.设计进行分析时,都很重要,所以首先做一下概述,希望能和大家分享. 首先和大家展示一下我对 ...
- Eclipse 乱码问题 修改设置
1.工作空间的字符编码设置 2.对于任意的项目,可以右键选择属性,然后修改 3.对于任意一个文件,同样的操作,右键选择属性,然后配置字符编码
- 几本不错的CPU设计以及VLSI的书籍
1. Microprocessor Design Principales and Practrices with VHDL 特点:电路与VHDL一一对应,比较清楚,而且还用MAX+plus进行仿真 ...
- Java字符串找出4个字节长度的字符
不解释,直接上代码: 由于Iteye代码贴四个字节的UTF-8字符出错,特能图的方式发布几个特殊字符: public class Byte4Check { public static void m ...