Codeforces Round #318
来源:互联网 发布:淘宝的全球购是正品吗 编辑:程序博客网 时间:2024/04/29 16:19
A. Bear and Elections
每次通过优先队列选取最大的数值,并与第一个人的票数l进行比较,大于l,自减1,并记录大于1的次数即为需要更改票数的人数。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <set>#include <map>#include <queue>#include <ctime>#define pb push_back#define ll long long#define mp make_pair#define f first#define s second#define pii pair < int, int >#define ull unsigned long long#define pll pair < ll, ll >#define forit(s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)#define all(s) s.begin(), s.end()const int inf = (1ll << 31) - 1;const int maxn = (int) 1e5 + 10;#define max(a, b) ((a) > (b) ? (a) : (b))#define min(a, b) ((a) < (b) ? (a) : (b)) using namespace std;int n = 0, l = 0;int ans = 0;int main(){ //freopen("data_A.txt", "r", stdin); priority_queue<int> s; ans = 0; scanf("%d%d", &n, &l); for (int i = 1; i < n; i++) { int a = 0; scanf("%d", &a); s.push(a); } while(s.top() >= l) { l++; int a = 0; a = s.top() - 1; s.pop(); s.push(a); ans++; } printf("%d\n", ans); return 0; }
再或者手写优先队列:每次找出数组中的最大值:
#include<iostream>#include<fstream>using namespace std;int n = 0, a[110];int rec = 0, ans = 0;int main(){ //freopen("data_A.txt", "r", stdin); while(scanf("%d", &n) != EOF) { ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } while(true) { rec = 0; for (int i = 1; i < n; i++) { if (a[i] >= a[rec]) { rec = i; } } if (rec == 0) break; a[0]++; a[rec]--; ans++; } printf("%d\n", ans); } return 0;}
B. Bear and Three Musketeers
n个人里面选三个人,除了这三个人之外, 求其他的有多少个人认识这三个人。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <set>#include <map>#include <queue>#include <ctime>#define pb push_back#define ll long long#define mp make_pair#define f first#define s second#define pii pair < int, int >#define ull unsigned long long#define pll pair < ll, ll >#define forit(s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)#define all(s) s.begin(), s.end()const int inf = (1ll << 31) - 1;const int maxn = (int) 1e5 + 10;#define max(a, b) ((a) > (b) ? (a) : (b))#define min(a, b) ((a) < (b) ? (a) : (b)) using namespace std;int n = 0, m = 0, matrix[4100][4100], indegree[4100]; int main(){ //freopen("data_B.txt", "r", stdin); while(scanf("%d%d", &n, &m) != EOF) { memset(matrix, 0, sizeof(matrix)); memset(indegree, 0, sizeof(indegree)); for (int i= 0; i < m; i++) { int a = 0, b = 0; scanf("%d%d", &a, &b); matrix[a][b] = 1; matrix[b][a] = 1; indegree[a]++; indegree[b]++; } int ans = inf; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (matrix[i][j]) { for (int k = j + 1; k <= n; k++) { if (matrix[i][k] && matrix[j][k]) { ans = min (ans, indegree[i] + indegree[j] + indegree[k]); } } } } } if (ans == inf) { printf("-1\n"); }else { printf("%d\n", ans - 6); } } return 0; }
C. Bear and Poker
递归实现超时,只有改用其他方法。改成非递归的方式。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <set>#include <map>#include <queue>#include <ctime>#define pb push_back#define ll long long#define mp make_pair#define f first#define s second#define pii pair < int, int >#define ull unsigned long long#define pll pair < ll, ll >#define forit(s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)#define all(s) s.begin(), s.end()const int inf = (1ll << 31) - 1;const int maxn = (int) 1e5 + 10;#define max(a, b) ((a) > (b) ? (a) : (b))#define min(a, b) ((a) < (b) ? (a) : (b)) using namespace std;int n = 0, a[110000]; int main(){ //freopen("data_C.txt", "r", stdin); while(scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { while (a[i] % 2 == 0) { a[i] /= 2; } while (a[i] % 3 == 0) { a[i] /= 3; } } for (int i = 1; i < n; i++) { if (a[i] != a[0]) { printf("No\n"); return 0; } } printf("Yes\n"); } return 0; }
D. Bear and Blocks
动态规划:从左到右以及从右到左进行遍历,找出其中的最小值。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <cmath>#include <algorithm>#include <set>#include <map>#include <queue>#include <ctime>#define pb push_back#define ll long long#define mp make_pair#define f first#define s second#define pii pair < int, int >#define ull unsigned long long#define pll pair < ll, ll >#define forit(s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it ++)#define all(s) s.begin(), s.end()const int inf = (1ll << 31) - 1;const int maxn = (int) 1e5 + 10;#define max(a, b) ((a) > (b) ? (a) : (b))#define min(a, b) ((a) < (b) ? (a) : (b)) using namespace std;int l[110000], r[110000], dp[110000], ans = 0;int n = 0;int d[110000];int main(){ //freopen("data_D.txt", "r", stdin); while(scanf("%d", &n) != EOF) { ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &d[i]); } l[0] = 1; r[n - 1] = 1; for (int i = 1; i < n; i++) { l[i] = min (l[i - 1] + 1, d[i]); } for (int i = n - 2; i > -1; i--) { r[i] = min (r[i + 1] + 1, d[i]); } for (int i = 0; i < n; i++) { dp[i] = min (l[i], r[i]); } for (int i = 0; i < n; i++) { ans = max (ans, dp[i]); } printf("%d\n", ans); } return 0; }
0 0
- Codeforces Round #318
- Codeforces Round #318 vp
- Codeforces Round #318 (Div. 2)
- Codeforces Round #318(ABCD)
- 【codeforces】Codeforces Round #363
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1)A,B
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)
- 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. 2) B 暴力
- 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. 1)
- Codeforces Round #318 A. Bear and Elections
- Codeforces Round #318 C - Bear and Poker
- Codeforces Round #318(Div. 2) A
- Codeforces Round #318-(C. Bear and Poker)
- HDU 1026 Ignatius and the Princess I 优先队列 +BFS.
- 场景转场动画的Canvas实现
- 利用ArcGIS Server动态图层改变地图服务的图层样式
- BestCoder Round #43 HDU5264 pog loves szh I
- 《第三次浪潮》的体会!
- Codeforces Round #318
- iOS7.0.1的注释插件失效解决办法与相应的配置
- 实现按钮响应的几种方法
- 试一试这样0 0
- ubutun
- 今日学习心得之读取资源文件
- leetcode 1. Two Sum
- 黑马程序员——多线程代码实践及一些思考
- 数据结构【线性表(二)链表】项目之线性表的应用:表的自然连接