Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)(574A,574B)

1.Bear and Elections

题目链接:

http://codeforces.com/problemset/problem/574/A

解题思路:

模拟即可。。。

AC代码:

#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;

multiset<int> s;
multiset<int>::iterator it;

int main(){
    int n;
    while(~scanf("%d",&n)){
        int ans,sum = 0;
        s.clear();
        scanf("%d",&ans);
        int x,tmp;
        for(int i = 2; i <= n; i++){
            scanf("%d",&x);
            s.insert(x);
        }
        it = s.end();
        it--;
        while(ans <= *it){
            sum++;
            ans++;
            tmp = (*it) - 1;
            s.erase(it);
            s.insert(tmp);
            it = s.end();
            it--;
        }
        printf("%d\n",sum);
    }
    return 0;
}


2.Bear and Three Musketeers

题目链接:

http://codeforces.com/problemset/problem/574/B

解题思路:

Warriors are vertices and "knowing each other" is an edge. We want to find connected triple of vertices with the lowest sum of 

degrees (and print sum - 6 because we don't want to count edges from one chosen vertex to another).

Brute force is O(n3). We iterate over all triples a, b, c and consider them as musketeers. They must be connected by edges (they 

must know each other). If they are, then we consider sum of their degrees.

We must notice that there is low limit for number of edges. So instead of iterating over triples of vertices we can iterate over edges 

and then iterate over third vertex. It gives us O(n2 + nm) and it's intended solution. To check if third vertex is connected with other 

two, you should additionally store edges in 2D adjacency matrix.

It's also possible to write it by adding "if" in right place in brute forces to get O(n2 + nm)

AC代码:

#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 5005;
int degree[maxn];
bool t[maxn][maxn];

int main(){
	int n,m;
	scanf("%d%d", &n, &m);
	for(int i = 0; i < m; ++i){
		int a, b;
		scanf("%d%d", &a, &b);
		t[a][b] = t[b][a] = true;
		degree[a]++;
		degree[b]++;
	}
	int result = INF;
	for(int i = 1; i <= n; i++)
		for(int j = i + 1; j <= n; j++){
			if(t[i][j]) {
				for(int k = j + 1; k <= n; ++k){
					if(t[i][k] && t[j][k])
						result = min(result, degree[i]+degree[j]+degree[k]);
				}
        }
    }
	if(result == INF)
        printf("-1\n");
	else
        printf("%d\n", result - 6);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值