Codeforces Round #318 (Div. 2) A、B、C

574A - Bear and Elections
题意:
输入一个数字n,接着输入n个正整数。
题目要求第一个整数要大于其余整数,其余整数每次可以减小1并增加到第一个数字中。
问至少要多少次才能满足要求。

思路:
用优先队列维护一下就可以了。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;

int main()
{
    int n, a;
    while(~scanf("%d", &n))
    {
        scanf("%d", &a);
        priority_queue<int> pq;
        for(int i = 1; i < n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            pq.push(tmp);
        }
        int c = 0;
        int temp = pq.top();
        while(temp >= a)
        {
            pq.pop();
            ++a;
            --temp;
            pq.push(temp);
            ++c;
            temp = pq.top();
        }
        printf("%d\n", c);
    }
}

574B - Bear and Three Musketeers
题意:
输入两个数字n, m。代表有n个人,m对人的关系。
接着输入m行两人之间的关系。
现有三个人的序号为a,b,c。
使a,b,c的关系形成环路,问他们三个人与其他人有关系的最少数量?

思路:
由于m最大值为4000,随意可以采用vecotr存储两人之间的关系,并用深搜的方式来找环路,取最小值即可。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <vector>
#include <stdlib.h>
using namespace std;
vector<int> v[4001];
#define INF 2000000000
int a[10];
int res;
bool vis[4001];
void dfs(int deep, int s)
{
    if(deep == 3 && binary_search(v[s].begin(), v[s].end(), a[0]))
    {
//        printf("%d %d %d\n", a[0], a[1], a[2]);
//        printf("%d %d %d\n", v[a[0]].size(), v[a[1]].size(), v[a[2]].size());
        int f = v[a[0]].size() + v[a[1]].size() + v[a[2]].size() - 6;
//        printf("[%d]\n", f);
        res = min(res, f);
        return ;
    }
    if(deep == 3) return ;
    int sz = v[s].size();
    for(int i = 0; i < sz; i++)
    {
        if(vis[v[s][i]])
        {
            vis[v[s][i]] = false;
            a[deep] = v[s][i];
            dfs(deep + 1, v[s][i]);
            vis[v[s][i]] = true;
        }

    }
}
int main()
{
    int n, m;
    while(~scanf("%d %d", &n, &m))
    {
        for(int i = 1; i <= n; i++)
        {
            v[i].clear();
        }
        for(int i = 0; i < m; i++)
        {
            int x, y;
            scanf("%d %d", &x, &y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        for(int i = 1; i <= n; i++)
        {
            sort(v[i].begin(), v[i].end());
        }
        res = INF;
        memset(vis, true, sizeof(vis));
        for(int i = 1; i <= n; i++)
        {
            if(v[i].size())
            {
                a[0] = i;
                vis[i] = false;
                dfs(1, i);
                vis[i] = true;
            }
        }
        if(res == INF) puts("-1");
        else printf("%d\n", res);
    }
}

573A - Bear and Poker
题意:
输入一个数字n,接着输入n个正整数。
每个整数ai可以变为ai * 2k 或者 ai * 3k,也可以不变。
问这n个数字是否可以经过变换后全部相同。

思路:
把每个数字中2和3的因子全部去掉后,看是否相同即可。

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
long long a[100005];
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%I64d", &a[i]);
            while(a[i] % 3 == 0)
            {
                a[i] /= 3;
            }
            while(a[i] % 2 == 0)
            {
                a[i] /= 2;
            }
        }
        sort(a, a+n);
        if(a[0] == a[n-1]) puts("Yes");
        else puts("No");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值