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

Happy Birthday, Polycarp!

\ [Time Limit: 1 s \
quad Memory Limit: 256 MB \] violent enumerate all numbers are all \ (I, I \ in [1,9] \) , then the full force is determined a plurality of \ (I \ ) number in \ (n \) can be less.


view

#include <unordered_map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

ll n, m;
int cas, tol, T;

int main() {
    scanf("%d", &T);
    while(T--) {
        ll ans = 0;
        scanf("%lld", &n);
        for(int i=1; i<=9; i++) {
            ll tmp = i;
            while(tmp <= n) {
                ans++;
                tmp = tmp*10+i;
            }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

Make Them Odd

\ [Time Limit: 3 s \
quad Memory Limit: 256 MB \] the number has been in addition to \ (2 \) , in addition to record how many times.

So the same for the rest of the count, as long as the record becomes the maximum number of times he needs to be in addition to on.

The final answer is the number needed to become tired and those remaining.


view

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;

map<int, int> mp;

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        mp.clear();
        ll ans = 0;
        for(int i=1, x; i<=n; i++) {
            scanf("%d", &x);
            int res = 0;
            while(x%2 == 0) {
                res++;
                x /= 2;
            }
            if(mp.count(x)) mp[x] = max(mp[x], res);
            else    mp[x] = res;
        }
        for(auto v : mp)    ans += v.se;
        printf("%lld\n", ans);
    }
    return 0;
}

As Simple as One and Two

\ [Time Limit: 3 s \
quad Memory Limit: 256 MB \] first consider \ (twone \) , where because of the \ (o \) is \ (one \) and \ (two \) are used, so direct it deletes it.

In other circumstances, for \ (one \) may exist \ (One, oneeee, oooone, oooneeee \) , then we can find, delete \ (o \) or delete \ (e \) is not very good choice, but \ (n \) does not recurring, so long as delete \ (n \) on it. For \ (two \) is the same reason, delete \ (w \) is the best.


view

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;

char s[maxn];

int main() {
    scanf("%d", &T);
    while(T--) {
        vector<int> ans;
        scanf("%s", s+1);
        int len = strlen(s+1);
        for(int i=1; i<=len; i++) {
            if(i+2<=len && s[i]=='t' && s[i+1]=='w' && s[i+2]=='o') {
                if(i+4<=len && s[i+3]=='n' && s[i+4]=='e') {
                    ans.pb(i+2);
                    i = i+4;
                } else {
                    ans.pb(i+1);
                    i = i+2;
                }
            }
            if(i+2<=len && s[i]=='o' && s[i+1]=='n' && s[i+2]=='e') {
                ans.pb(i+1);
                i = i+2;
            }
        }
        printf("%d\n", ans.size());
        for(auto v : ans)   printf("%d ", v);
        printf("\n");
    }
    return 0;
}

Let's Play the Words?

\ [Time Limit: 3 s \
quad Memory Limit: 256 MB \] string divided into four categories, namely \ (0..0,1 ... 0 ... 0 ... 1,1 \ ) .

First of all can be found, if there is a section \ (3/4 \) string class, the first \ (1/2 \) string class will be able to piece together, completely ignored. If the first does not exist \ (3/4 \) string class, then \ (1/2 \) string class can only occur alone, or certainly not stitching up.

Next, consider the presence of \ (3/4 \) string class, the \ (3 \) string class are \ (n-\) th, \ (4 \) string class are \ (m \) a.
Suppose \ (n-> m \) , then we just violence first reverse \ (3 \) string class becomes the \ (4 \) string class, such that \ (ABS (nm) <=. 1 \) , it must be spliced together, for \ (m> n \) the situation is similar, as long as the first reverse \ (4 \) string class becomes the \ (3 \) class can.

Why can you do? Since the beginning of the string of assurance given topic are \ (different \) , then I put the first \ (3 \) string class flipped past, certainly not on the other section (3 \) \ class string the need to flip the impact, so I just flipped every greedy enough.


view

#include <unordered_map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)
 
typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 1e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;
 
int n, m;
int cas, tol, T;
 
unordered_map<string, bool> mp;
vector<pair<string, int> > vv[4];
 
int main() {
    scanf("%d", &T);
    while(T--) {
        mp.clear();
        for(int i=0; i<4; i++)  vv[i].clear();
        scanf("%d", &n);
        string s;
        for(int i=1; i<=n; i++) {
            cin >> s;
            if(s[0]=='0' && s[s.length()-1]=='0')   vv[0].pb({s, i});
            if(s[0]=='1' && s[s.length()-1]=='1')   vv[1].pb({s, i});
            if(s[0]=='0' && s[s.length()-1]=='1')   vv[2].pb({s, i});
            if(s[0]=='1' && s[s.length()-1]=='0')   vv[3].pb({s, i});
        }
        if(vv[0].size() == n || vv[1].size() == n) {
            printf("0\n\n");
            continue;
        }
        if(vv[0].size() && vv[1].size() && !vv[2].size() && !vv[3].size()) {
            printf("-1\n");
            continue;
        }
        int id = 2;
        if(vv[3].size() > vv[2].size()) id = 3;
        n = vv[id].size(), m = vv[id^1].size();
        vector<int> ans;
        for(auto it : vv[id^1]) mp[it.fi] = true;
        for(auto it : vv[id]) {
            if(abs(n-m) <= 1)   break;
            string ss = it.fi;
            reverse(ss.begin(), ss.end());
            if(mp.count(ss))    continue;
            ans.push_back(it.se);
            n--, m++;
        }
            int sz = ans.size();
            printf("%d\n", sz);
            if(sz==0)   printf("\n");
            for(int i=0; i<sz; i++)
                printf("%d%c", ans[i], i==sz-1 ? '\n':' ');
        
    }
    return 0;
}

Two Fairs

\ [Time Limit: 3 s \
quad Memory Limit: 256 MB \] for start and end positions given \ (S, t \) , I need to find is from \ (s \) departure without \ (t \ ) the number of nodes that can be reached and the \ (t \) departure without \ (s \) the number of nodes that can be reached.

Looking for from \ (s \) departure without (t \) \ node, you can \ (dfs \) aside from the \ (t \) starting to reach the mark up the number of nodes, the termination condition is encountered \ ( S \) node or nowhere. Then from \ (s \) starting to see which nodes are not labeled. So find out the node is necessarily meet the conditions, the second empathy.

The final answer is the product of these mast section of two points.


view

#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  lowbit(x)  x & (-x)
#define  mes(a, b)  memset(a, b, sizeof a)
#define  fi         first
#define  se         second
#define  pb         push_back
#define  pii        pair<int, int>
#define  INOPEN     freopen("in.txt", "r", stdin)
#define  OUTOPEN    freopen("out.txt", "w", stdout)

typedef unsigned long long int ull;
typedef long long int ll;
const int    maxn = 2e5 + 10;
const int    maxm = 1e5 + 10;
const ll     mod  = 1e9 + 7;
const ll     INF  = 1e18 + 100;
const int    inf  = 0x3f3f3f3f;
const double pi   = acos(-1.0);
const double eps  = 1e-8;
using namespace std;

int n, m;
int cas, tol, T;

int s, t;
vector<int> g[maxn];
bool vis[maxn];

void dfs1(int u, int res) {
    if(vis[u])  return ;
    if(u == res)    return ;
    vis[u] = 1;
    for(auto v : g[u])  dfs1(v, res);
}

ll dfs(int u) {
    if(vis[u])  return 0;
    vis[u] = 1;
    ll ans = 1;
    for(auto v : g[u])  
        ans += dfs(v);
    return ans;
}

int main() {
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d%d%d", &n, &m, &s, &t);
        for(int i=1; i<=n; i++) g[i].clear();
        for(int i=1, u, v; i<=m; i++) {
            scanf("%d%d", &u, &v);
            g[u].pb(v), g[v].pb(u);
        }
        ll ans1 = 0, ans2 = 0;
        for(int i=1; i<=n; i++) vis[i] = 0;
        dfs1(t, s);
//      for(int i=1; i<=n; i++) printf("%d%c", vis[i], i==n?'\n':' ');
        ans1 = dfs(s)-1;
        for(int i=1; i<=n; i++) vis[i] = 0;
        dfs1(s, t);
//      for(int i=1; i<=n; i++) printf("%d%c", vis[i], i==n?'\n':' ');
        ans2 = dfs(t)-1;
        printf("%lld\n", ans1*ans2);
    }
    return 0;
}

Beautiful Rectangle

\ [Time Limit: 1 s \
quad Memory Limit: 256 MB \] to be filled pit.

Guess you like

Origin www.cnblogs.com/Jiaaaaaaaqi/p/12046792.html