Codeforces_844

Posted 冷暖知不知

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces_844相关的知识,希望对你有一定的参考价值。

A.统计字母个数。

技术分享
#include<bits/stdc++.h>
using namespace std;

string s;
int n;
map<char,int> mp;

int main()
{
    ios::sync_with_stdio(0);
    cin >> s >> n;
    int cnt = 0;
    for(int i = 0;i < s.length();i++)
    {
        if(mp[s[i]] == 0)   cnt++;
        mp[s[i]] = 1;
    }
    if(s.length() < n)    cout << "impossible" << endl;
    else cout << max(0,n-cnt) << endl;
    return 0;
}
View Code

B.统计每行每列0和1的个数,各自组合数计算数量,最后吧重复的n*m个减去。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,m,a[55][55];

long long c(int x)
{
    long long ans = 1;
    for(int i = 1;i <= x;i++)   ans *= 2;
    return ans-1;
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n >>m;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)   cin >> a[i][j];
    }
    long long ans = -n*m;
    for(int i = 1;i <= n;i++)
    {
        int cnt = 0;
        for(int j = 1;j <= m;j++)
        {
            if(a[i][j] == 1)    cnt++;
        }
        ans += c(cnt);
        ans += c(m-cnt);
    }
    for(int i = 1;i <= m;i++)
    {
        int cnt = 0;
        for(int j = 1;j <= n;j++)
        {
            if(a[j][i] == 1)    cnt++;
        }
        ans += c(cnt);
        ans += c(n-cnt);
    }
    cout << ans << endl;
    return 0;
}
View Code

C.离散化后,统计环的数量。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,a[100005],b[100005],cnt,vis[100005] = {0};
vector<int> v[100005],ans[100005];
map<int,int> mp;

void dfs(int x)
{
    vis[x] = 1;
    ans[cnt].push_back(x);
    for(int i = 0;i < v[x].size();i++)
    {
        int t = v[x][i];
        if(vis[t])  continue;
        dfs(t);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin >> n;
    for(int i = 1;i <= n;i++)   cin >> a[i],b[i] = a[i];
    sort(b+1,b+1+n);
    for(int i = 1;i <= n;i++)   mp[b[i]] = i;
    for(int i = 1;i <= n;i++)
    {
        v[i].push_back(mp[a[i]]);
    }
    cnt = 0;
    for(int i = 1;i <= n;i++)
    {
        if(!vis[i])
        {
            cnt++;
            dfs(i);
        }
    }
    cout << cnt << endl;
    for(int i = 1;i <= cnt;i++) sort(ans[i].begin(),ans[i].end());
    for(int i = 1;i <= cnt;i++)
    {
        cout << ans[i].size() << " ";
        for(int j = 0;j < ans[i].size();j++)    cout << ans[i][j] << " ";
        cout << endl;
    }
    return 0;
}
View Code

D.随机1000个点,剩下线性找1000个,有连续1000个点不被随1000次随到的概率可以忽略。

技术分享
#include<bits/stdc++.h>
using namespace std;

int n,now,x;
map<int,int> mp;

int main()
{
    ios::sync_with_stdio(0);
    srand(time(0));
    cin >> n >> now >> x;
    cout << "? " << now << endl;
    int ne;
    cin >> now >> ne;
    if(now >= x)
    {
        cout << "! " << now << endl;
        return 0;
    }
    for(int i = 1;i <= 1000;i++)
    {
        long long t = (long long)rand()*rand()%n+1;
        cout << "? " << t << endl;
        int v,vv;
        cin >> v >> vv;
        if(v < x && v > now)
        {
            now = v;
            ne = vv;
        }
    }
    while(ne != -1)
    {
        cout << "? " << ne << endl;
        cin >> now >> ne;
        if(now >= x)
        {
            cout << "! " << now << endl;
            return 0;
        }
    }
    cout << "! -1" << endl;
    return 0;
}
View Code

 

以上是关于Codeforces_844的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 844 D. Interactive LowerBound (随机)

CodeForces - 844C Sorting by Subsequences (排序+思维)

Codeforces 844 C. Sorting by Subsequences (循环节)

LeetCode_844-Backspace String Compare

[Codeforces Round #522 (Div. 2, based on Technocup 2019 Elimination Round 3)][C. Playing Piano](代码片段

Leetcode_easy844. Backspace String Compare