Codeforces Round #256 (Div. 2)
水题,把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;
}
题目的意思是:
给两个字符串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;
}
}
}
题目的简化意思是:
给你一个直方图,每次只能用画笔刷一行(注意是连在一起的)或者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;
}
递归实现
求二维数组第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))
题目的意思:
给定一个整数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)的更多相关文章
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...
- Codeforces Round #256 (Div. 2/B)/Codeforces448B_Suffix Structures(字符串处理)
解题报告 四种情况相应以下四组数据. 给两字符串,推断第一个字符串是怎么变到第二个字符串. automaton 去掉随意字符后成功转换 array 改变随意两字符后成功转换 再者是两个都有和两个都没有 ...
- Codeforces Round #256 (Div. 2) 题解
Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 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 ...
- 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 ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table
主题链接:http://codeforces.com/contest/448/problem/D 思路:用二分法 code: #include<cstdio> #include<cm ...
- Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)
解题报告 意思就是说有n行柜子,放奖杯和奖牌.要求每行柜子要么全是奖杯要么全是奖牌,并且奖杯每行最多5个,奖牌最多10个. 直接把奖杯奖牌各自累加,分别出5和10,向上取整和N比較 #include ...
- 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 ...
- Codeforces Round #256 (Div. 2)A-D
题目连接:http://codeforces.com/contest/448 A:给你一些奖杯与奖牌让你推断能不能合法的放在给定的架子上.假设能够就是YES否则就是NO. <span style ...
随机推荐
- bootstrap做了一个表格
花了一下午做了一个表格: 大致是这样: 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf ...
- 关于学习JavaScript 的 高三编程 一些心得(二)
今天在看高三的过程中,解决了之前我在 面试过程中遇到的一个问题. 就是将一段英文 颠倒过来. 下面就是我的代码: var zhang = "my name is zhangge !" ...
- alpha版、beta版、rc版的意思
很多软件在正式发布前都会发布一些预览版或者测试版,一般都叫“beta版”或者 “rc版”,特别是开源软件,甚至有“alpha版”,下面来解释一下各个版本的意思. alpha版:内部测试版.α是希腊字母 ...
- Ubuntu 12/14 个性化配置
计算机名:jianbao-pc 用户名:jianbao 修改 /opt目录的 用户名 及 用户组 : sudo chown -R jianbao:jianbao /opt Ubuntu 如何开启 ...
- Maven 入门 (1)—— 安装
Maven 入门 (1)—— 安装 http://blog.csdn.net/kakashi8841/article/details/17371837 1.下载maven安装包 http://mave ...
- Java 中的内存泄露
1.当你完成对流的读写时,应该通过调同close方法来关闭它,这个调用会释放掉十分有限的系统资源,否则,如果一个应用程序打开了过多的流而没有关闭,那么系统资源将被耗尽.
- 记录一下git 的常用命令
以后如果要写一个东西,最好先搭建一个本地仓库,用版本控制对其进行操作,可能一开始有一些麻烦,但是很有可能会受益无穷. 说到git,必然会和github联系起来. 不管是在ubuntu里面还是在Wind ...
- html新增一些常用标签
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python3简单爬虫
最近在抽空学了一下python,于量就拿爬是练了下手,不得不说python的上手非常简单.在网上找了一下,大都是python2的帖子,于是随手写了个python3的.代码非常简单就不解释了,直接贴代码 ...
- VisualStudio控制台输出窗口一闪而过
我用到开发环境是visual studio 2010,写的简单的控制台程序,结果一按control+F5输出窗口出现后立刻就消失了(平时很少这样的呀),按照以往经验,加上了在程序结尾getchar() ...