Sicily 1006. Team Rankings

求逆序对数,数据规模小,暴力即可。

这里还用到了STL中的组合数函数next_permutation(),顺便贴上该函数的用法:http://www.cplusplus.com/reference/algorithm/next_permutation/

#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
#define MIN 0x3f3f3f3f
using namespace std;

int main(){
    //freopen("in.txt", "r", stdin);
    int n;
    while(cin >> n && n){
        vector<string> team(105);
        for(int i = 0; i < n; i++)
            cin >> team[i];
        string s = "ABCDE", ans;
        int res = MIN;
        do{
            int cont = 0;
            for(int j = 0; j < n; j++){
                vector<bool> flag(10, false);
                for(int i = 0; i < s.size(); i++){
                    for(int k = 0; k < team[j].size(); k++){
                        if(s[i] == team[j][k]){
                            flag[k] = true;
                            break;
                        }
                        if(!flag[k])
                            cont++;
                    }
                    //cout << cont << endl;
                }

            }
            if(cont < res){
                res = cont;
                ans = s;
            }
        }while(next_permutation(s.begin(), s.end()));
        cout << ans << " is the median ranking with value " << res <<"." <<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值