Codeforces Round #212 (Div. 2) C. Insertion Sort
2013-11-15 17:29
393 查看
这道题的题意是给你一个数字n然后再给你n个数字,他们都是0-n-1的数字。求这n个数字,再交换任意两个数字之后再进行冒泡排序需要交换数字的次数。
首先肯定不是暴力、、、一开始没做出来,是鹏哥教的我、、、、太笨了啊,不解释啊、、、
大家还是看他的吧、、、http://blog.csdn.net/rowanhaoa/article/details/16341489
C. Insertion Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the
given integer zero-indexed array a of size n in
the non-decreasing order.
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1.
He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap,
was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
Input
The first line contains a single integer n (2 ≤ n ≤ 5000)
— the length of the permutation. The second line contains n different integers from 0 to n - 1,
inclusive — the actual permutation.
Output
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that
swapping the elements of the input permutation with indexes i and j leads
to the minimum number of the executions.
Sample test(s)
input
output
input
output
Note
In the first sample the appropriate pairs are (0, 3) and (0, 4).
In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
首先肯定不是暴力、、、一开始没做出来,是鹏哥教的我、、、、太笨了啊,不解释啊、、、
大家还是看他的吧、、、http://blog.csdn.net/rowanhaoa/article/details/16341489
C. Insertion Sort
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the
given integer zero-indexed array a of size n in
the non-decreasing order.
for (int i = 1; i < n; i = i + 1) { int j = i; while (j > 0 && a[j] < a[j - 1]) { swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1] j = j - 1; } }
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from 0 to n - 1.
He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap,
was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
Input
The first line contains a single integer n (2 ≤ n ≤ 5000)
— the length of the permutation. The second line contains n different integers from 0 to n - 1,
inclusive — the actual permutation.
Output
Print two integers: the minimum number of times the swap function is executed and the number of such pairs (i, j) that
swapping the elements of the input permutation with indexes i and j leads
to the minimum number of the executions.
Sample test(s)
input
5 4 0 3 1 2
output
3 2
input
5 1 2 3 4 0
output
3 4
Note
In the first sample the appropriate pairs are (0, 3) and (0, 4).
In the second sample the appropriate pairs are (0, 4), (1, 4), (2, 4) and (3, 4).
#include <stdio.h> #include <string.h> #include <iostream> #include <cmath> #define MAX 5050 using namespace std; int num[MAX][MAX]; int dp[MAX]; int wei[MAX]; int main() { int n, i, j; cin >>n; for(i = 0; i < n; i++) { cin >>dp[i]; wei[dp[i]] = i; } for(i = 0; i < n; i++) { for(j = n-1; j >= 0; j--) { if(dp[j] < i) num[i][j] ++; num[i][j] += num[i][j+1]; } } int sum = 0; for(i = 0; i < n; i++) sum += num[i][wei[i]]; int _max = sum; int cnt; for(i = 0; i < n; i++) { for(j = 0; j < i; j++) { if(dp[i] > dp[j]) continue; int x, y, z; x = num[dp[i]][j] - num[dp[i]][i]; y = num[dp[j]][j] - num[dp[j]][i]; z = y-x; int sum1 = sum+x-z-(y+1); if(sum1 < _max) { _max = sum1; cnt = 1; } else if(sum1 == _max) { cnt++; } } } cout <<_max<<' '<<cnt<<endl; return 0; }
相关文章推荐
- Insertion Sort (Codeforces Round #212 (Div. 2))
- Codeforces Round #277.5 (Div. 2)A——SwapSort
- Codeforces Round #212 (Div. 2)(C有关逆序数)
- Codeforces Round #258 (Div. 2) B. Sort the Array (模拟)
- Codeforces Round 212 Div 2 报告(以前没写完,现在也没心情补了,先就这样吧)
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift
- Codeforces Round #212 (Div. 2)
- Codeforces Round #212 (Div. 2) A. Two Semiknights Meet /B. Petya and Staircases
- Codeforces Round #277.5 (Div. 2)---A. SwapSort (sort)
- Codeforces Round #212 (Div. 2) B. Petya and Staircases
- Codeforces Round #212 (Div. 2) A题
- Codeforces Round #277.5 (Div. 2) A SwapSort 我自己写的奇葩代码竟然AC了。。。自己都觉得不可思议
- Codeforces Round #259 (Div. 2) B - Little Pony and Sort by Shift
- Codeforces Round #212 (Div. 2) A. Two Semiknights Meet
- Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads
- Codeforces Round #212 (Div. 2)A
- Codeforces Round #212 (Div. 2) B. Petya and Staircases
- Codeforces Round #277.5 (Div. 2)A——SwapSort
- Codeforces Round #212 (Div. 2)(完全)
- Codeforces Round #212 (Div. 2)