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 ...
随机推荐
- JavaScript的执行机制
JavaScript是单线程的,任务的执行时自上而下的,这就有了一个问题,当遇到一个比较耗时的任务时,下面的代码就会被阻塞,这就意味着卡死.所以js是有异步的,它的实现主要是通过事件循环(event ...
- Json数据常用操作
JSON字符串: var str1 = '{ "name": "cs", "sex": "man" }'; JSON对象 ...
- 1816: [Cqoi2010]扑克牌
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2737 Solved: 1082[Submit][Status][Discuss] Descripti ...
- ethereum(以太坊)(十三)--异常处理/元祖
pragma solidity ^0.4.4; contract Students{ uint[] data= new uint[](4); address _owner = msg.sender; ...
- Redis------Set集合类型
set是string类型的无序集合 类比:你的朋友不能超过2的32次方-1个元素 基本添加删除操作 取并集 取交集 取差集 注意:每个元素的各个元素不能重复 应用场合:qq好友推荐‘ TomFri 的 ...
- 阿里云Linux服务器,挂载硬盘并将系统盘数据迁移到数据盘
因为之前用宝塔上线,宝塔只挂载了系统盘50G,打开阿里云云盘列表发现系统盘无法直接升级,故另买一块数据盘挂载到Linux服务器下,下面根据网上教程再结合我实际情况讲解一下实际操作,其实非常easy l ...
- JDK学习---深入理解java中的LinkedList
本文参考资料: 1.<大话数据结构> 2.http://blog.csdn.net/jzhf2012/article/details/8540543 3.http://blog.csdn. ...
- c++右值引用
右值 右值是相对与左值来说的. 左值是以变量的形式存在,指向一个指定的内存,可以对它取地址.右值就是不指向任何地方,它是暂时和短命的,不能对它取地址. 右值引用 把临时的.生命周期短的值,绑定到一个变 ...
- MySQL 5.7远程连接
将/etc/mysql/my.cnf中的bind_address那一行注释掉或修改为"bind_address=0.0.0.0": bind_address并没有在/etc/mys ...
- STM32无法使用IAR下载程序问题
一开始建立了工程,然后程序下载都很正常.不知道什么情况自己下载代码之后,再重新下载代码无法成功. 我按照提示找了一下FlashStm32f30x8.flash这个文件,却发现IAR的目录下没并没有.又 ...