码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces_844

时间:2017-08-26 22:08:54      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:sed   bsp   合数   color   离散化   忽略   i++   技术分享   lap   

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

标签:sed   bsp   合数   color   离散化   忽略   i++   技术分享   lap   

原文地址:http://www.cnblogs.com/zhurb/p/7436414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!