A - Rewards

水题,把a累加,然后向上取整(double)a/5,把b累加,然后向上取整(double)b/10,然后判断a+b是不是大于n即可

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std; int main(){
double a1,a2,a3;
double b1,b2,b3;
int n;
cin >>a1 >> a2 >> a3 >>b1 >> b2 >> b3>> n;
int cnt = ceil((a1+a2+a3)/)+ceil((b1+b2+b3)/);
if(cnt > n) cout <<"NO"<<endl;
else cout<<"YES"<<endl;
}

B - Suffix Structures

题目的意思是:

  给两个字符串s和t,对字符串s可以进行下面操作(注意每种操作可以进行无限次)

  • 删除s的一个字符,如果只做这种操作,输出automation
  • 交换s的任意两个字符,如果只做这种操作,输出array
  • 如果既要删除又要交换,则输出both
  • 如果上面操作无法完成,则输出need tree

解题思路:

  (1)首先判断s和t的长度,如果s的长度比t的长度小,则上面的操作都无法完成如果s的长度大于t的长度,则需要删除字符

  (2)将s中不属于t内的字符删除,这剩下的字符为str

  (3)统计str和t中各自字符串出现的次数,

      如果中的字符不在str中,即strCnt.size() < tCnt.size(),则上面操作无法完成。

      如果tCnt中某字符的个数大于strCnt的字符的个数,则上面的操作无法完成。

      如果strCnt中存在字符的个数大于tcnt的相应字符的个数,则必须要删除s的字符。

      如果s的所有字符的个数和t的所有字符的个数相等,则只需要交换即可

  (4)最后通过在str中使用两个指针i,index,i指向str,index指向t,遍历str,如果t[index]==str[i],则index++; 最后判断index是否指向t的末尾,如果是,则只需要删除字符即可,否则既要删除又要交换字符。

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std; int main(){
string s,t;
cin >>s >>t;
bool needTree = false,automatonFlag = false;
if(s.length() < t.length()) needTree = true;
else if(s.length() >t.length()) automatonFlag = true;
string str="";
for(int i = ; i < s.length(); ++ i ){
if(t.find(s[i])!=string::npos) str+=s[i];
}
map<char,int> strCnt,tCnt;
for(int i = ; i < str.length(); ++ i) strCnt[str[i]]++;
for(int i = ; i < t.length(); ++ i) tCnt[t[i]]++;
if(strCnt.size()!=tCnt.size()) needTree = true;
else{
for(map<char,int>::iterator iter = tCnt.begin(); iter!=tCnt.end(); iter++){
if(iter->second > strCnt[iter->first]) {needTree = true;break;}
if(iter->second < strCnt[iter->first]) automatonFlag = true;
}
}
if(needTree) cout<<"need tree"<<endl;
else{
if(!automatonFlag) cout<<"array"<<endl;
else{
int i = ,index = ;
for(int i = ; i < str.length() && index < t.length(); ++i){
if(t[index] == str[i]) index++;
}
if(index < t.length()) cout<<"both"<<endl;
else cout<<"automaton"<<endl;
}
}
}

C - Painting Fence

题目的简化意思是:

  给你一个直方图,每次只能用画笔刷一行(注意是连在一起的)或者1竖,求至少要刷多少次能把这个直方图刷完

解题思路是

  每次取min(a1,a2,...an),然后水平刷min(a1,a2,...an)次,刷完后,相当于每个直方条少了ai-min(a1,a2....an)(i=0..n-1),这时候这些直方条就会被高度为0的直方条隔开,然后对每个部分,进行递归调用,算出次数,注意每次还要和right-left+1(相当于每直方条都刷一次)比较,然后取小值。

