codeforces 1277 D xg

题意:给n个01字符串,让你进行k下操作,使得t字符串翻转,使得n个字符串存在一个顺序,使得ai+1的首字母和ai相等。使得k最小,输出这k个数。(注意变化前和变化后的字符串不重复)

思路:只有四种字符串,记录下每种字符串的数量。如果11,00,都不为0,且10,01为0,则无解。

  考虑sum1和sum2的差值小于1是有解的,否则考虑使得数量多的。

  若10多于01,考虑10的字符串翻转状态不存在在原字符串的数量够不够(sum10-sum01)/2。

  字符串标记map就行。

#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <vector> 
// #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sp ' '
#define endl '\n'
#define inf  0x3f3f3f3f;
#define FOR(i,a,b) for( int i = a;i <= b;++i)
#define bug cout<<"--------------"<<endl
#define P pair<int, int>
#define fi first
#define se second
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define sca2(a,b) scanf("%d %d",&(a),&(b))
#define sca(a) scanf("%d",&(a));
#define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define sca2ll(a,b) scanf("%lld %lld",&(a),&(b))
#define scall(a) scanf("%lld",&(a));


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 sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;}

const double Pi = acos(-1.0);
const double epsilon = Pi/180.0;
const int maxn = 2e5+10;
map<string,int>vis;
string a[maxn];
int ans[maxn];
int main()
{
    // freopen("input.txt", "r", stdin);
    int _;
    scanf("%d",&_);
    while(_--)
    {
        int n;
        cin>>n;
        int tail = 0;
        int sum1 = 0,sum2 = 0,sum11=0,sum00=0;
        queue<int>que1;
        queue<int>que2;
        rep(i,1,n){
            string s;
            cin>>s;
            a[i] = s;
            vis[s] = 1;
            int len = s.length()-1;
            if(s[0]=='0'&&s[len]=='0') sum00++;
            else if(s[0]=='0'&&s[len]=='1') {
                que2.push(i);
                sum2++;
            }
            else if(s[0]=='1'&&s[len]=='1') sum11++;
            else if(s[0]=='1'&&s[len]=='0') {
                que1.push(i);
                sum1++;
            }
        }
        if(sum11&&sum00&&sum1==0&&sum2==0){
            cout<<-1<<endl;
            continue;
        }
        else {
            if(sum1 >= sum2){
                int cha = sum1 - sum2;
                if (cha <= 1)
                {
                    cout<<0<<endl;
                    /* code */
                }
                else {
                    cha /= 2;
                    tail = 0;
                    while(que1.size()){
                        if(tail == cha) break;
                        int pos = que1.front();
                        que1.pop();
                        string tmp = a[pos];
                        //int k = tmp.length();
                        reverse(tmp.begin(),tmp.end());
                        if(vis[tmp]==1) continue;
                        ans[++tail] = pos;
                    }
                    if(tail == cha){
                        cout<<tail<<endl;
                        rep(i,1,tail){
                            cout<<ans[i]<<sp;
                        }
                        cout<<endl;
                    }
                    else cout<<-1<<endl;    
                }
            }
            else {
                int cha = sum2 - sum1;
                if (cha <= 1)
                {
                    cout<<0<<endl;
                    /* code */
                }
                else {
                    cha /= 2;
                    tail = 0;
                    while(que2.size()){
                        if(tail == cha) break;
                        int pos = que2.front();
                        que2.pop();
                        string tmp = a[pos];
                        reverse(tmp.begin(),tmp.end());
                        if(vis[tmp]==1) continue;
                        ans[++tail] = pos;
                    }
                    if(tail == cha){
                        cout<<tail<<endl;
                        rep(i,1,tail){
                            cout<<ans[i]<<sp;
                        }
                        cout<<endl;
                    }
                    else cout<<-1<<endl;
                    
                }cout<<endl;
            }            
        }
        rep(i,1,n){
            vis[a[i]] = 0;
        }
        //cout<<sum1<<sp<<sum2<<endl;

    }
}

 

posted @ 2020-07-08 14:34  阿斯水生产线  阅读(141)  评论(0编辑  收藏  举报