当前位置:网站首页>Good Key, Bad Key (思维,临项交换,经典方法)
Good Key, Bad Key (思维,临项交换,经典方法)
2022-08-02 03:08:00 【lovesickman】
Good Key, Bad Key (思维,临项交换)
题目描述
There are $ n $ chests. The $ i $ -th chest contains $ a_i $ coins. You need to open all $ n $ chests in order from chest $ 1 $ to chest $ n $ .
There are two types of keys you can use to open a chest:
- a good key, which costs $ k $ coins to use;
- a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest $ i $ will do $ a_i = \lfloor{\frac{a_i}{2}\rfloor} $ , $ a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, \dots, a_n = \lfloor \frac{a_n}{2}\rfloor $ ;
- any key (both good and bad) breaks after a usage, that is, it is a one-time use.
You need to use in total $ n $ keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.
During the process, you are allowed to go into debt; for example, if you have $ 1 $ coin, you are allowed to buy a good key worth $ k=3 $ coins, and your balance will become $ -2 $ coins.
Find the maximum number of coins you can have after opening all $ n $ chests in order from chest $ 1 $ to chest $ n $ .
输入格式
The first line contains a single integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases.
The first line of each test case contains two integers $ n $ and $ k $ ( $ 1 \leq n \leq 10^5 $ ; $ 0 \leq k \leq 10^9 $ ) — the number of chests and the cost of a good key respectively.
The second line of each test case contains $ n $ integers $ a_i $ ( $ 0 \leq a_i \leq 10^9 $ ) — the amount of coins in each chest.
The sum of $ n $ over all test cases does not exceed $ 10^5 $ .
输出格式
For each test case output a single integer — the maximum number of coins you can obtain after opening the chests in order from chest $ 1 $ to chest $ n $ .
Please note, that the answer for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
样例 #1
样例输入 #1
5
4 5
10 10 3 1
1 2
1
3 12
10 10 29
12 51
5 74 89 45 18 69 67 67 11 96 23 59
2 57
85 60
样例输出 #1
11
0
13
60
58
提示
In the first test case, one possible strategy is as follows:
- Buy a good key for $ 5 $ coins, and open chest $ 1 $ , receiving $ 10 $ coins. Your current balance is $ 0 + 10 - 5 = 5 $ coins.
- Buy a good key for $ 5 $ coins, and open chest $ 2 $ , receiving $ 10 $ coins. Your current balance is $ 5 + 10 - 5 = 10 $ coins.
- Use a bad key and open chest $ 3 $ . As a result of using a bad key, the number of coins in chest $ 3 $ becomes $ \left\lfloor \frac{3}{2} \right\rfloor = 1 $ , and the number of coins in chest $ 4 $ becomes $ \left\lfloor \frac{1}{2} \right\rfloor = 0 $ . Your current balance is $ 10 + 1 = 11 $ .
- Use a bad key and open chest $ 4 $ . As a result of using a bad key, the number of coins in chest $ 4 $ becomes $ \left\lfloor \frac{0}{2} \right\rfloor = 0 $ . Your current balance is $ 11 + 0 = 11 $ .
At the end of the process, you have $ 11 $ coins, which can be proven to be maximal.
我就是不想动脑子的SB
明显有直觉,两种操作有两段性,用邻项交换证明一下就行。
证明之后维护一个前缀和,观察到 /2 ,警觉!观察值域,因为 $log_2{10^9} = 29.xxx $ ,所以时间复杂度 O ( 30 n ) O(30n) O(30n)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo_(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
template<typename T>
ostream& operator<<(ostream& os,const vector<T>&v){
for(int i=0,j=0;i<v.size();i++,j++)if(j>=5){
j=0;puts("");}else os<<v[i]<<" ";return os;}
template<typename T>
ostream& operator<<(ostream& os,const set<T>&v){
for(auto c:v)os<<c<<" ";return os;}
template<typename T1,typename T2>
ostream& operator<<(ostream& os,const map<T1,T2>&v){
for(auto c:v)os<<c.first<<" "<<c.second<<endl;return os;}
template<typename T>inline void rd(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {
if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
typedef pair<int,int>PII;
typedef pair<long,long>PLL;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+10,M=1e9+7;
ll n,k,m,_;
ll a[N];
void solve(){
cin>>n>>k;
fo(i,1,n)cin>>a[i];
ll sum,ans;
ans = sum = 0;
fo(i,0,n){
ll cnt = 0;
for(int j=i+1;j<=min((int)n,i+31);j++){
ll v = a[j];
v >>= (j-i);
cnt+=v;
}
if(i>=1)
sum += (a[i]-k);
ans = max(sum+cnt,ans);
}
cout<<ans<<endl;
}
int main(){
cin>>_;
while(_--){
solve();
}
return 0;
}
边栏推荐
- Redis主从、哨兵、 Cluster集群一锅端!
- Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
- 非稳压 源特电子 隔离电源模块芯片 5W VPS8504B 24V
- 第七周复习
- centos安装mysql8
- 【LeetCode】1374. Generate a string with an odd number of each character
- MySQL中根据日期进行范围查询
- DVWA安装教程(懂你的不懂·详细)
- aws s3 upload file
- Navicat cannot connect to database Mysql because of WiFi
猜你喜欢

关于跨域问题

01-Node-Express系统框架搭建(express-generator)
![[Daily LeetCode]——1. The sum of two numbers](/img/11/8a68f4ecb24fa19e3c804d536cdbec.png)
[Daily LeetCode]——1. The sum of two numbers

Navicat cannot connect to database Mysql because of WiFi

DVWA之SQL注入

常见的SQL面试题:经典50例

Invalid bound statement (not found)出现的原因和解决方法

2W字!详解20道Redis经典面试题!(珍藏版)

MySQL8.0.26 installation and configuration tutorial (windows 64-bit)

深度学习:目标检测入门知识
随机推荐
给你一个大厂面试的机会,你能面试上吗?进来看看!
PHP WebSehll backdoor script and detection tool
ASP WebShell 后门脚本与免杀
总体写作原则
Invalid bound statement (not found)出现的原因和解决方法
Navicat cannot connect to database Mysql because of WiFi
Tree Chain Segmentation-
Go简单实现协程池
MySQL8--Windows下使用msi(图形界面)安装的方法
MySql中的like和in走不走索引
2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)
【Koltin Flow(三)】Flow操作符之中间操作符(一)
Week 304 Dunk
7-44 基于词频的文件相似度 (30 分)
ROS2自学笔记:launch文件完整编写流程
消息队列经典十连问
Hit the programmer interview scene: What did Baidu interviewers ask me?
[LeetCode] 83. Delete duplicate elements in the sorted list
合奥科技网络 面试(含参考答案)
Chapter 10_Index Optimization and Query Optimization