Codeforces Round #606 (Div. 2) D. Let's Play the Words?(贪心+map)

?? ?? ??

题意:给你一些序列,要求把这些序列翻转之后能首尾相连(01,10),并且字符串不能相同,询问最小步数;

1.我们只关心这个字符串首尾位置,一共只有四种情况:00,01,10,11;00 和 11 是没必要翻转的,剩下 01,10 只要存在就可以相互抵消(0110,1001这种),剩下多的 01 or 10,就是我们最后需要翻转的字符串,数量为abs(num01-num10)/2;
2.为了满足字符串不能相同,一开始就记录下哪写字符串不可以翻转,例,假如s翻转之后为rev,rev出现过则说明s不可翻转,而同时rev也不可翻转,因为rev翻转之后为s,而s是一定不会变的

模一模大佬的代码QAQ

int n;
string s[MAXN];
int32_t main()
{
    //fast;
    int t;cin >> t;
    while (t--)
    {
        cin >> n;
        for (int i = 0; i < n; ++i) cin >> s[i];
        set<string> seen, bad;
        int zz = 0, oo = 0, zo = 0, oz = 0;
        for (int i = 0; i < n; ++i)
        {
            string t = s[i];
            if (t[0] == '0' && t[t.length() - 1] == '0') zz++;
            else if (t[0] == '0')//01
            {
                zo++;
                string rev(t.rbegin(), t.rend());//
                if (seen.count(rev))//
                    bad.insert(t),bad.insert(rev);
                seen.insert(t);
            }
            else if (t[t.length() - 1] == '0')//10
            {
                oz++;
                string rev(t.rbegin(), t.rend());
                if (seen.count(rev)) bad.insert(t),bad.insert(rev);
                seen.insert(t);
            }
            else oo++;
        }
        if (zo == 0 && oz == 0)
        {
            if (zz == 0 || oo == 0) cout << 0 << '\n'<< '\n';
            else cout << -1 << '\n';
        }
        else
        {
            int ans = abs(zo - oz) / 2;cout << ans << '\n';
            for (int i = 0; i < n; ++i)
            {
                if (ans == 0) break;
                if (zo > oz && s[i][0] == '0' && s[i].back() == '1' && !bad.count(s[i]))
                    ans--,cout << i + 1 << ' ';
                if (oz > zo && s[i][0] == '1' && s[i].back() == '0' && !bad.count(s[i]))
                    ans--,cout << i + 1 << ' ';
            }
            cout << '\n';
        }
    }
    return 0;
}

Codeforces Round #606 (Div. 2) D. Let's Play the Words?(贪心+map)

原文地址:https://www.cnblogs.com/Herlo/p/12063583.html

时间: 2024-08-29 09:37:52

Codeforces Round #606 (Div. 2) D. Let's Play the Words?(贪心+map)的相关文章

【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析:

Codeforces Round #606 Div. 2 比赛情况

比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论. Talk is cheap.Show me the code. B 我们把数值一样的数放在一起,扔进堆里.按数值从大到小处理就OK了. 注意值域比较大,用一下 \(STL\) 里面的 map. Talk is cheap.Show me the code. #include<bits/stdc++.h> using na

Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

A. 签到题:思路为:所求答案 = 9 * (字符长度 - 1) + 最高位数 +(- 1)//通过判断语言确定是否需要再减个一 如果a****** > *******则需要加一反之不需要 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main () { ll t; cin >> t; while(t--) { ll ans; ll temp = 0; string num; c

Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

?? ?? ?? 题意:求点对中,满足要互达必须经过a,b两点的对数,图为无向连通图 若(x,y)要满足互达必须经过(a,b),反过来想, a必须通过b点到达y点:满足a--->b--->y: b必须通过a点到达x点:满足b--->a--->x,无向图:x--->a--->b: 连起来即为:x--->a--->b--->y: int vis[MAXN]; vector<int>edge[MAXN]; void dfs(int x,int e

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心

链接: http://codeforces.com/contest/822/problem/C 题意: 有x天的假期, 有n张旅行票, 每张票有起始时间l, 结束时间r, 花费cost, 想把假期分成两部分出去旅游, 两部分时间不能重合(ri < lj || rj < li), 问最小花费是多少, 如果不能两部分, 输出-1 题解: CF官方解法, 效率O(nlogn2) 设置一个结构体, struct P{int p, len, cost, type}; 将每张票(l, r, cost) 表

Codeforces Round #415 (Div. 2)(A,暴力,B,贪心,排序)

A. Straight «A» time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Noora is a student of one famous high school. It's her final year in school — she is going to study in university next year.

Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)

传送门 Description A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it's new tablet Lastus 3000. This new devic

Codeforces Round #342 (Div. 2) A. Guest From the Past(贪心)

传送门 Description Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated. Koly

Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)

传送门 Description We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a subs