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的更多相关文章

  1. Codeforces Beta Round #14 (Div. 2)

    Codeforces Beta Round #14 (Div. 2) http://codeforces.com/contest/14 A 找最大最小的行列值即可 #include<bits/s ...

  2. 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 ...

  3. Codeforces Beta Round #14 (Div. 2) C. Four Segments 水题

    C. Four Segments 题目连接: http://codeforces.com/contest/14/problem/C Description Several months later A ...

  4. Codeforces Beta Round #14 (Div. 2) B. Young Photographer 水题

    B. Young Photographer 题目连接: http://codeforces.com/contest/14/problem/B Description Among other thing ...

  5. 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. ...

  6. 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 ...

  7. Codeforces Testing Round #8 B. Sheldon and Ice Pieces 水题

    题目链接:http://codeforces.com/problemset/problem/328/B 水题~ #include <cstdio> #include <cstdlib ...

  8. Codeforces Testing Round #12 C. Subsequences 树状数组维护DP

    C. Subsequences Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...

  9. Codeforces Testing Round #12 B. Restaurant 贪心

    B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...

随机推荐

  1. 用例图(Use Case Diagram)

    用例图(Use Case Diagram) 执行者/参与者(Actor): 表示与您的应用程序或系统进行交互的用户.组织或外部系统.用一个小人表示. 用例(Use Case): 即系统具有的功能,在用 ...

  2. Arduino 舵机sg90电位器实现转动方向控制

    /* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...

  3. c语言中的输入

    先打个白条有时间在写 c语言中输入一行回车之后,以空格为单位进行的分割

  4. Win7(64bit)搭建SVN

    开始: 第一步:下载SVN客户端程序TortoiseSVN并安装(不习惯英文操作界面的顺便在最底下下载一个语言包),下载地址tortoiseSVN下载(由于系统是64位的,我下载的是TortoiseS ...

  5. linux 设备树【转】

    转自:http://blog.csdn.net/chenqianleo/article/details/77779439 [-] linux 设备树 为什么要使用设备树Device Tree 设备树的 ...

  6. centos_7.1.1503_src_7

    http://vault.centos.org/7.1.1503/os/Source/SPackages/ tex-fonts-hebrew-0.1-21.el7.src.rpm 05-Jul-201 ...

  7. 很重要的处理项目url[www]

    http://www.xdowns.com/soft/10/57/2013/Soft_113319.html https://github.com/TricksterGuy/Morphan http: ...

  8. python之operator操作符函数

    operator函数主要分为以下几类:对象比较.逻辑比较.算术运算和序列操作. 举例: #python 3.4 >>> operator.eq(1,2)False >>& ...

  9. Python抓取花瓣网高清美图

    一:前言 嘀嘀嘀,上车请刷卡.昨天看到了不错的图片分享网——花瓣,里面的图片质量还不错,所以利用selenium+xpath我把它的妹子的栏目下爬取了下来,以图片栏目名称给文件夹命名分类保存到电脑中. ...

  10. git学习笔记三

    1.每个分支的历史版本维护信息位置是.git/logs/refs/heads/master,这个位置的信息是文本文件,不是引用. harvey@harvey-Virtual-Machine:~/dem ...