C. Insertion Sort----构造题
来源:互联网 发布:知乎怎么那么多出国的 编辑:程序博客网 时间:2024/06/03 05:57
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.
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.
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.
54 0 3 1 2
3 2
51 2 3 4 0
3 4
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://codeforces.com/contest/362/problem/C
这个题并没有想出来,看了大佬的题解,还需要加强这方面的练习啊
http://blog.csdn.net/u013912596/article/details/39005037
代码:
#include <cstdio>#include <cstring>#include <iostream>#define inf 0x3f3f3f3f#define N 5005using namespace std;int mi[N][N],ma[N][N],num[N],cnt,ans=inf;int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&num[i]); } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(num[j]>num[i]) ma[i][j]=1; if(num[j]<num[i]) mi[i][j]=1; if(j){ ma[i][j]+=ma[i][j-1]; mi[i][j]+=mi[i][j-1]; } } } int cur=0; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i<j&&num[i]>num[j]) cur++; } } for(int i=0;i<n;i++){ for(int j=i+1;j<n;j++){ if(num[i]<num[j]) continue; int temp=cur; temp+=(mi[j][j-1]-mi[j][i]+ma[i][j-1]-ma[i][i]); temp-=(ma[j][j-1]-ma[j][i]+mi[i][j-1]-mi[i][i]); temp--; if(ans==temp) cnt++; else if(ans>temp){ ans=temp; cnt=1; } } } cout<<ans<<" "<<cnt<<endl; return 0;}
- C. Insertion Sort----构造题
- Codeforces 362C. Insertion Sort【构造】
- (C#)插入排序 Insertion Sort
- cf-362C-Insertion Sort
- Codeforces 362C. Insertion Sort
- codeforces 362c Insertion Sort
- cf——C. Insertion Sort
- [C++] 插入排序法 (Insertion sort)
- c编写 Insertion or Heap Sort
- 【LeetCode】147. Insertion Sort List C语言
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- Insertion Sort
- insertion sort
- insertion sort
- insertion sort
- mysql中engine=innodb和engine=myisam的区别
- GBDT决策树入门教程
- JavaScript 参考手册
- SpringBoot整合Mybatis多数据源
- 使按钮在不同状态时显示不同图片
- C. Insertion Sort----构造题
- volley超时和重复请求问题
- 使用el表达式的优先级
- Windows的cmd中cd指令无法转换路径怎么办?
- @ManyToOne @OneToMany @JoinColumn
- sass的混合宏
- android中得到系统时间
- TextAsset,直接拉取文本对象
- RxJava+Retrofit+OkHttp3+Dagger2+MVP构建Android项目简单例子