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;}
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)(574A,574B)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)A,B
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)A
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A 模拟
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)(573A,573B)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C 模拟
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) E DFS
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)B Bear and Three Musketeers
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks dp
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) A. Bear and Poker gcd
- Codeforces Round #317 [AimFund Thanks-Round] (Div. 2)(A,B)
- springMVC(8) springMVC注解优化
- SpringMVC中使用Interceptor拦截器
- linux ntp ntpdate 同步时间
- Java通过一系列工具打包成不需要JDK支持的exe文件
- 注意Java陷阱
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)(574A,574B)
- [Phonegap+Sencha Touch] 移动开发4 Cordova/Phonegap应用调试方法汇总
- JavaScript 函数调用
- 630-850nm雪崩管apd列表
- github-july-寻找和为定值的多个数
- jedisPool中的jedis实例被还回后是否会断开和redis服务端的socket连接
- iOS 学习资料整理
- linux时间同步,ntpd、ntpdate
- Java内存管理原理及内存区域详解