hey_left 14 Codeforces Round 849 (Div. 4) 续
F.
区间修改,单点查询,考虑用树状数组
可以维护每个点需要操作的次数
然后直接对询问的点进行操作
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
#define int long long
struct TreeArray{
vector<int>tree;
TreeArray(int n){
tree.resize(n+1,0);
}
void update(int index,int value){
while(index<tree.size()){
tree[index]+=value;
index+=index&(-index);
}
}
int query(int index){
int sum=0;
while(index>0){
sum+=tree[index];
index-=index&(-index);
}
return sum;
}
};
void solve(){
int n,q;cin>>n>>q;
vector<int>a(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
TreeArray ta(N);
while(q--){
int op;cin>>op;
if(op==1){
int l,r;cin>>l>>r;
ta.update(l,1);
ta.update(r+1,-1);
}else if(op==2){
int x;cin>>x;
int time=ta.query(x);
int y=a[x];
int sum=0;
while(time--){
while(y){
sum+=y%10;
y/=10;
}
y=sum;
if(sum<10)break;
sum=0;
}
cout<<y<<'\n';
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
G1.
每个转换器的代价是固定的
那么直接把代价从小到大排序,能拿几个拿几个
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int n,c;cin>>n>>c;
vector<int>cost(n+1);
for(int i=1;i<=n;i++){
cin>>cost[i];
cost[i]+=i;
}
sort(cost.begin()+1,cost.end());
int ans=0,sum=0;
for(int i=1;i<=n;i++){
if(sum+cost[i]<=c){
sum+=cost[i];
ans++;
}else break;
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
G2.
同easy的思路,但要求起点在0
也就是必须有一个是从0开始的,其他随意
那么我们枚举从0开始的点
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve(){
int ans=0;
int n,c;cin>>n>>c;
vector<int>a(n+1);
vector<pair<int,int>>pa(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
pa[i]={a[i]+min(i,n+1-i),a[i]+i};
}
sort(pa.begin()+1,pa.end());
vector<int>pre(n+1);
for(int i=1;i<=n;i++){
pre[i]=pre[i-1]+pa[i].first;
}
int cnt;
for(int i=1;i<=n;i++){
if(c<pa[i].second)continue;
cnt= upper_bound(pre.begin()+1,pre.end(),c-pa[i].second+pa[i].first)-pre.begin()-1;
if(cnt<i)cnt= upper_bound(pre.begin()+1,pre.end(),c-pa[i].second)-pre.begin();
ans=max(ans,cnt);
}
cout<<ans<<'\n';
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int hey_left=1;
cin>>hey_left;
while(hey_left--){
solve();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现