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 ...
随机推荐
- 基于canvas的陈列订货的分析
订货会软件中又新增了进行陈列订货,即一杆衣服订的显示出来,没订的不显示出来 主要遇到的问题是如何呈现,原先老是想着定位,left,top但是花出来的图容易出现原先的数据填写错误导致后期的图片的呈现出现 ...
- CSS line-height与vertical-align:baseline
一.当line-height与vertical-align相遇,会发生很多匪夷所思的现象 首先,请看如下代码: <!DOCTYPE html> <html> <head& ...
- 快排 快速排序 qsort quicksort C语言
现在网上搜到的快排和我以前打的不太一样,感觉有点复杂,我用的快排是FreePascal里/demo/text/qsort.pp的风格,感觉特别简洁. #include<stdio.h> # ...
- 大熊君学习html5系列之------History API(SPA单页应用的必备)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- 今天在学习NTP时发现了2个网站
NTP 调整系统时间 一个网站是:http://chrony.tuxfamily.org/doc/1.31/manual.html 这个是专门介绍chrony的,做的很详细. 另外一个是:http: ...
- JavaScript学习总结(二)数组和对象部分
pt学习总结(二)数组和对象部分 2016-09-16 分类:WEB开发.编程开发.首页精华暂无人评论 来源:trigkit4 分享到:更多1 对象部分 Object类型 Object ...
- springmvc 表单提交
Spring MVC自带的表单标签比较简单,很多时候需要借助EL和JSTL来完成. 下面是一个比较简单的表单提交页面功能: 1.User model package com.my.controller ...
- 我对Jenkins的认识
参考: http://www.cnblogs.com/sunzhenchao/archive/2013/01/30/2883289.html
- Qt界面中嵌入其他exe程序的界面,使用Qt5
下面用一个小例子来演示如何在Qt的界面中嵌入其他exe程序的界面,最终效果如下图所示.本文参考了 http://blog.csdn.net/jiaoyaziyang/article/details/4 ...
- tornado django flask 跨域解决办法(cors)
XMLHttpRequest cannot load http://www.baidu.com. No 'Access-Control-Allow-Origin' header is present ...