POJ 2188 Cow Laundry
Cow Laundry
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1376 Accepted: 886
Description
The cows have erected clothes lines with N (1 <= N <= 1000) wires upon which they can dry their clothes after washing them. Having no opposable thumbs, they have thoroughly botched the job. Consider this clothes line arrangement with four wires:
Wire slot: 1 2 3 4
--------------- <-- the holder of the wires
\ \ / /
\ \/ /
\ /\ /
\ / \ / <-- actual clothes lines
/ \
/ \ / \
/ \/ \
/ /\ \
/ / \ \
--------------- <-- the holder of the wires
Wire slot: 1 2 3 4
The wires cross! This is, of course, unacceptable.
The cows want to unscramble the clothes lines with a minimum of hassle. Even their bovine minds can handle the notion of “swap these two lines”. Since the cows have short arms they are limited to swapping adjacent pairs of wire endpoints on either the top or bottom holder.
In the diagram above, it requires four such swaps in order to get a proper arrangement for the clothes line:
1 2 3 4
------------- <-- the holder of the wires
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
------------- <-- the holder of the wires
1 2 3 4
Help the cows unscramble their clothes lines. Find the smallest number of swaps that will get the clothes line into a proper arrangement.
You are supplied with clothes line descriptions that use integers to describe the current ordering of the clothesline. The lines are uniquely numbered 1…N according to no apparent scheme. You are given the order of the wires as they appear in the N connecting slots of the top wire holder and also the order of the wires as they appear on the bottom wire holder.
Input
Line 1: A single integer: N
Lines 2…N+1: Each line contains two integers in the range 1…N. The first integer is the wire ID of the wire in the top wire holder; the second integer is the wire ID of the wire in the bottom holder. Line 2 describes the wires connected to top slot 1 and bottom slot 1, respectively; line 3 describes the wires connected to top and bottom slot 2, respectively; and so on.
Output
- Line 1: A single integer specifying the minimum number of adjacent swaps required to straighten out the clothes lines.
Sample Input
4
4 1
2 3
1 4
3 2
Sample Output
4
Source
USACO 2003 Fall Orange
找到规律的话就是求有多少逆序对,还是比较好想的,但是要处理的话,这个逆序是相对于开始状态,不是另一端点,代码比较简单,思维题,签到题)
#include<iostream>
#include<map>
using namespace std;
int ob[1005];
int oj[1005];
map<int,int>w;
int main()
{
int n;
w.clear();
cin>>n;
for(int i=0;i<n;i++){
cin>>ob[i]>>oj[i];
w.insert(make_pair(ob[i],i));
}
int ans=0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(w[oj[i]]>w[oj[j]]) ans++;
cout<<ans<<endl;
}
POJ 2188 Cow Laundry的更多相关文章
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- POJ 2375 Cow Ski Area(强连通)
POJ 2375 Cow Ski Area id=2375" target="_blank" style="">题目链接 题意:给定一个滑雪场, ...
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
随机推荐
- go 反射包
一.什么是反射? 反射是用程序检查其所拥有的结构,尤其是类型的一种能力: 二.Printf Printf 的函数声明为: func Printf(format string, args ... int ...
- go For-range结构
For结构: for 初始化;条件语句;修饰语句{ 输出 } 一.For-range结构是可以怎么用? 这种构建方法可以应用于数组和切片: for ix, value := range slice1 ...
- Tcl编程第一天,helloworld
#!/usr/bin/tclsh puts "hello world" 注意:第一行代码表示的是tcl程序运行所需要的文件位置 puts函数代表输出
- Python中list(列表)、dict(字典)、tuple(元组)、set(集合)详细介绍
更新时间:2019.08.10 更新内容: "2.14加入sorted()函数" "2.3"加入一种删除元素的方法 "二.字典"新增1.5, ...
- floyd最小环&&模板
floyd的核心代码: ;k<=n;k++){ ;i<=n;i++){ ;j<=n;j++){ dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j] ...
- git获取特定的commit
git reset --hard [commit_id]
- 使用docker-compose编写常规的lnmp容器,pdo连接mysql失败。
问题的核心是yii2 是通过pdo的方式去连接数据的.但是我们通过容器去搭建lnmp环境时,nginx , php , mysql 这三个服务是独立的三个容器,彼此隔离.所以在yii2中连接mysql ...
- 正整数的二进制表示中1的个数计算(使用移位或者n&(n-1))
第一种:使用n&(n-1)表示来计算有多少个1 int n=127; int count=0; while (n!=0){ count++; n=n&(n-1); } 第二种:使用移位 ...
- vuepress+gitee 构建在线项目文档
目录 快速入门 在现有vue项目中安装本地开发依赖vuepress 在现有vue项目根目录下创建docs目录 创建并配置文档首页内容 运行,查看效果 可能会出现vue和vue-server-rende ...
- ASP.NET MVC学习——控制器传递数据到view的简单案例
从控制传数据到view端的方法: 方法一:修改Control数据,添加ViewBag 1.ViewBag.+ 任意变量名 = "你想要输入的数据" 2.在Control对应的csh ...