包括的なトレーニング(2)+文字列

A-チップの移動

#include <stdio.h> 
#include <map> 
#include <iostream> 
#include <algorithm> 
using namespace std; 
const int MAX = 1e2 + 2; 
int x [MAX]、n; 
/ * 
奇数の場合は、次を移動: 2 * n + 1、1のコストが必要です¥
ヨーロッパスタイルの場合、移動します:2 * n、お金は使いません
考慮されたお金のみを使用します
* / 
int main()
{ 
	cin >> n; 
	map <int、int> pos ; 
	for(int i = 0; i <n; ++ i)
    { 
        cin >> x [i]; 
        pos [x [i]] ++; //既存の座標の数を記録する
    } 
       sort(x 、x + n); 
    int len = unique(x、x + n)-x; 
    / * 
    一意の役割は、コンテナ内の隣接する要素の重複する要素を「削除」することです(必ずしも順序付けされた配列は必要ありません)
    。要素はコンテナーの最後に追加され(配列のサイズは変更されていません)
    、戻り値は重複排除後の末尾アドレスであるため、重複排除後にサイズを取得するには、初期アドレスを減算する必要があります
    * /
    int ans = 0x3f3f3f3f; 
    int sum; 
    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]]; 
            } 
        } 
        ans = min(ans、sum); 
    } 
    cout << ans; 
    0を返します。
}

  

おすすめ

転載: www.cnblogs.com/chrysanthemum/p/12695915.html