codeforces 1283E New Year Parties (greedy)

Link: https: //codeforces.com/contest/1283/problem/E

Meaning of the questions: There are n number of individuals living in the house, and some people living in the same house. Each person can choose to move to his left or right side of the house that house the house about, or do not move, a move can only move left or right. At least ask these people to live up to live a few houses and a few houses.

Solution: greed. The minimum is three people gather together, polymerization in a house. The maximum is greedy move to an empty house as much as possible

 

AC Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 2e5+5 ;
int x[maxn];
int s[maxn];
int main(){
    int n;cin>>n;
    for(int i = 0;i<n;i++){
        int t;cin>>t;
        x[t] ++;
        s[t] ++;
    }
    int= MX 0 , mi The = 0 ;
     for ( int I = . 1 ; I <= n-+ . 1 ; I ++) { // find the maximum value 
        IF (X [I] == 0 ) Continue ; // this is 0, unoccupied skipped 
        IF (X [I- . 1 ] == 0 ) { // before a house is empty, is moved over a person, representing a house 
            X [I] - ; 
            X [I - . 1 ] ++ ; 
        } 
        IF (X [I]> . 1 ) { // after a house moved forward a person, if the person moves again there is excess, it is possible to go down the back of the house moves 
            x [i +. 1 ] ++ ; 
            X [I] - ; 
        } 
    } 
    for ( int I = 0 ; I <= n-+ . 1 ; I ++ ) {
         IF (! X [I] = 0 ) MX ++ ; 
    } 
    Vector < int > V;
     int = F 0 , CUR = 0 ;
     for ( int I = . 1 ; I <= n-; I ++ ) {
         IF (! S [I] = 0 ) { // if the current house someone, then the local house +1 and -1 people polymerization over, once every three people jump
            mi++;
            i+=2;//直接跳转
        }
    }
    cout<<mi<<" "<<mx;
    return 0;
}

Guess you like

Origin www.cnblogs.com/AaronChang/p/12129873.html
Recommended