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棵树状数组【好题】】的更多相关文章

  1. Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】

    A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...

  2. Codeforces Round #248 (Div. 2) B称号 【数据结构:树状数组】

    主题链接:http://codeforces.com/contest/433/problem/B 题目大意:给n(1 ≤ n ≤ 105)个数据(1 ≤ vi ≤ 109),当中有m(1 ≤ m ≤  ...

  3. 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 ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Office 2016、2019 与 Office 365 的区别

    点开观看更清晰:

  2. 题解 CF437C

    基本思路---贪心 既然要求最小代价,当用一定顺序删除时代价一定最小,不难发现,每次都删去x,y中最小的,最后的总代价业一定最小! 因此就可以写出下面的简单的代码 代码 #include<ios ...

  3. Python 【收发邮件】

    发邮件 smtplib模块主要负责发送邮件 email模块主要负责构造邮件.这两个都是Python内置模块 smtplib.SMTP.方法 #按住Ctrl键并点击SMTP ,会看到对SMTP的解释(v ...

  4. 【数据结构 Python & C++】顺序表

    用C++ 和 Python实现顺序表的简单操作 C++代码 // Date:2019.7.31 // Author:Yushow Jue #include<iostream> using ...

  5. 【解决方法】You seem to have the current working directory in your LD_LIBRARY_PATH environment variable.

    参考地址:https://blog.csdn.net/qq_24755999/article/details/78722788 You seem to have the current working ...

  6. hdu 6165

    虽然题解上说缩点然后判断入度就可以了,然后比赛的时候瞎暴力过了. #include <iostream> #include <cstring> #include <str ...

  7. (十二)SpringBoot之Spring-Data-Jpa(一)

    一.Spring-Data-Jpa概念 JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate.TopLink等. Spring D ...

  8. 上传文件-layui+ashx

    public void ProcessRequest (HttpContext context) { if (true) { context.Response.ContentType = " ...

  9. gin框架封装自己的路由 ②

    在一个项目中,我们会有很多路由,那么我们该如何更好的管理自己的路由,在多人协同的情况下可以更好的规范路由呢,我来说一下自己的做法 1.承接gin框架初识(先跑一个简单demo) ①,先创建一个cont ...

  10. VBA消息框(MsgBox)(五)

    MsgBox函数显示一个消息框,并等待用户点击一个按钮,然后根据用户点击的按钮执行相关的操作. 语法 MsgBox(prompt[,buttons][,title][,helpfile,context ...