【POJ3270】【Cow Sorting】

2024-08-31 16:58
文章标签 cow sorting poj3270

本文主要是介绍【POJ3270】【Cow Sorting】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Cow Sorting
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6411 Accepted: 2488

Description

Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage FJ's milking equipment, FJ would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (not necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes FJ a total of X+Y units of time to exchange two cows whose grumpiness levels are X and Y.

Please help FJ calculate the minimal time required to reorder the cows.

Input

Line 1: A single integer:  N
Lines 2.. N+1: Each line contains a single integer: line  i+1 describes the grumpiness of cow  i

Output

Line 1: A single line with the minimal time required to reorder the cows in increasing order of grumpiness.

Sample Input

3
2
3
1

Sample Output

7

Hint

2 3 1 : Initial order. 
2 1 3 : After interchanging cows with grumpiness 3 and 1 (time=1+3=4). 
1 2 3 : After interchanging cows with grumpiness 1 and 2 (time=2+1=3).

Source

USACO 2007 February Gold

能想到这个式子还是很厉害的,,,

因为必须要有dp[i][j-1] 和dp[i+1][j] 但是这样又多了,又得减去。还有2个端点没有考虑到。


#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;int n,m;
int a[1010];
int dp[1010][1010];
int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int i=n;i>=1;i--)for(int j=i;j<=n;j++){dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1];if(a[i] > a[j]) dp[i][j] ++;}int l,r;while(m--){	scanf("%d%d",&l,&r);printf("%d\n",dp[l][r]);}return 0;
}


这篇关于【POJ3270】【Cow Sorting】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1124471

相关文章

Sorting It All Out POJ(拓扑排序+floyd)

一个就是简单的拓扑排序,一个就是利用floyd判断是否存在环 #include<cstdio>#include<cstring>#include<vector>#include<queue>using namespace std;#define MAXD 30#define MAX_SIZE 1000vector<int>G[MAXD];int n,m;char L[MAX

[LeetCode] 969. Pancake Sorting

题:https://leetcode.com/problems/pancake-sorting 题目大意 对于数组A,可进行 k-reverse操作,即将 前k个元素翻转,求能使A为升序的 k-reverse 操作顺序。 解题思路 分析k操作,k-reverse操作只会影响 前面的数,不会影响 数组中后面的数,所以思路是逐步将最大元素 放置在合适的位置。 先拷贝 A 为 Abackup,

【POJ】3660 Cow Contest floyd(可以拓扑排序?)

Cow Contest Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6925 Accepted: 3792 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating i

BFS —— POJ 3278 Catch That Cow

对应POJ题目:点击打开链接 Catch That Cow Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 54686 Accepted: 17096 Description Farmer John has been informed of the location of a fugitive cow

USACO Section 2.3 Cow Pedigrees

题意: N个节点  深度为K  的正则二叉树  求  树有几种形态 思路: 一开始以为是数学题…  看了byvoid的题解才知道是dp… 每棵树由根节点、左子树、右子树构成  由此得状态转移  树=左子树*右子树 节点数和深度是影响答案的属性  所以令dp[i][j]表示i个节点深度在j以内的树的形态数 深度在j以内的树又两个深度在j-1以内的树和一个根节点构成 设左子树k个节

Codeforces 283. B. Cow Program记忆化搜索

output standard output Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1, a2, ..

Sorting (Merge Sort, Rainball Sort, quick select) 总结

Merge k Sorted Lists (这题是用PQ,或者merge sort都可以做,关于第K大的问题,参考: Find Kth 题目类型总结) Sort an Array (重点掌握merge sort和quick sort,因为两者可以演变为,divide conquer, quick select, 参考: Find Kth 题目类型总结) Sort Colors 思路:三指针,i

【POJ3268】【Silver Cow Party】【反向dij】【sizeof失效】

Silver Cow Party Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15522 Accepted: 7039 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going

POJ 1985 Cow Marathon(树的直径)

题目链接 题意:给出一棵树,求出这个树的直径 解答:任选一点进行dfs,会找到一个最远点s,在以这个最远点s进行dfs,会找到一个最远点是t,那么s-t就是树的直径。 //#include<bits/stdc++.h>#include<cstdio>#include<algorithm>#include<vector>#include<cstring>using namespace

Code Practice Journal | Day58_Graph08 Topological Sorting

1. 概念 在一个有向无环图(DAG)中,根据节点的依赖关系,对所有的节点进行线性排序的算法 拓扑排序的结果不一定是唯一的 2. 实现 2.1 BFS(卡恩算法) 1、步骤 2、代码实现 以KamaCoder 117.软体构建 题目:117. 软件构建 (kamacoder.com) class Program{public static void Main(string