Comprehensive training (2) + string

A - Chips Moving

#include<stdio.h>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=1e2+2;
int x[MAX],n;
/*
If it is an odd number, move: 2 * n + 1, it must cost 1 ¥
If it is European style, move: 2 * n, it will definitely not cost money
Only consider the money spent
*/
int main ()
{
	cin>>n;
	map<int,int>pos;
	for(int i=0;i<n;++i)
    {
        cin>>x[i];
        pos [x [i]] ++; // Record the existing number of each coordinate
    }
       sort(x,x+n);
    int len=unique(x,x+n)-x;
    /*
    The role of unique is to "remove" duplicate elements of adjacent elements in the container (not necessarily ordered array)
    It will add duplicate elements to the end of the container (so the array size has not changed)
    The return value is the tail address after deduplication, so if you want to get the size after deduplication, you need to subtract the initial address
    */
    int ans = 0x3f3f3f3f;
    int I;
    for(int i=0;i<len;++i)
    {
        sum=0;
        for(int j=0;j<len;++j)
        {
            if(x[j]!=x[i])
            {
                if(abs(x[i]-x[j])&1) sum+=pos[x[j]];
            }
        }
        years = min (years, sum);
    }
    cost << years;
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/chrysanthemum/p/12695915.html