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 ...
随机推荐
- bzoj 3524 可持久化线段树
我们可以先离散化,然后建立权值的可持久化线段树,记录每个数出现的次数,对于区间询问直接判断左右儿子的cnt是不是大于(r-k+1)/2,然后递归到最后一层要是还是大于就有,否则不存在. 反思:挺简单一 ...
- D题 hdu 1412 {A} + {B}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1412 {A} + {B} Time Limit: 10000/5000 MS (Java/Others ...
- CursorFileManager对cursor文件的读写
public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...
- Problems with Ribbon/Feign/Zuul retry
原文 https://github.com/spring-cloud/spring-cloud-netflix/issues/1577 I'm using Spring Cloud Camden SR ...
- cart回归树算法过程
回归树:使用平方误差最小准则 训练集为:D={(x1,y1), (x2,y2), …, (xn,yn)}. 输出Y为连续变量,将输入划分为M个区域,分别为R1,R2,…,RM,每个区域的输出值分别为: ...
- 74cms 注入exp
遇到就瞎写了一个: #!/usr/bin/env python #encoding:utf-8 #by i3ekr import requests,optparse,re parse = optpar ...
- python自动开发之第二十三天(Django)
一.一大波model操作 1. 创建数据库表 # 单表 # app01_user ==> tb1 # users class User(models.Model): name = models. ...
- 【swupdate文档 二】许可证
许可证 SWUpdate是免费软件.它的版权属于Stefano Babic和其他许多贡献代码的人(详情请参阅实际源代码和git提交信息). 您可以根据自由软件基金会发布的GNU通用公共许可证第2版的条 ...
- 一个文档让vim飞起来
原文地址:http://www.cnblogs.com/songfy/p/5635757.html 引言 今天我们特地来讲讲这个vim的配置. vim这东西, 很多人装逼的时候经常会提到, 不过大部分 ...
- 阿里云ECS安装Docker
阿里云ESC系统信息,官方说2.6内核运行docker服务可能会不稳定: $ uname -a Linux iZ259dixwg8Z -.el6.x86_64 # SMP Thu Jul :: UTC ...