题目大意:给n个数的一个序列,通过交换相邻的两个数使得这n个数按照从小到大的顺序排列。

  Inversion index problem: count how many swaps are needed to make the list sorted. 使用冒泡排序解决。

 #include <cstdio>

 int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
int a[];
while (T--)
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
int swap = ;
for (int i = n; i > ; i--)
for (int j = ; j+ < i; j++)
if (a[j] > a[j+])
{
int t = a[j];
a[j] = a[j+];
a[j+] = t;
swap++;
}
printf("Optimal train swapping takes %d swaps.\n", swap);
}
return ;
}

UVa 299 - Train Swapping的更多相关文章

  1. UVA 299 (13.07.30)

     Train Swapping  At an old railway station, you may still encounter one of the lastremaining ``train ...

  2. UVa 11586 - Train Tracks

    题目:给你一些积木碎片,每一个碎片的两端仅仅能是凸或凹(M或F).凸凹可拼起来.是否能拼成一个环. 分析:图论.欧拉回路.推断入度等于出度就可以,即M和F同样且大于1组. 说明:╮(╯▽╰)╭. #i ...

  3. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  4. Volume 1. Sorting/Searching(uva)

    340 - Master-Mind Hints /*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次. 题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S ...

  5. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  6. UVA - 1025 A Spy in the Metro[DP DAG]

    UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especia ...

  7. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  8. UVA - 11374 - Airport Express(堆优化Dijkstra)

    Problem    UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...

  9. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

随机推荐

  1. PAT1004

    A family hierarchy is usually presented by a pedigree tree. 一个家族的层次结构经常用一个血缘树来呈现. Your job is to cou ...

  2. [转] Spring Security(01)——初体验

    [转自:http://haohaoxuexi.iteye.com/blog/2154299] 首先我们为Spring Security专门建立一个Spring的配置文件,该文件就专门用来作为Sprin ...

  3. rstPixelType Constants

    Constant Value Description PT_UNKNOWN -1 Pixel values are unknown. PT_U1 0 Pixel values are 1 bit. P ...

  4. 【百度地图开发之二】基于Fragment的地图框架的使用

    写在前面的话: [百度地图开发之二]基于Fragment的地图框架的使用(博客地址:http://blog.csdn.net/developer_jiangqq),转载请注明. Author:hmji ...

  5. n个List<Map>合并,Map中某属性值相等的value值相加

    List<Map> maps1 =[{"bigtypes":100,"num":400},{"bigtypes":200,&qu ...

  6. flex4 list 自动适应高度

    <s:List width="100%"> <s:layout> <s:VerticalLayout useVirtualLayout="f ...

  7. js获取屏幕和窗口的信息

    <html><script>function a(){document.write("屏幕分辨率为:"+screen.width+"*" ...

  8. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...

  9. springMVC注解及优化

    1. 新建web project 2. 添加jar 3. 改写web.xml, 注意spring-servlet.xml的名字 <?xml version="1.0" enc ...

  10. 51Nod 1534

    分析:Pwin代表Polycarp走的步数,而Win代表Vasiliy走的步数,则有Pwin=p.x+p.y,Vwin=max(v.x,v.y);显然若Pwin<=Win,肯定是Vasiliy胜 ...