UVA299 Train Swapping

本文介绍了一个基于冒泡排序算法的火车车厢排序问题解决方案。通过交换相邻车厢的位置来达到最优顺序,最小化所需的交换次数。

简单的冒泡排序,这题是练英语阅读的。

题目:

Train Swapping

 

 Train Swapping 

At an old railway station, you may still encounter one of the last remaining ``train swappers''. A train swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.

Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the carriages off, one by one, at the stations for which the load is meant.

 

The title ``train swapper'' stems from the first person who performed this task, at a station close to a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left or right.

The first train swapper had discovered that the bridge could be operated with at most two carriages on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move either way, so who cares).

Now that almost all train swappers have died out, the railway company would like to automate their operation. Part of the program to be developed, is a routine which decides for a given train the least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to create that routine.

 

Input Specification

The input contains on the first line the number of test cases (N). Each test case consists of two input lines. The first line of a test case contains an integer L, determining the length of the train ( tex2html_wrap_inline30 ). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc. with carriage L coming last.

 

Output Specification

For each test case output the sentence: 'Optimal train swapping takes S swaps.' where S is an integer.

 

Example Input

 

3
3
1 3 2
4
4 3 2 1
2
2 1

 

Example Output

 

Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.

 

解答:

 1 #include<stdio.h>
 2 #include<string.h>
 3 int num[100];
 4 int main()
 5 {
 6     int s,t;
 7     scanf("%d",&s);
 8     while(s--)
 9     {
10         int i,j,t,cnt=0;
11         scanf("%d",&t);
12         for(i=0;i<t;i++)
13             scanf("%d",&num[i]);
14         for(i=0;i<t;i++)
15         {
16             for(j=i+1;j<t;j++)
17             {
18                 if(num[i]>num[j])
19                 {
20                     int tmp;
21                     tmp=num[i];
22                     num[i]=num[j];
23                     num[j]=tmp;
24                     cnt++;
25                 }
26             }
27         }
28         printf("Optimal train swapping takes %d swaps.\n",cnt);
29     }
30     return 0;
31 }

 

转载于:https://www.cnblogs.com/terryX/archive/2013/02/10/2909801.html

在系统运维过程中,如果观察到频繁的交换(swapping)行为,这通常表明系统的物理内存(RAM)不足以满足当前运行的应用程序需求,导致操作系统将部分内存数据转移到磁盘上的交换空间(swap space),从而缓解内存压力。然而,这种交换行为会显著影响系统性能,因为磁盘的读写速度远低于内存。 ### 原因分析 1. **物理内存不足** 当系统中运行的应用程序消耗的内存总量超过可用的物理内存时,操作系统会开始使用交换空间来释放内存页,这会导致频繁的内存与磁盘之间的数据交换[^1]。 2. **内存泄漏或资源未释放** 某些应用程序可能存在内存泄漏问题,即程序在运行过程中不断分配内存而未能正确释放,导致可用内存逐渐减少,最终触发交换行为。 3. **缓存和缓冲区占用过高** Linux 系统会利用空闲内存作为文件系统缓存(如 page cache 和 slab),以提高 I/O 性能。但在内存紧张时,系统可能无法及时释放这些缓存,从而导致交换行为增加。 4. **交换阈值设置不合理** Linux 系统通过 `swappiness` 参数控制内核将内存页移出到交换空间的倾向,默认值为 60。如果该值设置过高,即使系统仍有可用内存,也可能触发不必要的交换行为。 5. **突发性内存需求** 某些任务或进程在短时间内需要大量内存,例如批处理作业、大数据分析任务等,可能导致瞬时内存压力增大,从而引发交换。 ### 优化方法 1. **增加物理内存** 最直接的解决方案是增加服务器的物理内存(RAM),以减少对交换空间的依赖。这是解决频繁交换问题的根本方法。 2. **优化应用程序内存使用** 分析应用程序的内存使用情况,识别并修复内存泄漏问题,合理管理内存分配和释放。使用工具如 `valgrind`、`gperftools` 等可以帮助检测内存泄漏。 3. **调整 `swappiness` 参数** 通过修改 `/proc/sys/vm/swappiness` 文件或在 `sysctl.conf` 中设置 `vm.swappiness` 值来降低系统对交换空间的依赖。建议值为 10 到 20 之间,具体取决于系统负载特性。 ```bash sysctl -w vm.swappiness=10 ``` 4. **监控内存与交换使用情况** 使用 `top`、`htop`、`free`、`vmstat`、`sar` 等工具监控系统的内存和交换使用情况,及时发现异常行为。 ```bash free -h vmstat 1 ``` 5. **限制进程内存使用** 使用 `cgroups` 或 `systemd` 的内存限制功能,为特定服务或进程设置内存上限,防止个别进程占用过多内存。 ```ini # 示例:在 systemd 服务单元文件中限制内存 [Service] MemoryLimit=2G ``` 6. **使用 ZRAM 或 ZSWAP(内存压缩)** 启用 ZRAM 或 ZSWAP 可以在内存不足时将部分数据压缩后存储在内存中,而不是写入磁盘,从而降低 I/O 延迟。 ```bash modprobe zram echo $((16 * 1024 * 1024 * 1024)) > /sys/block/zram0/disksize mkswap /dev/zram0 swapon /dev/zram0 ``` 7. **避免不必要的缓存占用** 在内存紧张时,系统会自动释放部分缓存,但也可以通过手动清理缓存来释放内存: ```bash echo 1 > /proc/sys/vm/drop_caches # 释放 page cache echo 2 > /proc/sys/vm/drop_caches # 释放 dentries 和 inodes echo 3 > /proc/sys/vm/drop_caches # 释放所有缓存 ``` --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值