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 ...
随机推荐
- 【C++札记】引用
介绍 引用是C++中特有的语法,在C语言中不存在. 本质上引用(reference)就是指针,在类型名后面加上一个&号就是引用类型. 1.指针与引用的定义进行比较 指针定义: 引用定义: in ...
- C++基础--函数重载
函数重载定义: 在相同的作用域中具有相同的函数名而函数形参列表(参数类型.参数个数.参数顺序)不同的两个函数,称之为函数重载.注意:函数返回值类型并不是重载的条件. 函数重载优点: 可以使用相同的函数 ...
- Python习题003
作业一:将字符串”k:1/k1:2/k2:3/k3:4”处理成字典(比较难) 方法一 list = 'k:1/k1:2/k2:3/k3:4' new_list = list.split("/ ...
- 数据库中间件之mycat安装部署(一)
在学习数据库中间件前,我们先抛出三个问题 1.数据库数据量不大,但并发读写操作很大,应该怎么办? 此时我们首先考虑使用缓存中间件来减轻读压力,如果不能满足则考虑数据库读写分离,此时就会引入新的问题,这 ...
- .htaccess 转 SAE AppConfig
新浪的SAE不支持 htaccess,但是他们开发了 AppConfig,可以完全代替 htaccess 的常见功能,AppConfig采用类自然语言的规则描述,还是很人性化的. 这里来写一个短网址的 ...
- css之弹性盒模型
弹性盒子(Flexible Box/filebox)是一种当页面需要适应不同的屏幕大小以及设备类型时确保元素拥有恰当的行为的布局方式.引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子 ...
- main函数前后
void f1(void)__attribute__((constructor)); void f2(void)__attribute__((destructor)); void f1(void) { ...
- swore tcp服务学习
TcpServer.php <?php /** * Created by PhpStorm. * User: mac * Date: 2019/9/13 * Time: 20:33 */ cla ...
- C#面向对象 什么是面向对象
1.面向对象(Object Oriented,OO) 是当前计算机界关心的重点,它是90年代软件开发方法的主流.面向对象的概念和应用已超越了程序设计和软件开发,扩展到很宽的范围.如数据库系统.交互式界 ...
- SPI、I2C、I2S
1. SPI总线 1.1 基础概念: 技术性能 SPI接口是Motorola 首先提出的全双工三线同步串行外围接口,采用主从模式(Master Slave)架构:支持多slave模式应用,一般仅支持单 ...