Codeforces 433 Div.2(A、B、C、D)
A. Fraction
暴力遍历1-1000,取组成的真分数比值最大且分子分母gcd为1时更新答案
代码:
- #include <stdio.h>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- #include <bitset>
- #include <string>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LC(x) (x<<1)
- #define RC(x) ((x<<1)+1)
- #define MID(x,y) ((x+y)>>1)
- #define fin(name) freopen(name,"r",stdin)
- #define fout(name) freopen(name,"w",stdout)
- #define CLR(arr,val) memset(arr,val,sizeof(arr))
- #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
- typedef pair<int, int> pii;
- typedef long long LL;
- const double PI = acos(-1.0);
- int main(void)
- {
- int n;
- while (~scanf("%d", &n))
- {
- double mm = 0;
- int aa, bb;
- for (int a = 1; a <= n; ++a)
- {
- for (int b = a + 1; b <= n; ++b)
- {
- if (a + b == n && a < b && (a * 1.0 / b) > mm && __gcd(a, b) == 1)
- {
- aa = a; bb = b;
- mm = a * 1.0 / b;
- }
- }
- }
- printf("%d %d\n", aa, bb);
- }
- return 0;
- }
B. Maxim Buys an Apartment
最好的情况是尽量放中间,每一个房子都可以造成两个good位置,这样一共有n/3个位置,如果k<=n/3则答案就是k*3,否则把剩余的位置先放到对答案没影响的,再放到对答案影响为1的地方。
代码:
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- #include <bitset>
- #include <string>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LC(x) (x<<1)
- #define RC(x) ((x<<1)+1)
- #define MID(x,y) ((x+y)>>1)
- #define fin(name) freopen(name,"r",stdin)
- #define fout(name) freopen(name,"w",stdout)
- #define CLR(arr,val) memset(arr,val,sizeof(arr))
- #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
- typedef pair<int, int> pii;
- typedef long long LL;
- const double PI = acos(-1.0);
- int main(void)
- {
- LL n, k;
- while (cin >> n >> k)
- {
- if (k == 0 || k == n)
- puts("0 0");
- else
- {
- if (n == 2)
- puts("1 1");
- else
- {
- if (k <= (n / 3))
- printf("1 %I64d\n", k * 2);
- else
- {
- LL need = n / 3;
- LL res = k - need;
- printf("1 %I64d\n", need * 2 - (res - (n - need * 3)));
- }
- }
- }
- }
- return 0;
- }
C. Planning
贪心枚举当前时间k,让1-k时间内花费最多的航班最优降落即可,弱比我想到的是线段树,而大牛都是set……
代码:
- #include <stdio.h>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- #include <bitset>
- #include <string>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LC(x) (x<<1)
- #define RC(x) ((x<<1)+1)
- #define MID(x,y) ((x+y)>>1)
- #define fin(name) freopen(name,"r",stdin)
- #define fout(name) freopen(name,"w",stdout)
- #define CLR(arr,val) memset(arr,val,sizeof(arr))
- #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
- typedef pair<int, int> pii;
- typedef long long LL;
- const double PI = acos(-1.0);
- const int N = 6e5 + 7;
- struct seg
- {
- int l, mid, r;
- int v, id;
- } T[N << 2];
- int arr[N];
- int Time[N];
- inline void pushup(int k)
- {
- if (T[LC(k)].v > T[RC(k)].v)
- {
- T[k].v = T[LC(k)].v;
- T[k].id = T[LC(k)].id;
- }
- else
- {
- T[k].v = T[RC(k)].v;
- T[k].id = T[RC(k)].id;
- }
- }
- void build(int k, int l, int r)
- {
- T[k].l = l;
- T[k].r = r;
- T[k].mid = MID(l, r);
- if (l == r)
- {
- T[k].v = arr[l];
- T[k].id = l;
- }
- else
- {
- build(LC(k), l, T[k].mid);
- build(RC(k), T[k].mid + 1, r);
- pushup(k);
- }
- }
- void update(int k, int x)
- {
- if (T[k].l == T[k].r)
- T[k].v = -INF;
- else
- {
- if (x <= T[k].mid)
- update(LC(k), x);
- else
- update(RC(k), x);
- pushup(k);
- }
- }
- int query(int k, int l, int r)
- {
- if (l <= T[k].l && T[k].r <= r)
- return T[k].id;
- else
- {
- if (r <= T[k].mid)
- return query(LC(k), l, r);
- else if (l > T[k].mid)
- return query(RC(k), l, r);
- else
- {
- int ll = query(LC(k), l, r);
- int rr = query(RC(k), l, r);
- if (arr[ll] > arr[rr])
- return ll;
- else
- return rr;
- }
- }
- }
- int main(void)
- {
- int n, k, i;
- while (~scanf("%d%d", &n, &k))
- {
- for (i = 1; i <= n; ++i)
- scanf("%d", &arr[i]);
- build(1, 1, n);
- LL ans = 0;
- for (i = k + 1; i <= k + n; ++i)
- {
- int pos = query(1, 1, min(i, n));
- ans = ans + (LL)(arr[pos]) * (LL)(i - pos);
- update(1, pos);
- arr[pos] = -INF;
- Time[pos] = i;
- }
- printf("%I64d\n", ans);
- for (i = 1; i <= n; ++i)
- printf("%d%c", Time[i], " \n"[i == n]);
- }
- return 0;
- }
D. Jury Meeting
前缀+后缀思想,主要用两个二维数组pre和suf和维护当前某地最少花费的cost数组,用pre[i][0]表示当前到第i天为止所有能到达0点的人最少花费,pre[i][1]表示有几个人能到达0点,因此枚举i,把航班天数小于等于i的数据都拿去更新cost,再结合cost更新pre,类似于前缀最小值一样处理一遍,然后返程suf数组做一个与pre相反顺序的后缀最小花费处理,然后枚举到达和离开的区间[l,l+k+1],检查一下区间合法性和区间内是否可以到齐,再更新答案即可
代码;
- #include <stdio.h>
- #include <iostream>
- #include <algorithm>
- #include <cstdlib>
- #include <cstring>
- #include <bitset>
- #include <string>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <set>
- #include <map>
- using namespace std;
- #define INF 0x3f3f3f3f
- #define LC(x) (x<<1)
- #define RC(x) ((x<<1)+1)
- #define MID(x,y) ((x+y)>>1)
- #define fin(name) freopen(name,"r",stdin)
- #define fout(name) freopen(name,"w",stdout)
- #define CLR(arr,val) memset(arr,val,sizeof(arr))
- #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
- typedef pair<int, int> pii;
- typedef long long LL;
- const double PI = acos(-1.0);
- const int N = 1000010;
- struct info
- {
- int d, s, t, c;
- };
- info A[N], B[N];
- LL pre[N][2], suf[N][2], cost[N];
- void init()
- {
- CLR(pre, 0);
- CLR(suf, 0);
- }
- int main(void)
- {
- int n, m, k, i;
- while (~scanf("%d%d%d", &n, &m, &k))
- {
- init();
- int cnta = 0, cntb = 0;
- int maxday = 0;
- for (i = 0; i < m; ++i)
- {
- int d, s, t, c;
- scanf("%d%d%d%d", &d, &s, &t, &c);
- if (t == 0)
- A[cnta++] = (info) {d, s, t, c};
- else
- B[cntb++] = (info) {d, s, t, c};
- maxday = max(maxday, d);
- }
- sort(A, A + cnta, [](info a, info b) {return a.d < b.d;});
- sort(B, B + cntb, [](info a, info b) {return a.d > b.d;});
- LL ps = 0, pc = 0;
- int cur = 0;
- CLR(cost, 0);
- for (i = 1; i <= maxday; ++i)
- {
- while (A[cur].d <= i && cur < cnta)
- {
- if (!cost[A[cur].s])
- {
- cost[A[cur].s] = A[cur].c;
- ps += A[cur].c;
- ++pc;
- }
- else if (cost[A[cur].s] > A[cur].c)
- {
- ps -= cost[A[cur].s];
- ps += A[cur].c;
- cost[A[cur].s] = A[cur].c;
- }
- ++cur;
- }
- pre[i][0] = ps;
- pre[i][1] = pc;
- }
- cur = 0;
- ps = 0;
- pc = 0;
- CLR(cost, 0);
- for (i = maxday; i >= 1; --i)
- {
- while (B[cur].d >= i && cur < cntb)
- {
- if (!cost[B[cur].t])
- {
- cost[B[cur].t] = B[cur].c;
- ps += B[cur].c;
- ++pc;
- }
- else if (cost[B[cur].t] > B[cur].c)
- {
- ps -= cost[B[cur].t];
- ps += B[cur].c;
- cost[B[cur].t] = B[cur].c;
- }
- ++cur;
- }
- suf[i][0] = ps;
- suf[i][1] = pc;
- }
- LL ans = 0x3f3f3f3f3f3f3f3f;
- for (i = 1; i <= maxday; ++i)
- {
- if (i + k + 1 <= maxday && pre[i][1] >= n && suf[i + k + 1][1] >= n && pre[i][0] + suf[i + k + 1][0] < ans)
- ans = pre[i][0] + suf[i + k + 1][0];
- }
- printf("%I64d\n", ans == 0x3f3f3f3f3f3f3f3f ? -1 : ans);
- }
- return 0;
- }
Codeforces 433 Div.2(A、B、C、D)的更多相关文章
- CodeForces 122G Lucky Array(一脸懵逼的树状数组)
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- Codeforces Round #258 (Div. 2)(A,B,C,D)
题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...
- codeforces 792CDivide by Three(两种方法:模拟、动态规划
传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...
- CodeForces 710CMagic Odd Square(经典-奇数个奇数&偶数个偶数)
题目链接:http://codeforces.com/problemset/problem/710/C 题目大意:输入一个奇数n,则生成n*n矩阵,要求矩阵的行.列还有斜着,所有元素之和为奇数. 解题 ...
- Codeforces Gym100502G:Outing(缩点+有依赖的树形背包)
http://codeforces.com/gym/100502/attachments 题意:有n个点,容量为tol,接下来n个关系,表示选了第i个点,那么第xi个点就必须被选.问最多可以选多少个点 ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- Codeforces 917D - Stranger Trees(矩阵树定理/推式子+组合意义)
Codeforces 题目传送门 & 洛谷题目传送门 刚好看到 wjz 在做这题,心想这题之前好像省选前做过,当时觉得是道挺不错的题,为啥没写题解呢?于是就过来补了,由此可见我真是个大鸽子(( ...
- Codeforces 772D - Varying Kibibits(高维差分+二项式定理维护 k 次方和)
Codeforces 题目传送门 & 洛谷题目传送门 首先很容易注意到一件事,那就是对于所有 \(f(S)\) 可能成为 \(x\) 的集合 \(S\),必定有 \(\forall y\in ...
随机推荐
- 基于PHP的微信公众平台开发(TOKEN验证,消息回复)
微信公众平台开发 实现步骤: 第一步:填写服务器配置 登录微信公众平台官网后,在公众平台后台管理页面 - 开发者中心页,点击“修改配置”按钮,填写服务器地址(URL).Token和EncodingAE ...
- IDEA搭建SSM出现的一些错误
下面是我这几天整合SpringMVC+Spring+MyBatis框架遇到的一些问题 ,在这里总结一下: 1:HTTP Status 500 - Request processing failed; ...
- css样式 body的font-size 为什么用625%
浏览器的默认高度?一般为16px. 为什么用62.5%作为body的默认样式?16px62.5%=10px.* 那么为什么一般多是 16px *625% = 100px; <响应式Web设计实 ...
- Redis ---------- 持久化(AOF)操作
每小时做一次快照持久化 8:00 快照持久化 9:00 快照持久化 10:00 快照持久化 上帝想玩弄人类,突然停电,55万个key丢失了 11:00 快照持久化 解决方案: 8:00-9:00在 ...
- 小游戏banner广告流量量主指引
小程序导航 https://wq.xmaht.top
- R-biomaRt使用-代码备份
目标:使用R脚本从ensembl上下载transcript数据 简单粗暴,直接上代码.biomaRt的介绍晚一点更新. # this file helps extract information fr ...
- Small Talk Matters【闲谈很重要】
Small Talk Matters We' ve all been there: in a lift, in line at the bank or on an airplane, 我们都有过这样的 ...
- 如何查询进程中占用CPU的线程
top -c 命令查找进程PID top -Hp PID 找进程中的线程号 echo %x 线程号 将线程转换成16进制 jstack PID |grep ...
- RHCE考试
RHCSA_PDF版传送门:https://files.cnblogs.com/files/zhangjianghua/RHCSA%E8%AF%95%E9%A2%98.pdf RHCE_PDF版传送门 ...
- NPM安装vue-cli,并创建vue+webpack项目模板
1.安装npm npm 是node.js 的包管理工具, 安装流程地址:https://docs.npmjs.com/cli/install 估计会非常慢,我们可以使用淘宝NPM镜像下载安装:htt ...