#include <iostream>
#include <vector>
#include <algorithm> using namespace std;
int solve(vector<int>& a,int left, int right){
if(right< left) return ;
int minv = *min_element(a.begin()+left,a.begin()+right+);
int res = minv, index = left;
for(int i = left; i<=right; ++i) a[i]-=minv;
for(int i = left; i<= right; ++ i){
if(a[i] == ){
if(index < i) {
res+=solve(a,index,i-);
}
index = i+;
}
}
res+=solve(a,index,right);
return min(res,right-left+);
} int main(){
int n;
cin >> n ;
vector<int> a(n,);
for(int i = ; i < n; ++ i) cin >> a[i];
cout<<solve(a,,n-)<<endl;
}

递归实现

D - Multiplication Table

求二维数组第k小值,用优先权队列取每个元素的下面和右边的元素插入队列,结果超时。由于用优先权队列其时间复杂度为klognm,而k<=nm则最坏时间复杂度是nmlog(nm),肯定超时

#include <iostream>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>
#define LL long long using namespace std; struct Point{
int row;
int col;
Point(int x=, int y = ):row(x), col(y){} bool operator<(const Point& a) const{
return (LL)row*(LL)col > (LL)a.row*(LL)a.col;
}
}; int main(){
int n, m;
LL k;
cin >> n >> m >>k;
vector<vector<bool> > visit(n+,vector<bool>(m+,false));
priority_queue<Point> que;
que.push(Point(,));
visit[][] = true;
LL res = ;
for(int i = ; i <k; ++i){
Point tmp = que.top();que.pop();
res = (LL)tmp.row*(LL)tmp.col;
Point a(tmp.row+,tmp.col),b(tmp.row,tmp.col+);
if(a.row <= n && a.col<=m && !visit[a.row][a.col]){que.push(a);visit[a.row][a.col] = true;}
if(b.row<=n && b.col<=m && !visit[b.row][b.col]) {que.push(b);visit[b.row][b.col] = true;}
}
cout<<res<<endl; }

优先权队列!!超时

现在利用二分搜索。

假设对任意给一个数x,如何判断nxm二维数组中小于x的个数?

注意由于对第i行第j列的数是i*j,则j=x/i表示x在第i行的位置,也就是在该行第j列之前的位置都是小于x的

所以遍历每行,然后累加min(m,x/i)即可,则即是小于x的个数。

#include <iostream>
#include <vector>
#include <algorithm>
#define LL long long using namespace std; LL n,m,k; LL calc(LL x){
LL cnt = ;
for(LL i = ; i <= n; ++ i ) cnt += min(m,x/i);
return cnt;
} int main(){
cin >> n >>m >>k;
LL left = , right = n*m;
while(left<=right){
LL mid = (left+right)/;
if(calc(mid) >=k) right = mid-;
else left = mid+;
}
cout<<right+<<endl;
}

二分搜索,时间复杂度O(nlog(nm))

E - Divisors

题目的意思:

  给定一个整数X,X0=[X], Xi = [...]由Xi-1各个数的因子组成,求Xk,注意Xk中元素的个数最多为100000

解题思路:

  本题先求出X1,即先求出X1的因子,然后对X每个因子进行深搜,直到k=0或者x=1,这样求出每个因子的k,但深搜要注意剪枝,题目要求的Xk的因子数不多余100000,故当搜索的因子数到达100000,就把后面的因子剪掉。这样可以避免超时

#include <iostream>
#include <vector>
#include <algorithm>
#include <vector>
#define LL long long
using namespace std; LL limit = 1e5;
vector<LL> divs,res; bool solve(LL x, LL k){
if(k == || x == ){
res.push_back(x);
return res.size() >= limit;
}
for(int i = ;i < divs.size(); ++ i){
if(divs[i] > x) break;
if(x%divs[i] == ){
if(solve(divs[i],k-)) return true;
}
}
return false;
} int main(){
LL x,k;
cin >> x >> k;
for(LL i = ; i*i <= x; ++ i){
if(x%i == ){
divs.push_back(i);
if(i*i != x) divs.push_back(x/i);
}
}
sort(divs.begin(),divs.end());
solve(x,k);
for(int i = ; i < res.size(); ++ i){
if(i) cout<<" ";
cout<<res[i];
}
cout<<endl;
return ;
}

