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;
}



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;
}



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;
}



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;
}



Codeforces_844

时间: 2024-10-12 16:24:08

Codeforces_844的相关文章