P5200 [USACO19JAN] Sleepy Cow Sorting cattle off the holiday season 6 D team lost cattle (greedy)

Links: https://ac.nowcoder.com/acm/contest/993/E
Source: Cattle-off network

Sort bovine
time limits: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
64bit IO the Format:% LLD
Title Description
Farmer John is trying his N cows (1≤N≤ 100), convenience numbered 1 ... N, before they go to pasture eating breakfast in good order.
Currently, these cows to p1, p2, p3, ..., pN sequence of aligned, Farmer John cow standing in front of p1. He wants to rearrange the cows so that they become the order 1,2,3, ..., N 1 next to cows in Farmer John.

Today, some sleepy cows, so any time only direct-to-Farmer John's cows will pay attention to listen to Farmer John's instructions. Each time he may order the cow moves backward along the k-team, k can be any number in the range N-1 ... 1. She passed k cow moves forward to make room so that she can be inserted into position after the team cows.

For example, assume that N = 4, when the cows are started in this order:

FJ: 4, 3, 2, 1
only note FJ instruction cow is a cow 4. When he ordered her to move two steps to the team, will become the order of the team:

FJ: 3, 2, 4, 1
and now only note FJ instruction cows cows 3, the second time he can give the command cows 3, so until the cows lined up order.

Farmer John is eager to complete the order, so he can return to his farmhouse enjoying his breakfast. Please help him find the minimum number of operations to cows in good order required.

Input Description: The
first line of input contains N.

The second line contains N space separated integers, p1, p2, p3, ... , pN, cows represents the start sequence.
Description Output:
output an integer, for the best strategy may be employed Farmer John these cows N row number of operations required in good order.
Example 1
Input
replication
. 4
1 2 3. 4
Output
Copy
3
Notes:

Meaning of the questions: ideas: we know that a number of cattle x, if there is a cow of the right number y so that y <x x then the number of cattle will definitely have to be sorted, then we might as well go directly to the rightmost this satisfies the condition of cattle. To


See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int a[maxn];
int n;
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    gbtb;
    cin >> n;
    repd(i, 1, n) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = n; i >= 1; i--) {
        int isok = 0;
        for (int j = i + 1; j <= n; ++j) {
            if (a[i] > a[j]) {
                isok = 1;
            }
        }
        if (isok) {
            ans = i;
            break;
        }
    }
    cout << ans << endl;
    return 0;
}

inline void getInt(int *p)
{
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    } else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Guess you like

Origin www.cnblogs.com/qieqiemin/p/11285026.html