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

来源:互联网 发布:ubuntu安装qq2016 编辑:程序博客网 时间:2024/05/22 04:01

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


0 0