AtCoder Beginner Contest 145
A - Circle
签到。
B - Echo
签到到。
C - Average Length
要卡下精度,可用二分或者long double来搞。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 20:04:44
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 10;
const double eps = 1e-6;
int n;
struct Point{
int x, y;
}p[N];
int a[N];
double dis(Point A, Point B) {
double tmp = 1.0 * (A.x - B.x) * (A.x - B.x) + 1.0 * (A.y - B.y) * (A.y - B.y);
double l = 0, r = tmp, mid;
for(int i = 1; i <= 500; i++) {
mid = (l + r) / 2;
if(mid * mid < tmp) l = mid;
else r = mid;
}
return r;
}
void run(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
for(int i = 1; i <= n; i++) a[i] = i;
double ans = 0;
int tot = 0;
do {
++tot;
for(int i = 2; i <= n; i++) {
ans += dis(p[a[i]], p[a[i - 1]]);
}
} while(next_permutation(a + 1, a + n + 1));
ans = ans / tot;
cout << ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
D - Knight
题意:
现在有个棋子位于\((0,0)\)点,当棋子位于\((i,j)\)时,可以跳向\((i+1,j+2),(i+2,j+1)\)这两个格子。
问有多少种方式可以到点\((x,y)\)。
思路:
由于\(x,y\leq 10^6\),显然直接\(dp\)不行。
然后找规律,从终点往回跳来模拟一下,发现最终会形成多条斜率为\(1\)的直线,每跳直线上面相关点的答案为一个组合数。
然后发现最终\((0,0)\)点所在的层数为\(\frac{x+y}{3}\),最终答案就是\(\frac{x+y}{3}\choose t\)。
细节在纸上画一下就出来了。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 20:26:13
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 15, MOD = 1e9 + 7;
int fac[N];
ll qpow(ll a, ll b) {
ll ans = 1;
while(b) {
if(b & 1) ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
int C(int n, int m) {
return 1ll * fac[n] * qpow(fac[n - m], MOD - 2) % MOD * qpow(fac[m], MOD - 2) % MOD;
}
void run(){
fac[0] = 1;
for(int i = 1; i < N; i++) fac[i] = 1ll * fac[i - 1] * i % MOD;
int x, y;
cin >> x >> y;
if((x + y) % 3 != 0) {
cout << 0;
return;
}
int t = (x + y) / 3;
if(y - t >= 0 && y - t <= t) {
cout << C(t, y - t);
} else cout << 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
E - All-you-can-eat
题意:
现在有\(n\)盘菜,每盘菜需要\(a_i\)的时间去吃,有\(b_i\)的美味度。
现在有如下规则:
- 点了一盘菜之后,只能将其吃完后才能点下一盘菜。
- \(T\)时刻过后就不能再点菜了,但是依旧可以吃菜。
- 每种菜只能点一次。
最后问最后能够得到的最大美味度是多少。
思路:
- 注意比较重要的一点,无论最终点菜顺序是什么,我们都可以将某一份菜安排在\(T\)时刻来点。
- 那么之后问题就变得很简单了:总共有\(T-1\)时刻,然后相当于一个背包问题,直接\(dp\)需要\(O(n^2)\)。
- 但是我们需要枚举哪个菜在最后点,所以总复杂度为\(O(n^3)\)的,显然时间复杂度不能承受。
- 由于我们只关注的是某一个菜不选,那么我们可以预处理一个前后缀的\(dp\),\(dp1[i][j]\)表示\(1\)~\(i\)的物品中,总时间不超过\(j\)的最大美味度,\(dp2[i][j]\)同理。
- 最终时间复杂度为\(O(n^2)\)。
直接来做这个题因为可以超出时间限制,所以不好定义状态,但是钦定最后一个位置后问题就得到转换。
前后缀\(dp\)预处理还是比较巧妙QAQ。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/18 15:30:25
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 3005;
int n, t;
int a[N], b[N];
int dp1[N][N], dp2[N][N];
void run(){
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= t - 1; j++) {
dp1[i][j] = dp1[i - 1][j];
if(j >= a[i])
dp1[i][j] = max(dp1[i][j], dp1[i - 1][j - a[i]] + b[i]);
}
}
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= t - 1; j++) {
dp2[i][j] = dp2[i + 1][j];
if(j >= a[i])
dp2[i][j] = max(dp2[i][j], dp2[i + 1][j - a[i]] + b[i]);
}
}
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= t - 1; j++) {
ans = max(ans, dp1[i - 1][j] + dp2[i + 1][t - 1 - j] + b[i]);
}
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> t) run();
return 0;
}
这个题还有更巧妙的解法:假设我们知道了最后点的哪些菜,那么显然我们总能把耗时最多的安排在\(T\)时刻来点。
这时问题转换地更进一步:假设钦定了最后一位,那么前面只能选择耗时不超过它的。
所以直接处理出上面的\(dp1[i][j]\),同时维护一个后缀最大值即可。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/18 15:30:25
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 3005;
int n, t;
struct node {
int a, b;
bool operator < (const node &A) const {
return a < A.a;
}
}p[N];
int maxv[N];
int dp[N][N];
void run(){
for(int i = 1; i <= n; i++) cin >> p[i].a >> p[i].b;
sort(p + 1, p + n + 1);
for(int i = n; i >= 1; i--) maxv[i] = max(maxv[i + 1], p[i].b);
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= t - 1; j++) {
dp[i][j] = dp[i - 1][j];
if(j >= p[i].a) dp[i][j] = max(dp[i][j], dp[i - 1][j - p[i].a] + p[i].b);
}
ans = max(ans, dp[i][t - 1] + maxv[i + 1]);
}
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> t) run();
return 0;
}
F - Laminate
题意:
现在有\(n\)个柱子排在一起,每个柱子有个高度\(h_i,0\leq h_i\leq 10^9\)。
现在有至多\(k\)次机会任意修改某些柱子的高度。
之后会执行操作:每次可以横向消去一段连续的柱子。问最终最少的操作次数是多少。
思路:
这个题感觉直接做也没什么思路...太菜了555。
就感觉问题很抽象,考虑很多情况,一般这种感觉将问题形象化、具体化是解题的关键,比如这个题,我们考虑\(k=0\)的情况,那么最终的答案为:
\]
然后有个结论:如果修改一根柱子,假设其位置为\(i\),那么最终其高度在\([h_{i-1},h_{i+1}]\)之间是最优的。
这个结论较为显然,那么我们可以直接钦定:修改操作即令\(h_i=h_{i-1}\)。
进一步观察可以发现,操作等价于删除一根柱子。
这个时候解法就呼之欲出了,可令\(dp[i][j]\)表示前\(i\)根柱子,保留\(j\)根的最小操作次数。
那么有转移:\(dp[i][j] = min\{dp[k][j-1]+max(0,h_i-h_k),j<i\}\)。
直接\(O(n^3)\)来搞就没了。
P.S:这个可以将后面\(max\)操作打开,然后用树状数组维护两个值来优化,复杂度可以达到\(O(n^2logn)\)
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/18 18:16:10
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 300 + 5;
int n, K;
int h[N];
ll dp[N][N];
void run(){
for(int i = 1; i <= n; i++) cin >> h[i];
memset(dp, INF, sizeof(dp));
dp[0][0] = 0;
for(int i = 1; i <= n - K; i++) {
for(int j = i; j <= n; j++) {
for(int k = 0; k < j; k++) {
dp[j][i] = min(dp[j][i], dp[k][i - 1] + max(0, h[j] - h[k]));
}
}
}
ll ans = INF;
for(int i = 1; i <= n; i++) ans = min(ans, dp[i][n - K]);
if(ans == INF) ans = 0;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> K) run();
return 0;
}
AtCoder Beginner Contest 145的更多相关文章
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- AtCoder Beginner Contest 064 D - Insertion
AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...
- AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】
AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...
随机推荐
- pymysql连接
import pymysql conn=pymysql.connect(host='localhost',user='root',password='123',db='sg',charset='utf ...
- Re-stheasy
https://dn.jarvisoj.com/challengefiles/ctf2.b93676be23733b2fcda3988c1133c1c1 用IDA-32bit 打开,找到main函数 ...
- Java流程控制之循环语句
循环概述 循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要在合适的时候把循环判断条件修改为false,从而结束循环,否则循环将 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心
D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...
- HTML连载47-设计思想、浮动元素高度问题
一.设计网页的思想 拿到需求之后我们先对各个模块(盒子)进行划分,然后从外到内进行设计(1)设计一个盒子最基本的设计大致包括背景颜色(其实用于识别),宽,高,边界浮动流还是标准流. (2)然后盒子和盒 ...
- 使用OC实现单链表:创建、删除、插入、查询、遍历、反转、合并、判断相交、求成环入口
一.概念 链表和数组都是一种线性结构,数组有序存储的,链表是无序存储的. 数组中的每一个元素地址是递增或者递减的关系,链表的每一个节点的地址没有此规律,它们是通过指针的指向连接起来. 链表种类:单链表 ...
- 【C#】C#中的HtmlEncode与HtmlDecode:HttpUtility.HtmlEncode,HttpUtility.HtmlDecode,Server.HtmlEncode,Server.HtmlDecode,WebUtility.HtmlEncode,WebUtility.HtmlDecode
HtmlEncode(String) 将字符串转换为 HTML 编码字符串. HtmlDecode(String) 将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串. 在we ...
- C语言程序设计100例之(1):鸡兔同笼
例1 鸡兔同笼 [问题描述] 一个笼子里面关了鸡和兔子(鸡有2 只脚,兔子有4 只脚,没有例外).已知笼子里面脚的总数a,问笼子里面至少有多少只动物,至多有多少只动物? [输入数据] 第1 行是测 ...
- PHP JWT token实现
原文链接:https://www.jb51.net/article/146790.htm 机制: 代码如下: <?php /** * PHP实现jwt */ class Jw ...
- js获取计算机操作系统版本
如题,想要获取当先计算机的操作系统和版本号的话,可以用如下方法. 首先,创建osversion.js文件,文件里面的代码如下 var osData = [ { name: 'Windows 2000' ...