codeforces round#510
A. Benches
Description
给出$N$个座位, 每个座位上初始有$a_i$ 个人, 这些人都不能移动。
另外还有$M$个人, 要求让他们坐到座位上, $max$ 为所有座位上人数最多 的 人数。
求出 $max$ 的可能最大值和 最小值。
Solution
最大值就是 给出的最多的人数 的座位 加上 $M$
最小值可以二分答案, 容易求出
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 1e3; int n, m, a[N], maxn; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(;c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} bool jud(int minn) {
int rest = m;
for(int i = ; i <= n; ++i) {
if(a[i] > minn) return false;
rest -= minn - a[i];
}
if(rest > ) return false;
else return true;
} int main()
{
n = rd; m = rd;
for(int i = ; i <= n; ++i)
a[i] = rd;
for(int i = ; i <= n; ++i)
maxn = max(maxn, a[i]);
int l = , r = 2e4 + , ans = ;
while(l <= r) {
int mid = (l + r) >> ;
if(jud(mid)) r = mid - , ans = mid;
else l = mid + ;
}
printf("%d %d\n", ans, maxn + m);
}
A. Benches
B. Vitamins
Description
给出$N$种维生素药, 第$a_i$种药包含了$A,B,C,$中的其中一种或多种维生素, 每一种药都有其价格$cost_i$
现要求出 能吃到维生素 $A,B,C$ 至少花费多少钱。
Solution
显然, 药最多只有7种, 用$2$进制 $S$ 表示是否含有某种维生素, 该种药的最小价格记为 $minn[S]$
在输入时,我们把第 $i$ 种药 更新到对应的 含有维生素的集合S。
求最后的答案时, 只需要三种枚举就行了:
$1$ : 买一种药, 对应集合 $111(B)$
$2$ : 买两种药 $i$ 和 $j$, 并且$i \ | \ j \ = \ 111(B)$
$3$: 买三种药 $i$ 和 $j$, 并且$i \ | \ j \ | \ k \ = \ 111(B)$
求其中的最小值即可。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 1e3 + ;
const int inf = ~0U >> ; int n, v[], minn[], ans = inf; char s[]; struct node {
int cost, vis;
}a[N]; int main()
{
for(int i = ; i < ; ++i)
minn[i] = inf;
scanf("%d", &n);
for(int i = ; i <= n; ++i) {
memset(s, ,sizeof(s));
memset(v, , sizeof(v));
scanf("%d%s", &a[i].cost, s);
for(int j = , len = strlen(s); j < len; ++j)
a[i].vis |= << (s[j] - 'a');
minn[a[i].vis] = min(minn[a[i].vis], a[i].cost);
}
ans = min(ans, minn[]);
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
if((i | j) == ) ans = min(ans, minn[i] + minn[j]);
for(int i = ; i < ; ++i)
for(int j = ; j < ; ++j)
for(int k = ; k < ; ++k)
if((i | j | k) == )
ans = min(ans, minn[i] + minn[j] + minn[k]);
printf("%d\n", ans == inf ? - : ans);
}
B. Vitamins
C. Array Product
Description
给出一个有$N$项的序列${a}$, 有两种操作
$1$: 选择两个数 $i$ $j$, 把$j$ 位置上的数更新为$a_j * a_i$, 将$i$ 位置上的数删去(删去后不能再进行操作)
$2$: 选择一个数$i$, 将 $i$ 位置上的数直接删去 ( 该种操作最多只能进行$1$次)
问如何操作才能使 $N-1$ 次操作后剩下的那个数最大, 输出操作的方案。
Solution
大佬说这是前四题最不可做的, 所以我 先水了D题, 最后还WA了两次才 在1:52的时候过(好像我rank并没有升多少
有如下4中情况:
$1$ : 序列中有 奇数个 负数, 并且 存在 $0$—— 将所有的 $0$ 合并成一个,再把 $0$ 和 绝对值最小的 负数 合并 得到 $0$, 删去这个 $0$ 后 把剩余的数合并。
$2$ : 序列中有 奇数个 负数, 并且 不存在 $0$—— 直接删除 绝对值最小的负数, 剩下的合并。
$3$ : 序列中有 偶数个 负数, 并且存在 $0$—— 把所有的$0$ 合并成一个, 把 $0$ 删去, 剩下的数合并
$4$ : 序列中有 偶数个负数, 并且 不存在 $0$—— 直接合并。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define rd read()
using namespace std; const int N = 2e5 + ;
const int inf = ~0U >> ; int a[N], vis[N], tot, n, rest; vector<int> cur0, cur1; int read() {
int X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} int main()
{
n = rd;
rest = n - ;
a[] = -inf;
for(int i = ; i <= n; ++i) {
a[i] = rd;
if(a[i] < ) tot++;
if(a[i] < ) cur1.push_back(i);
if(a[i] == ) cur0.push_back(i);
}
/*if((tot & 1)) {
int pos = 0;
for(int i = 0, len = cur1.size(); i < len; ++i)
if(a[pos] < a[cur1[i]]) pos = cur1[i];
vis[pos] = 1;
printf("2 %d\n", pos);
rest--;
}
for(int i = 0, len = cur0.size(); i < len - 1 && rest; ++i) {
vis[cur0[i]] = 1;
printf("1 %d %d\n", cur0[i], cur0[i + 1]);
rest--;
}
if(tot % 2 == 0 && cur0.size() && rest) {
printf("2 %d\n", cur0[(int)cur0.size() - 1]);
vis[cur0[(int)cur0.size() - 1]] = 1;
rest --;
}*/ if(tot % && cur0.size()) {
for(int i = , len = cur0.size(); i < len - && rest; ++i) {
vis[cur0[i]] = ;
printf("1 %d %d\n", cur0[i], cur0[i + ]);
rest--;
}
int pos = ;
for(int i = , len = cur1.size(); i < len; ++i)
if(a[pos] < a[cur1[i]]) pos = cur1[i];
printf("1 %d %d\n", cur0[(int)cur0.size() - ], pos);
rest--;
vis[cur0[(int)cur0.size() - ]] = ;
if(rest) {
printf("2 %d\n", pos);
vis[pos] = ;
rest--;
}
}
else if(cur0.size()) {
for(int i = , len = cur0.size(); i < len - && rest; ++i) {
vis[cur0[i]] = ;
printf("1 %d %d\n", cur0[i], cur0[i + ]);
rest--;
}
if(rest) {
printf("2 %d\n", cur0[(int)cur0.size() - ]);
vis[cur0[(int)cur0.size() - ]] = ;
rest --; }
}
else if(!cur0.size() && tot % ) {
int pos = ;
for(int i = , len = cur1.size(); i < len; ++i)
if(a[pos] < a[cur1[i]]) pos = cur1[i];
printf("2 %d\n", pos);
rest--;
vis[pos] = ;
}
for(int i = , j = ; rest;) {
while(vis[i] && i <= n) i++;
j = i + ;
while(vis[j] && j <= n) j++;
printf("1 %d %d\n", i, j);
vis[i] = ;
rest--;
} }
C. Array Product
D. Petya and Array
Description
求出有多少个 子段的 和 $<t$
Solution
跟树状数组求逆序对一样的原理。
我不会用语言表述 $QuQ$ (╥╯^╰╥)
简单说, 就是先求出前缀和$sum$, 当前我们需要求出以$i$ 为结尾的子段有多少个满足$sum[i] \ - \ sum[j] \ < \ t$,$0<=j<i$
这样而树状数组求静态逆序对就相当于 $t = 0$时的做法了。
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define ll long long
using namespace std; const int N = 2e5 + ; int n, a[N];
ll qsum[N], t, b[N], tot;
ll sum[N], ans; ll read() {
ll X = , p = ; char c = getchar();
for(; c > '' || c < ''; c = getchar()) if(c == '-') p = -;
for(; c >= '' && c <= ''; c = getchar()) X = X * + c - '';
return X * p;
} int fd(ll x) {
int tmp = lower_bound(b + , b + + tot, x) - b;
if(b[tmp] == x) return tmp;
else return tmp - ;
} int lowbit(int x) {
return x & -x;
} ll query(int x) {
ll re = ;
for(; x; x -= lowbit(x)) re += sum[x];
return re;
} void add(int x) {
for(; x <= tot; x += lowbit(x))
sum[x]++;
} int main()
{
n = rd; t = rd;
for(int i = ; i <= n; ++i)
a[i] = rd;
for(int i = ; i <= n; ++i) {
qsum[i] = qsum[i - ] + a[i];
b[++tot] = qsum[i];
}
b[++tot] = ;
sort(b + , b + + tot);
tot = unique(b + , b + + tot) - b - ;
add(fd());
for(int i = ; i <= n; ++i) {
int tmp = fd(qsum[i] - t);
ans += (i - query(tmp));
tmp = fd(qsum[i]);
add(tmp);
}
printf("%I64d\n", ans);
}
D. Petya and Array
codeforces round#510的更多相关文章
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #510 (Div. 2) B. Vitamins
B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #510 #C Array Product
http://codeforces.com/contest/1042/problem/C 给你一个有n个元素序列,有两个操作:1,选取a[i]和a[j],删除a[i],将$a[i]*a[j]$赋值给a ...
- Codeforces Round #510 #B
http://codeforces.com/contest/1042/problem/B 题意: 给出n种饮料,每种饮料还有一种或多种维生素(A或B或C),某人想集齐三种维生素,问最少需要花费多少? ...
- Codeforces Round #510 #A
http://codeforces.com/contest/1042/problem/A 题目大意就是: 现在公园里有n个长椅(要多长有多长),第i个长椅上有a[i]个人(泰山崩于前而不乱),现在又有 ...
- Codeforces Round #510 (Div. 2)(C)
传送门:Problem C https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 给你n个数,定义有两种操作 ① 1 i j : (i != ...
- Codeforces Round #510 (Div. 2)(B)
传送门:Problem B https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 如果可以通过喝果汁将维生素A,B,C全部摄取,求最小花费,如 ...
- Codeforces Round #510 (Div. 2)(A)
传送门:Problem A https://www.cnblogs.com/violet-acmer/p/9682082.html 题意: 公园里有n个沙滩,a[i]表示第i个沙滩初始人数,现有m个人 ...
随机推荐
- 05_ssm基础(三)之Spring基础
11.spring入门引导 12.spring_HelloWord程序 实现步骤: 0.找到spring压缩包,并解压 1.拷贝jar包 2.添加主配置文件(官方文档约28页) 3.在测试中使用 13 ...
- 解题4(NumberToEnglish )
题目描述 Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文: 如22:twenty two,123:one hundred and twenty three. 说明: 数字为正整数, ...
- ftp删除目录和文件,目录下有文件删除提示【550 Remove directory operation failed.】
注意:目录下有文件,直接删除目录会失败,提示550 Remove directory operation failed. 必须先将目录下的文件都删除,才能删除目录 ftp命令行: ftp删除目 ...
- 【翻译】View Frustum Culling --2 Geometric Approach – Extracting the Planes
在上一篇中,我们知道了视锥体的形状,并且也确定了我们进行裁剪时的步骤.那我们接下来要走的就是确定视锥体的六个平面: near, far, top, bottom, left and right 2.计 ...
- 调整数组顺序使奇数位于偶数前面(python)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. # -*- codi ...
- Dbutils 的JDBC链接
最近实践周,再次用到了这个Apache的开源工具包就在次对着个方面进行了复习 1.JDBC的基本 操作姿势 2.JDBC的步骤是 1.添加驱动包 Class.forName("oracle. ...
- ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)
There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...
- ajax中的contendType和dataType知识点梳理
在ajax中有2个参数比较重要,之前一直没有搞清楚,下面我们开始梳理一下 1.contentType字段:这个字段的意思,ajax发送给后端的数据是什么类型 如果在ajax中不指定这个属性,则默认是u ...
- 662. Maximum Width of Binary Tree二叉树的最大宽度
[抄题]: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...
- [leetcode]252. Meeting Rooms会议室有冲突吗
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...