深度搜索+剪枝

Codeforces Round #256 (Div. 2)的更多相关文章

  1. Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)

    转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...

  2. Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)

    题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...

  3. Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)

    解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 ...

  4. Codeforces Round #256 (Div. 2) 题解

    Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. Codeforces Round #256 (Div. 2) A. Rewards

    A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. Codeforces Round #256 (Div. 2) D. Multiplication Table 二分法

     D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input st ...

  7. Codeforces Round #256 (Div. 2) D. Multiplication Table

    主题链接:http://codeforces.com/contest/448/problem/D 思路:用二分法 code: #include<cstdio> #include<cm ...

  8. Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)

    解题报告 意思就是说有n行柜子,放奖杯和奖牌.要求每行柜子要么全是奖杯要么全是奖牌,并且奖杯每行最多5个,奖牌最多10个. 直接把奖杯奖牌各自累加,分别出5和10,向上取整和N比較 #include ...

  9. Codeforces Round #256 (Div. 2) D. Multiplication Table 很有想法的一个二分

    D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes input stand ...

  10. Codeforces Round #256 (Div. 2)A-D

    题目连接:http://codeforces.com/contest/448 A:给你一些奖杯与奖牌让你推断能不能合法的放在给定的架子上.假设能够就是YES否则就是NO. <span style ...

随机推荐

  1. SourceTree推送时,增加额外的远程仓库,不用每次都自定义粘贴复制网络

    一.命令行添加 二.软件界面可以查看到结果 更新,以上是代码添加了远程仓库,最近,找到了不用代码,直接在文件夹里写地址来添加的方式.直接上图了.

  2. 22章、Java集合框架习题

    1.描述Java集合框架.列出接口.便利抽象类和具体类. Java集合框架支持2种容器:(1) 集合(Collection),存储元素集合 (2)图(Map),存储键值对.

  3. A:手把手教Wordpress仿站(基础)

    安装源码 需要服务器有php环境(PHP,Mysql,Apeach/Ngnax) 我用的主机宝(环境一键安装工具)   打开后台突然出现这种情况 Briefly unavailable for sch ...

  4. QQ空间HD(6)-实现自定义的选项卡切换效果

    DJTabbarButton.m #import "DJTabbarButton.h" @implementation DJTabbarButton - (instancetype ...

  5. 今天又学了一招,牛逼!!!解决"-bash:No such file or directory"问题

    今天在64服务器上:发现好像bash 坏了,用户名前边   用-bash 显示,,前几天就发现这个问题,,,但是当时忙没有解决,,,,,今天来看看到底是怎么回事! File Exists but... ...

  6. glusterFS系统中文管理手册(转载)

    GlusterFS系统中文管理手册       1文档说明 该文档主要内容出自 www.gluster.org 官方提供的英文系统管理手册<Gluster File System 3.3.0 A ...

  7. saltstack(master迁移)

    环境:http://www.cnblogs.com/zzzhfo/p/6126223.html 在添加一台master 把master的/etc/salt目录下的pki打包 上传至第二台master的 ...

  8. 引用项目外dll时不显示注释的解决方案

    在引用项目外的dll时,显示类库中的注释可按以下步骤: 方法或变量用summary添加注释,如:         /// <summary>发送post请求         /// < ...

  9. 各大搜索引擎智能提示API(JSONP跨域实现自动补全搜索建议)

    ---------------------------------------搜索引擎JSONP接口--------------------------------------------- 提示:U ...

  10. python之路三

    集合 set拥有类似dict的特点:可以用{}花括号来定义:其中的元素没有序列,也就是是非序列类型的数据;而且,set中的元素不可重复,这就类似dict的键. set也有继承了一点list的特点:如可 ...