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. dos 固定ip命令

    dos 固定ip命令 ***************************************************************************************** ...

  2. Java排序算法——拓扑排序

    package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...

  3. MySQL 5.6 新参数对binlog日志量的优化

    数据库版本:5.6.* 1.row日志image类型 参数binlog_row_image 控制着这种image类型,默认为FULL(log all columns),即记录before&af ...

  4. 04OC之分类Category,协议Protocol,Copy,代码块block

    一.Protocol协议 我们都知道,在C#有个规范称之为接口,就是规范一系列的行为,事物.在C#中是使用Interface关键字来声明一个接口的,但是在OC中interface是用来声明类,所以用了 ...

  5. command shell 的知识整理

    cmd 也是shell  windowns的外壳一种. 查看文件和文件夹 dir mkdir *** 创建文件夹 rd *** 删除文件夹(应该有参数的,递归之类的)CTR+C 终止命令 cd> ...

  6. BZOJ 1770: [Usaco2009 Nov]lights 燈

    Description 一个图,对一个点进行操作会改变这个点及其相邻的点的状态,问全部变成黑色至少需要几次.数据保证有解. Sol Meet in middle. 我一开始写个高斯消元,发现有两个点过 ...

  7. css pre标签

    浏览器:firfox49.0.2 在使用<pre>标签输出格式化文本的时候,遇到了一个小问题. 要在页面的底部输出两行文本,但是最后一行的文字总是距离屏幕的底部太大.下面图中的样子: 相关 ...

  8. 关于用bootstrap显示查询的后台数据

    PrintWriter pw = response.getWriter(); pw.println(sb); pw.flush(); 由于用bootstrap查询数据,页面需要自身返回bootstra ...

  9. PowerDesigner使用

    首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示. 看看怎样用PowerDesigner快速的创建出这个数据库吧. 1. ...

  10. 两个已排序数组进行合并后的第K大的值--进军硅谷

    我看到此题时,首先想到一个一个比较遍历过去,这是最暴力的方法,后面我想到了已经排序,那么对每个数组进行二分,然后比较这两个值.此书第三种解法,挺不错,只对那个长度较小的数组进行二分查找,保证i+j-1 ...