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)的更多相关文章

  1. CodeForces 122G Lucky Array(一脸懵逼的树状数组)

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...

  2. CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)

    The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...

  3. 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 ...

  4. codeforces 792CDivide by Three(两种方法:模拟、动态规划

    传送门:https://codeforces.com/problemset/problem/792/C 题意:给你一个字符串,要求让你删除最少个数的元素,使得最终答案是没有前导0并且是3的倍数. 题解 ...

  5. CodeForces 710CMagic Odd Square(经典-奇数个奇数&偶数个偶数)

    题目链接:http://codeforces.com/problemset/problem/710/C 题目大意:输入一个奇数n,则生成n*n矩阵,要求矩阵的行.列还有斜着,所有元素之和为奇数. 解题 ...

  6. Codeforces Gym100502G:Outing(缩点+有依赖的树形背包)

    http://codeforces.com/gym/100502/attachments 题意:有n个点,容量为tol,接下来n个关系,表示选了第i个点,那么第xi个点就必须被选.问最多可以选多少个点 ...

  7. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  8. Codeforces 917D - Stranger Trees(矩阵树定理/推式子+组合意义)

    Codeforces 题目传送门 & 洛谷题目传送门 刚好看到 wjz 在做这题,心想这题之前好像省选前做过,当时觉得是道挺不错的题,为啥没写题解呢?于是就过来补了,由此可见我真是个大鸽子(( ...

  9. Codeforces 772D - Varying Kibibits(高维差分+二项式定理维护 k 次方和)

    Codeforces 题目传送门 & 洛谷题目传送门 首先很容易注意到一件事,那就是对于所有 \(f(S)\) 可能成为 \(x\) 的集合 \(S\),必定有 \(\forall y\in ...

随机推荐

  1. python备份mysql数据库

    介绍使用python结合mysqldump对mysql数据库进行备份 import os import sys import configparser import time def test_fil ...

  2. 写给iOS小白的MVVM教程(序)

    这几天,需要重构下部分代码,这里简要记录下.但是涉及的技术要点还是很多,所以分为多个篇章叙述.此教程来源于,并将于应用于实践,不做过多的概念性阐释和争论.每个篇章都会附上实际的可执行的代码.因涉及的技 ...

  3. 利用Selenium+java实现淘宝自动结算购物车商品(附源代码)

    转载请声明原文地址! 本次的主题是利用selenium+java实现结算购买购物车中的商品. 话不多说,本次首先要注意的是谷歌浏览器的版本,浏览器使用的驱动版本,selenium的jar包版本.   ...

  4. echarts零基础快速入门

    第一步:得到这个dom对象.然后进行各种操作. var myChart = echarts.init(document.getElementById('item1')); 第二步:所有的配置项全部放在 ...

  5. (转)为什么国外 MMORPG 中不采用自动寻路等功能?

    不只是自动寻路,现在网游中的教学引导系统,辅助系统的功能强大程度,友好程度都可以说到了变态的程度,开发这些功能投入的资源甚至要超过游戏内容本身.究其原因,还是竞争越来越激烈,人心越来越浮躁,游戏商家为 ...

  6. 内置函数系列之 map

    map(映射函数)语法: map(函数,可迭代对象) 可以对可迭代对象中的每一个元素,分别执行函数里的操作 # 1.计算每个元素的平方 lst = [1,2,3,4,5] lst_new = map( ...

  7. c#常用数据结构解析【转载】

    引用:http://blog.csdn.net/suifcd/article/details/42869341 前言:可能去过小匹夫博客的盆油们读过这篇对于数据结构的总结,但是小匹夫当时写那篇文章的时 ...

  8. MySQL创建民族表的SQL语句

    MySQL创建民族表的SQL语句 CREATE TABLE `nation` ( `id` ) unsigned NOT NULL AUTO_INCREMENT, `nation` ) NOT NUL ...

  9. 基于form表单submit提交不跳转

    方法一:target <html> <body> <form action="" method="post" target=&qu ...

  10. C语言数组篇(二)指针数组和数组指针

    数组指针 和 指针数组         这两个名词可以说是经常搞混了         数组指针--> 数组的指针       就是前面讲的 指向数组a的指针p;         指针数组--&g ...