Codeforces Testing Round 14
A:The Way to Home
link:http://codeforces.com/contest/910/problem/A
题面:有每次最大跳跃距离d,只有一部分的点可以落脚,求最少几步达到终点D
Solution :预处理+贪心
用f[i]表示离点i最近的可落脚点,贪心即可(dp同理)
#include <bits/stdc++.h> using namespace std;
int n,d,pre[];
string s; int main()
{
cin >> n >> d >> s;
n--; pre[]=;
for(int i=;i<s.size();i++)
if(s[i]=='') pre[i]=i;
else pre[i]=pre[i-]; int cur=,res=;
bool f=true;
while(cur<n)
{
if(pre[min(cur+d,n)]==cur)
{
f=false;
break;
}
cur=pre[min(cur+d,n)];
res++;
} if(!f) cout << -;
else cout << res; return ;
}
Problem A
B. Door Frames
http://codeforces.com/contest/910/problem/B
题面:有4个长度为a和2个长度为b的木板,求最少用多少个长度为n的木板可以裁出所要求的所有木板
Solution A:分析法
这也是我用的方法,因为只有2个长度为b的木板,因此只有两种情况,一个n做2个b,或2个n做2个b,其余空间全部由a填满。
Tip:c++中自带的上下取整函数:floor()和ceil()
#include <bits/stdc++.h> using namespace std;
typedef pair<float,float> P;
int n,a,b; int main()
{
cin >> n >> a >> b; if(n>=(a+a+b)*) cout << ;
else if(n>=(a+a+b)) cout << ;
else
{
P n1,n2,n3;
int res=1e9;
n1.first=floor(n/a);n1.second=;
if(n>=*b)
{
n3.first=floor((n-*b)/a);
n3.second=; res=min(res,+(int)ceil((-n3.first)/n1.first));
}
if(n>=b)
{
n2.first=floor((n-b)/a);
n2.second=; res=min(res,+(int)ceil((-*n2.first)/n1.first));
}
cout << res;
} return ;
}
Solution A
Solution B:
因为真正会影响最终结果的是裁剪的顺序,因此对6个数全排列即可
在考虑最优解问题时,可以从枚举出每种可能的顺序下手
Tip:next_permutation()如一开始不排序,最终枚举的是该序列之后的排列
#include <bits/stdc++.h> using namespace std;
#define pb push_back int main()
{
int n,a,b,res=1e9;
cin >> n >> a >> b;
vector<int> v;
for(int i=;i<;i++) v.pb(a);
v.pb(b);v.pb(b); sort(v.begin(),v.end()); do
{
int l=n,cur=;
for(int i=;i<;i++)
if(l-v[i]>=) l-=v[i];
else cur++,l=n-v[i];
res=min(res,cur);
}
while(next_permutation(v.begin(),v.end()));
cout << res;
return ;
}
Solution B
Solution C:递归
这也是我一开始未想到的,其实本应该是非常直观的思路
用dfs(x,y)返回还剩x个a,y个b最少要用几个n来组成,直接暴力
Tip:当有两层循环且内层循环当i=0时j要从1开始时,可以将起始条件设为j=(i==0)
#include <bits/stdc++.h> using namespace std;
int n,a,b; int dfs(int x,int y)
{
int ret=;
if(!x && !y) return ;
for(int i=;i<=x && i*a<=n;i++)
for(int j=(i==);j<=y && j*b+i*a<=n;j++)
{
ret=min(ret,dfs(x-i,y-j));
}
ret++;
return ret;
} int main()
{
cin >> n >> a >> b; cout << dfs(,);
return ;
}
Solution C
Solution D:状压DP
Zscoder大神的写法,虽然没有必要,但代码技巧是值得学习的
先用only数组储存所有一个n能组成的情况,接下来进行状压DP
Tip: 1、当判断在二进制下i是否包含j时,使用(i&j)==j
2、当表示在二进制下只在i中但不在j中的1时,使用i^j(前提:i包含j,否则表示的是两者不共同所有的1)
#include <bits/stdc++.h> using namespace std;
#define pb push_back
int dp[(<<)];
int n,a,b; int main()
{
cin >> n >> a >> b;
vector<int> v;
for(int i=;i<;i++) v.pb(a);
for(int i=;i<;i++) v.pb(b);
for(int i=;i<(<<)+;i++) dp[i]=1e9; dp[] = ;
vector<int> only;
for(int i=;i<(<<);i++)
{
int sum=;
for(int j=;j<v.size();j++)
if(i&(<<j)) sum+=v[j];
if(sum<=n)
{
dp[i]=;only.pb(i);
}
}
for(int i=;i<(<<);i++)
for(int z=;z<only.size();z++)
{
int j=only[z];
if((i&j)==j)
dp[i]=min(dp[i],dp[j]+dp[(i^j)]);
} cout<<dp[(<<)-];
}
Solution D
C:Minimum Sum
link:http://codeforces.com/contest/910/problem/C
Solution:
先计算出每个字母要计算的总次数,同时记录其是否有可能为首位,接下来贪心
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
typedef pair<ll,bool> P; int n;
P a[]; int main()
{
cin >> n;
for(int i=;i<=n;i++)
{
string s;cin >> s; ll time=;
for(int j=s.size()-;j>=;j--)
{
if(!j) a[s[]-'a'].second=true;
a[s[j]-'a'].first+=time;
time*=;
}
} sort(a,a+,greater<P>()); ll res=,cur=;
bool used=false;
for(int i=;i<=;i++)
{
if(!a[i].second && !used) used=true;
else
{
res+=a[i].first*cur;
cur++;
}
}
cout << res; return ;
}
Problem C
Codeforces Testing Round 14的更多相关文章
- Codeforces Beta Round #14 (Div. 2)
Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
- Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题
C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...
- Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题
B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...
- Codeforces Beta Round #14 (Div. 2) A. Letter 水题
A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树的直径
题目链接: http://codeforces.com/contest/14/problem/D D. Two Paths time limit per test2 secondsmemory lim ...
- Codeforces Testing Round #8 B. Sheldon and Ice Pieces 水题
题目链接:http://codeforces.com/problemset/problem/328/B 水题~ #include <cstdio> #include <cstdlib ...
- Codeforces Testing Round #12 C. Subsequences 树状数组维护DP
C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
随机推荐
- javascript 变量类型判断
一.typeof 操作符 对于Function, String, Number ,Undefined 等几种类型的对象来说,他完全可以胜任,但是为Array时 "); typeof arr ...
- Python3 学习第一天总结
一.python介绍 1.python是一门动态解释性的强类型定义语言: 简单解释一下: 定义变量不需要定义类型的为动态语言:典型的有Python和Ruby,反之定义变量需要定义类型的为静态语言:典型 ...
- Caffe学习笔记2
Caffe学习笔记2-用一个预训练模型提取特征 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hi ...
- Linux的SMP,UMA,NUMA
SMP 是Symmetric Multi-Processing的意思,对称多处理器,一种多核结构,认为这些核是完全同构的,任务可以随便在任一个核上跑. UMA是Uniform Memory Acces ...
- .net页面实时预览图片
<script type="text/javascript"> //获取上传图片的本地路径 function getPath(obj){ if(obj) { if(na ...
- English——Unit 2
radiant radiate radical ideal ideology identical identification identify identity journal jounalist ...
- WdatePicker做出onchange效果
WdatePicker({onpicking: function (dp) {if (dp.cal.getDateStr() != dp.cal.getNewDateStr()) { Func(dp. ...
- windows系统安装mysql压缩zip版
1.下载 打开官网:https://www.mysql.com 进入DOWNLOADS--->Community--->MySQL Community Server,选择系统对应的版本点击 ...
- P2885
2885 code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; b ...
- 深入解析当下大热的前后端分离组件django-rest_framework系列四
查漏补缺系列 解析器 request类 django的request类和rest-framework的request类的源码解析 局部视图 from rest_framework.parsers im ...