排列 题目及pas程序

【排列】(sum.cpp/c/pas)

时间限制:1s

内存限制:256MB

【问题描述】给出一个随机的排列,请你计算最大值减最小值的差小于等于 0~n-1 的区间分别有多少个。

【输入】  输入文件名为 sum.in。第一行一个数 T(<=10),表示数据组数对于每一组数据:第一行一个数 n(1<=n<=100,000)第二行 n 个数 a1...an,表示一个随机的排列

【输出】输出文件名为 sum.out。对于每组数据输出 n 行,分别表示差值小于等于 0~n-1 的区间个数

【输入输出样例】

1

4

3 2 4 1


4

5

7

10

【数据说明】对于 30%的数据,1<=n<=300;对于 60%的数据,1<=n<=5000对于 100%的数据,1<=n<=1000003.

var
    t,n,i,j,k,l,temp:longint;
    ans,a:array[0..100000]of longint;
function max(x,y:longint):longint;
var
    i:longint;
begin
    max:=0;
    for i:=x to y do
        if a[i]>max then
            max:=a[i];
end;
function min(x,y:longint):longint;
var
    i:longint;
begin
    min:=maxlongint;
    for i:=x to y do
        if a[i]<min then
            min:=a[i];
end;
begin
    readln(t);
    for i:=1 to t do
    begin
        readln(n);
        for j:=1 to n do
            read(a[j]);
        readln;
        temp:=0;
        for j:=1 to n-1 do
            if a[j]=a[j+1] then
                inc(temp);
        ans[0]:=temp+n;


        for l:=1 to n-1 do
        begin
            temp:=0;
            for j:=1 to n do
                for k:=j+1 to n do
                    if max(j,k)=l+min(j,k) then
                        inc(temp);
            ans[l]:=ans[l-1]+temp;
        end;


    end;
    for i:=1 to t do
        for j:=0 to n-1 do
            writeln(ans[j]);
end.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值