[POJ 2299]Ultra-QuickSort

Description

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,

Ultra-QuickSort produces the output
0 1 4 5 9 .

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 – the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source

Waterloo local 2005.02.05

tips:题面右边有个马桶塞不知道想表达什么。

简述题意,求一个数列的逆序对数量。多组数据,每组数据第一行一个n,接下来n行是这个数列。

题解:
由于a[i]很大,所以我们就不能用树状数组了。我们就得用归并排序来统计逆序对数量。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
#define LiangJiaJun main
#define eps 1e-9
using namespace std;
int a[500004],temp[500004],n;
ll ans=0;
int combi(int l,int m,int r){
    int i=l,j=m+1,k=l;
    while(i<=m&&j<=r){
        if(a[i]>a[j]){
            ans+=(m-i+1);
            temp[k++]=a[j++];
        }
        else temp[k++]=a[i++];
    }
    while(i<=m)temp[k++]=a[i++];
    while(j<=r)temp[k++]=a[j++];
    for(i=l;i<=r;i++)a[i]=temp[i];
    return 0;
}
int erfen(int l,int r){
    if(r<=l)return 0;
    int mid=(l+r)>>1;
    erfen(l,mid);
    erfen(mid+1,r);
    combi(l,mid,r);
}
int LiangJiaJun(){
    while(scanf("%d",&n)!=EOF){
        ans=0;
        if(n == 0)break;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        erfen(1,n);
        printf("%lld\n",ans);
    }
    return 0;
}
POJ 3213 题目是一个关于矩阵乘法的经典计算机科学问题。矩阵乘法通常是线性代数的基础操作,给定两个矩阵 A 和 B,你需要计算它们的乘积 C = A * B,其中每个元素 C[i][j] 是对应位置上 A 的行向量与 B 的列向量的点积。 以下是一个简单的 Java 代码示例,使用嵌套循环来实现矩阵乘法: ```java import java.util.Scanner; public class MatrixMultiplication { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 输入矩阵的维度 System.out.println("Enter the dimensions of matrix A (m x n):"); int m = scanner.nextInt(); int n = scanner.nextInt(); // 创建矩阵 A 和 B int[][] matrixA = new int[m][n]; int[][] matrixB = new int[n][n]; // 读取矩阵 A 的元素 System.out.println("Enter elements of matrix A:"); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { matrixA[i][j] = scanner.nextInt(); } } // 读取矩阵 B 的元素(假设输入的矩阵都是方阵,大小为 n x n) System.out.println("Enter elements of matrix B:"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { matrixB[i][j] = scanner.nextInt(); } } // 矩阵乘法 int[][] result = new int[m][n]; // 结果矩阵 for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { // 每次循环k用于遍历B的列 result[i][j] += matrixA[i][k] * matrixB[k][j]; } } } // 输出结果矩阵 System.out.println("Matrix multiplication result:"); for (int[] row : result) { for (int element : row) { System.out.print(element + " "); } System.out.println(); } scanner.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值