当前位置:网站首页>2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)
2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)
2022-07-27 08:43:00 【Madness makes freedom】
2040: [ Blue Bridge Cup 2022 Preliminaries ] Cut bamboo
Memory limit :256 MB The time limit :1 S Standard input and output
Topic type : Tradition Evaluation method : Text comparison Uploaded by : External import
Submit :137 adopt :60
Title Description
On the day , Xiao Ming is cutting bamboo , In front of him is n Bamboo trees stand in a row , At first i The height of each bamboo is hi.
He thought it was too slow to cut one by one , Decided to use magic to cut bamboo .
Magic can be used on a continuous section of bamboo of the same height , Suppose the height of this section of bamboo is H,
Then using magic once can change the height of this section of bamboo into :

among ⌊x⌋ Said to x Rounding down .
Xiao Ming wants to know how many times he can use magic at least to make the height of all bamboos become 1.
Input format
The first line is a positive integer n, Indicates the number of bamboos .
The second line is n A positive integer separated by spaces hi, Indicates the height of each bamboo tree .
20% Test data for :n ≤ 1000; hi ≤ 10^6.
100% Test data for :n ≤ 2 × 10^5; hi ≤ 10^18.
Output format
An integer represents the answer .
sample input Copy
6
2 1 4 2 6 7sample output Copy
5Data range and tips
One of them is :
2 1 4 2 6 7
→ 2 1 4 2 6 2
→ 2 1 4 2 2 2
→ 2 1 1 2 2 2
→ 1 1 1 2 2 2
→ 1 1 1 1 1 1
A common need 5 Step through .
1) The priority queue implemented by the structure
/**
1) The priority queue implemented by the structure
*/
/**
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <functional>
using namespace std;
typedef long long LL;
// You can define the structure ( class ) The operator is overloaded when , Of course, when the overloaded operator is a member function , Member functions
// Of ( Show ) The number of parameters is one less than the number of operands ,this Bind to left operand ;
// When it is not a member function , To define as a friend function , That is, add the keyword before the function friend , At this point, the parameters of the friend function
// And the number of general functions is the same
struct infor
{
LL val;
int pos;
// friend bool operator < (const infor &a,const infor &b) // Nonmember functions need to be defined as friend functions
// {
// if(a.val!=b.val)
// return a.val<b.val;
// else
// return a.pos<b.pos;
// }
};
struct cmp
{
bool operator () (const infor &a,const infor &b)
{
if(a.val!=b.val)
return a.val<b.val;
else
return a.pos<b.pos;
}
};
int main()
{
int n;
infor val;
// priority_queue<infor> vec;
priority_queue<infor,vector<infor> ,cmp> vec;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%lld",&val.val);
val.pos=i;
vec.push(val);
}
int sum=0;
while(vec.top().val!=1)
{
infor top=vec.top();
vec.pop();
LL ans=floor(sqrt((top.val/2+1)*1.0));
vec.push({ans,top.pos});
int coun=1;
while(vec.top().val==top.val&&vec.top().pos==top.pos-coun++)
{
infor temp=vec.top();
vec.pop();
vec.push({ans,temp.pos});
}
++sum;
}
cout << sum << endl;
return 0;
}
*/
2) pair Priority queue implemented by container
By default, the priority of priority queue is set to high priority if the number is large
/**
2) pair Priority queue implemented by container
By default, the priority of priority queue is set to high priority if the number is large
*/
/**
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef pair<long long ,int> pll;
int main()
{
int n;
pll val;
priority_queue<pll> vec;
scanf("%d",&n);
for(int i=0;i<n;++i)
{
scanf("%lld",&val.first);
val.second=i;
if(val.first!=1)
vec.push(val);
}
int sum=0;
while(!vec.empty()&&vec.top().first!=1)
{
pll top=vec.top();
vec.pop();
long long ans=floor(sqrt((top.first/2+1)*1.0));
if(ans!=1)
vec.push({ans,top.second});
int coun=1;
while(!vec.empty()&&vec.top().first==top.first&&vec.top().second==top.second-coun++)
{
pll temp=vec.top();
vec.pop();
if(ans!=1)
vec.push({ans,temp.second});
}
++sum;
}
cout << sum << endl;
return 0;
}*/3) First add up the number of times it takes to cut down a tree , Then subtract the adjacent equal trees ;
Record the height of each tree before each cut , Because it is 200000 tree ,1e18 High tree
Cut to 1 Times of 6, So you can open a dp[500010][6] To store data .
/**
3) First add up the number of times it takes to cut down a tree , Then subtract the adjacent equal trees ;
Record the height of each tree before each cut , Because it is 200000 tree ,1e18 High tree
Cut to 1 Times of 6, So you can open a dp[500010][6] To store data .
*/
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn=200010,m=10;
LL dp[maxn][m];
int main()
{
int n;
scanf("%d",&n);
LL sta[m],val,res=0;
int ans=0;
for(int i=0;i<n;++i)
{
int top=0;
scanf("%lld",&val);
while(val>1) sta[++top]=val,val=sqrt(val/2+1);
ans=max(ans,top);
res+=top;
for(int j=0;top>0;++j,--top)
dp[i][j]=sta[top];
}
for(int i=0;i<ans;++i)
for(int j=0;j<n-1;++j)
{
if(dp[j][i]&&dp[j][i]==dp[j+1][i]) // Why only need to judge here dp[j][i]>=1 Can then , because dp[j][[i]
res--; // The minimum values are 2
}
cout << res << endl;
return 0;
}边栏推荐
- Digital intelligence innovation
- JWT authentication and login function implementation, exit login
- 海关总署:这类产品暂停进口
- MCDF顶层验证方案
- UVM Introduction Experiment 1
- Supervisor 安装与使用
- [nonebot2] several simple robot modules (Yiyan + rainbow fart + 60s per day)
- How to uninstall -- Qianxin secure terminal management system
- JS basic exercises
- Unity3D 2021软件安装包下载及安装教程
猜你喜欢

Day5 - Flame restful request response and Sqlalchemy Foundation

海关总署:这类产品暂停进口

网络IO总结文

Zhongang Mining: the new energy industry is developing rapidly, and fluorine chemical products have a strong momentum

How to merge multiple columns in an excel table into one column

MCDF top level verification scheme

说透缓存一致性与内存屏障

Digital intelligence innovation

“寻源到结算“与“采购到付款“两者有什么不同或相似之处?

UVM入门实验1
随机推荐
VS Code中#include报错(新建的头文件)
Sliding conflict of view
693. 行程排序
Matlab求解微分代数方程 (DAE)
Set set
Matlab数据导入--importdata和load函数
Implementation of registration function
Network IO summary
4279. 笛卡尔树
Horse walking oblique sun (backtracking method)
NIO总结文——一篇读懂NIO整个流程
Help send some recruitment. If you are interested, you can have a look
2034: [Blue Bridge Cup 2022 preliminary] pruning shrubs
3428. Put apples
General view, DRF view review
NIO示例
低成本、低门槛、易部署,4800+万户中小企业数字化转型新选择
Aruba学习笔记10-安全认证-Portal认证(web页面配置)
Forced login, seven cattle cloud upload pictures
Login to homepage function implementation