Codeforces Round #404 (Div. 2)
来源:互联网 发布:apache beam 实时流 编辑:程序博客网 时间:2024/05/22 08:22
简单题。map<string,int>mp累计字符串权值
#include <bits/stdc++.h>
#define scf0(a) scanf("%s", &a)
#define scf1(a) scanf("%d", &a)
#define scf2(a, b) scanf("%d%d", &a, &b)
#define scf3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scf4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define MEM(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using
namespace
std;
int
main() {
map<string,
int
>mp;
mp[
"Tetrahedron"
] = 4;
mp[
"Cube"
] = 6;
mp[
"Octahedron"
] = 8;
mp[
"Dodecahedron"
] = 12;
mp[
"Icosahedron"
] = 20;
string op;
int
n;
cin >> n;
int
sum = 0;
while
(n--) {
cin >> op;
sum += mp[op];
}
cout << sum << endl;
}
B - Anton and Classes 【贪心】
题意:给定两门课的不同课程安排,各选择一个,使得两门课的间隔时间最短。
解法:求两门课最小右界(minn、minm)和最大左界(maxn、maxm)。结果ans = max( 0, max(maxn-minm, maxm-minn) )
(做的时候初始化minn写成0x3f3f3f,差了一个3f...粗心啊)
#include <bits/stdc++.h>
#define scf0(a) scanf("%s", &a)
#define scf1(a) scanf("%d", &a)
#define scf2(a, b) scanf("%d%d", &a, &b)
#define scf3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define scf4(a, b, c, d) scanf("%d%d%d%d", &a, &b, &c, &d)
#define MEM(a, b) memset(a, b, sizeof(a))
#define PII pair<int, int>
using
namespace
std;
int
main() {
int
n, m, x, y;
scf1(n);
int
maxn = -1, minn = 0x3f3f3f3f;
for
(
int
i = 0; i < n; i++) {
scf2(x,y);
maxn = max(maxn, x);
minn = min(minn, y);
}
scf1(m);
int
maxm = -1, minm = 0x3f3f3f3f;
for
(
int
i = 0; i < m; i++) {
scf2(x, y);
maxm = max(maxm, x);
minm = min(minm, y);
}
int
ans = maxm - minn;
ans = max(ans, maxn - minm);
cout << ( ans > 0 ? ans : 0) << endl;
}
C - Anton and Fairy Tale 【数学题】
题意:仓库容量n,每天补充粮食m份但不能超过仓库容量,第i天麻雀吃掉i份粮食,求哪一天仓库会没有粮食
第 i 天 开始 结束
1 n n-1
2 n n-2
3 n n-3
..
m n n-m
-------------------------------
m+1 n n-m-1
m+2 n-1 n-m-3
..
m+x n-x n - m - x(x+1)/2 <= 0
一、直接解方程做法:
所以可以把时间分成两部分:1.前m天(包括第m天)2.第m天以后
解方程 n - m - x(x+1)/2 得 x = ( -1+sqrt(1+8(n-m)) ) / 2
然后!!注意精度问题。。
1.手写个sqrt避免例如sqrt(9)=2.9999向下取整得到2的情况。。
2.总之得到一个手算精确的结果,然后向上取整。
#include <bits/stdc++.h>
using
namespace
std;
long
long
SQRT(
long
long
x) {
long
long
temp =
sqrt
(x);
temp -= 5;
for
(temp;;temp++) {
if
(temp * temp == x) {
return
temp;
}
if
(temp * temp > x) {
return
temp - 1;
}
}
}
int
main() {
long
long
n, m;
cin >> n >> m;
if
(n <= m ) {
cout << n << endl;
return
0;
}
long
long
x = 1 + 8 * (n - m);
long
long
temp = SQRT(x);
//求得一个准确的sqrt
if
(temp * temp == x) {
//如果sqrt后是整数
long
long
ans = -1 + temp;
if
(ans % 2 == 0)
cout << ans / 2 + m << endl;
else
cout << ans / 2 + m + 1 << endl;
}
else
{
//sqrt后不是整数,那肯定要向上取整
long
long
ans = -1 + temp;
cout << ans / 2 + m + 1<< endl;
}
}
但因为sqrt精度丢失只会变小,所以也可以用直接用sqrt得出一个ans,然后judge一下ans是否满足题意( ans*(ans+1)/2 >= n - m )即可。
#include <bits/stdc++.h>
using
namespace
std;
int
main() {
long
long
n, m;
cin >> n >> m;
if
(n <= m ) {
cout << n << endl;
return
0;
}
long
long
x = (
sqrt
( 1 + 8 * (n - m) ) - 1) / 2;
while
(1) {
long
long
temp = (x + 1) * x / 2;
if
(temp >= n - m) {
cout << x + m << endl;
return
0;
}
x++;
}
}
二、二分结果+judge做法:求左界。
#include <bits/stdc++.h>
using
namespace
std;
long
long
n, m;
bool
judge(unsigned
long
long
x) {
if
( x * (x + 1) >= 2 * (n - m) )
return
true
;
else
return
false
;
}
int
main() {
cin >> n >> m;
if
(n <= m) {
cout << n << endl;
return
0;
}
long
long
low = 0LL, high = 1LL << 31;
while
(low < high) {
long
long
mid = low + high >> 1;
if
(judge(mid)) high = mid;
else
low = mid+1;
}
cout <<high + m << endl;
}
- Codeforces Round #404 (Div. 2)
- Codeforces Round #404 (Div. 2)
- Codeforces Round #404 (Div. 2)
- Codeforces Round #404 (Div. 2)
- Codeforces Round #404 (Div. 2) 题解
- Codeforces Round #404 (Div. 2) A题
- Codeforces Round #404 (Div. 2) B题
- Codeforces Round #404 (Div. 2) C题
- Codeforces Round #404 (Div. 2) A&B
- Codeforces Round #404 (Div. 2)(A+B)
- Codeforces Round #404 (Div. 2) 题解(待续)
- 贪心-Codeforces Round #404 (Div. 2)
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- PAT 数列的片段和【简单数论】
- 微信小程序开发深入解读
- 日志输出两次 spring初始化两次
- PAT 天梯赛真题集(L2、L3)
- 用Unity 3D生成真实地形
- Codeforces Round #404 (Div. 2)
- 你应该选择哪一个Web前端框架?,选Vue还是React?
- 基于物品的协同过滤-电影推荐
- Codeforces Round #405 (Div. 2)
- R语言读CSV、txt文件方式以及read.table read.csv 和readr(大数据读取包)
- 将Linux命令的结果作为下一个命令的参数
- C语言通讯录(利用数组实现)
- 51nod 1090 & 1267 【二分简单题】
- 框架整合____Spring整合Mybatis(主流,最精简整合方式,)