Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】
A题
题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值。
AC代码:
#include<bits/stdc++.h> using namespace std;
#define int long long
signed main(){
int _;
cin>>_;
while(_--){
int n;
cin>>n;
int arr[n+];
int s=;
for(int i=;i<=n;i++){
cin>>arr[i];
s+=arr[i];
}
int x=s/n;
if(s%n==){
cout<<x;
}else{
cout<<x+;
}
cout<<endl;
}
return ;
}
B1,B2题
题意:给你个长度为 n 的数组和一个队列 , 队列最多可以同时存在 k 个数。遍历这个数组 , 如果当前数组对应的数在队列中则不做改动 , 如果不在则将它插入队首 , 并且将队尾弹出。遍历完后按照队列顺序输出。
思路:模拟即可【deque+map】
AC代码:
#include<bits/stdc++.h> using namespace std; #define int long long
map<int,int> vis;
deque<int> q;
signed main(){
int n,k;
cin>>n>>k;
int temp;
for(int i=;i<=n;i++){
scanf("%lld",&temp);
if(vis[temp]){
continue;
}else{
vis[temp]++;
q.push_front(temp); if(q.size()>k){
int x=q.back();
q.pop_back();
vis[x]--;
}
}
}
deque<int>::iterator it = q.begin();
cout<<q.size()<<endl;
for(;it!=q.end();it++){
printf("%lld ",*it);
}
return ;
}
C题:
题意:有六种管子 , 其中1、2可以互相转换 , 3、4、5、6可以互相转换 , 然后给你两行管道 , 每行有 n 列问水能不能从左上角(第1行第1列)流到右下角(第2行第n列)
思路:模拟即可。判断是否从row==2行流出。是,则判断流出的水是不是水平的。否则,直接NO。【注意:后面四种的形状只能上下两个都是才能往前走】
AC代码:
#include<bits/stdc++.h> using namespace std; int main(){
int _;
cin>>_;
while(_--){
int n;
cin>>n;
string s1,s2;
cin>>s1>>s2;
int mp[][n+];
int row=;
int flag=;
for(int i=;i<n;i++){
if(!flag){
break;
}
if(i==){
if(s1[i]==''||s1[i]==''){
mp[row][i]=;
}else{
mp[row][i]=;
row=;
if(s2[i]==''||s2[i]==''){
flag=;
break;
}else{
mp[row][i]=;
}
}
continue;
}
if(row==){
if(s1[i]==''||s1[i]==''){
mp[row][i]=;
continue;
}else{
mp[row][i]=;
row=;
if(s2[i]==''||s2[i]==''){
flag=;
break;
}else{
mp[row][i]=;
}
}
}else{
if(s2[i]==''||s2[i]==''){
mp[row][i]=;
continue;
}else{
mp[row][i]=;
row=;
if(s1[i]==''||s1[i]==''){
flag=;
break;
}else{
mp[row][i]=;
}
}
}
}
if(!flag||row==){
printf("NO\n");
continue;
}
if(mp[][n-]==||mp[][n-]==){
printf("YES\n");
}else{
printf("NO\n");
}
}
return ;
} /*
6
7
2323216
1615124
1
3
4
2
13
24
2
12
34
3
536
345
2
46
54 */
D题
题意:给你一个字符串 , 有q个操作:
①、 将 pos 位置的字符改为 c
②、查询 L~ R 区间不同字符的个数
思路1:set模拟实现。
AC代码:
#include<bits/stdc++.h> using namespace std;
set<int> s[];
char str[];
int main(){ scanf("%s",str+);
for(int i=;i<=strlen(str+);i++){
s[str[i]-'a'].insert(i);
}
int _;
scanf("%d",&_);
while(_--){
int T;
scanf("%d",&T);
if(T==){
int x;
char c;
scanf("%d",&x);
cin>>c;
s[str[x]-'a'].erase(x);
s[c-'a'].insert(x);
str[x]=c;
}else{ int l,r;//cin>>l>>r;
scanf("%d%d",&l,&r);
int ans=;
for(int i=;i<;i++){
set<int>::iterator it;
it=s[i].lower_bound(l);
if(it==s[i].end()){
continue;
}
if(*it>=l&&*it<=r)
ans++; }
printf("%d\n",ans);
}
} return ;
}
思路2:【了解了大佬们的写法】维护26个树状数组,代表每个字母从1到i出现了多少次,对于查询,遍历这26个树状数组看每个字母是否在区间内出现,对于修改,这个位置原来的字母减去1,新来的字母加上1即可.
AC代码:
#include<bits/stdc++.h>// 维护26棵树状数组QAQ using namespace std;
#define int long long
int n;
struct str{
int c[];
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=v;
}
int getsum(int x){
int res=;
for(int i=x;i;i-=lowbit(i))
res+=c[i];
return res;
}
int query(int l,int r){
return getsum(r)-getsum(l-);
}
}st[];
signed main(){
string s;
cin>>s;
n=s.size();
for(int i=;i<s.size();i++){
st[s[i]-'a'].update(i+,);
}
int Q;
cin>>Q;
int temp;
while(Q--){
cin>>temp;
if(temp==){
int x;
char y;
cin>>x>>y;
st[s[x-]-'a'].update(x,-);
s[x-]=y;
st[y-'a'].update(x,);
}else{
int sum=;
int L,R;
cin>>L>>R;
for(int i=;i<;i++){
if(st[i].query(L,R)>)
sum++;
}
printf("%lld\n",sum);
}
}
return ;
}
Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】的更多相关文章
- Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】
主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤ ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...
- Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s
C. Inna and Candy Boxes Inna loves sweets very much. She has n closed present boxes lines up in a ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
随机推荐
- RecursiveSequence(HDU-5950)【矩阵快速幂】
题目链接: 题意:Si=S(i-1)+2*S(i-2)+i^4,求Sn. 思路:想到了矩阵快速幂,实在没想出来怎么构造矩阵.... 首先构造一个向量vec={a,b,16,8,4,2,1}. 在构造求 ...
- ubuntu内lnmp相关操作命令
LNMP状态管理命令: **LNMP状态管理:** sudo lnmp {start|stop|reload|restart|kill|status} **Nginx状态管理:**sudo /etc/ ...
- docker 实践二:操作镜像
本篇我们来详细介绍 docker 镜像的操作. 注:环境为 CentOS7,docker 19.03 之前已经说过,容器是 docker 的核心概念之一,所以对应的就需要知道它的使用方法,接下来我们就 ...
- uboot中打开 debug调试信息的方法
在uboot目录下include/common.h中, 原理:只需要让 _DEBUG 的值为 1即可. 最简单的做法就是在下图第一行之前添加 #define DEBUG
- docker部署mysql,nginx,php,并上传镜像到私有仓库
前言 最近公司准备把现有环境全部搞成容器化,所以笔者就先了解了一下docker,并搞了一搞,并把自己搞的过程记录下来.话不多说直接开干 环境说明 Centos7 Docker version 18.0 ...
- golang跨平台编译
// 目标平台linux 64 SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build //目标平台windows SET CGO_ENA ...
- Linux学习之如何让普通用户获得ROOT权限
https://blog.csdn.net/qq_41940950/article/details/81044594
- 如何将 HTML 转换为 XHTML
1.添加一个 XHTML <!DOCTYPE> 到你的网页中 2.添加 xmlns 属性添加到每个页面的html元素中 3.改变所有的元素为小写 4.关闭所有的空元素 5.修改所有的属性名 ...
- (七)Redis之持久化之RDB方式
一.持久化概念 所有的数据都存在内存中,从内存当中同步到硬盘上,这个过程叫做持久化过程. 使用方法: 1. rdb持久化方法:在指定的时间间隔写入硬盘 2. aof方式:将以日志,记录 ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...