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】1914: [Usaco2010 OPen]Triangle Counting 数三角形
[题意]给定坐标系上n个点,求能构成的包含原点的三角形个数,n<=10^5. [算法]极角排序 [题解]补集思想,三角形个数为C(n,3)-不含原点三角形. 将所有点极角排序. 对于一个点和原点 ...
- 【NOIP】提高组2015 运输计划
[题意]n个点的树,m条链,求将一条边的权值置为0使得最大链长最小. [算法]二分+树上差分 [题解] 最大值最小化问题,先考虑二分最大链长. 对所有链长>mid的链整体+1(树上差分). 然后 ...
- Android应用程序App应用上线流程
对于很多初级开发者,可能对app应用上线不太了解,本文跟大家介绍一下怎么上线app应用.上线App并不是一件很困难的事情,App的应用功能也不需要很强大,甚至不用联网,只有简单的一两个页面的App应用 ...
- 【Matlab】使用Matlab运行Windows命令
可以使用Matlab的一些命令来帮助程序运行.比如说 ! calc % 打开计算器 ! mspaint % 打开画图 dos calc % 打开计算器 比如一个程序要运行很长时间,而我们又不能一直守在 ...
- C#中执行批处理文件(.bat),执行数据库相关操作
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- FineReport——发送邮件
在FR中,个用户之间可以通过邮件的形式进行通信,但是存在一个问题就是FineReport平台只能设置一个发件人账户,收件人账户可以有多个. 所以在实际的系统开发中可能需要自己开发信息交互的模块. 在系 ...
- IntelliJ IDEA centos安装
安装的时候注意: 不要装一个插件否则无法启动!!!
- LightOJ - 1234
Harmonic Number Time Limit: 3000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Descri ...
- Balanced Binary Tree——经典题
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- linux下输入密码不回显
这几天在做一个登陆的程序,需要将输入的密码屏蔽掉,自己百度,找到了两种方法,先贴下第一种方法, #include<stdio.h> #include<unistd.h> int ...