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 #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  2. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  3. Codeforces Round #590 (Div. 3)【D题:维护26棵树状数组【好题】】

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

  4. Codeforces Round #590 (Div. 3)补题

    要想上2000分,先刷几百道2000+的题再说 ---某神 题目 E F 赛时是否尝试 × × tag math bitmask 难度 2000 2400 状态 ∅ √ 解 E 待定 F 传送门 第一 ...

  5. Codeforces Round #305 (Div. 2) D题 (线段树+RMQ)

    D. Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  6. Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)

    F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...

  7. Codeforces Round #590 (Div. 3) Editorial

    Codeforces Round #590 (Div. 3) Editorial 题目链接 官方题解 不要因为走得太远,就忘记为什么出发! Problem A 题目大意:商店有n件商品,每件商品有不同 ...

  8. Codeforces Round #378 (Div. 2) D题(data structure)解题报告

    题目地址 先简单的总结一下这次CF,前两道题非常的水,可是第一题又是因为自己想的不够周到而被Hack了一次(或许也应该感谢这个hack我的人,使我没有最后在赛后测试中WA).做到C题时看到题目情况非常 ...

  9. Codeforces Round #612 (Div. 2) 前四题题解

    这场比赛的出题人挺有意思,全部magic成了青色. 还有题目中的图片特别有趣. 晚上没打,开virtual contest打的,就会前三道,我太菜了. 最后看着题解补了第四道. 比赛传送门 A. An ...

随机推荐

  1. Django导出数据为Excel,调用浏览器下载

    1. 环境 Django (2.1.10) + Python3.6 + xlwt (1.3.0) 操作系统使用的为:Windows 7 2. 接口代码 def now_export(request): ...

  2. Warning: popen() has been disabled for security reasons in OS/Guess.php on line 241

    今天使用pecl install swoole命令编译安装swoole的时候提示:Warning: popen() has been disabled for security reasons in ...

  3. Centos7.3 为php7 安装swoole 扩展

    今天心血来潮想在服务器上安装一下swoole扩展  下面列一下教程: xshell进入你的服务器  然后目录自选吧  反正我放在根目录了 下面是扩展链接: wget https://github.co ...

  4. (四)spring+servlet 整合

    一.Spring与Servlet的整合 1.1: 加入Spring的jar包.(要加web.jar包) 1.2: java工程中获取Spring的上下文对象. ApplicationContext c ...

  5. ppt调整三级标题的位置

    ---恢复内容开始--- 标题格式:一级标题   中文数字加.例如 一. 二级标题  中文数字加:   例如二: 三级标题  小写数字加.  例如3. 使用方法: 打开PPT  按alt+f11,打开 ...

  6. JS基础_构造函数

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 树莓派二:apt-get出错、蓝牙、汉化、输入法

    用apt-get install一个软件的时候出现了一个错误: E: Encountered a section with no Package: header E: Problem with Mer ...

  8. 服务器去掉IE浏览器网络安全限制

  9. 8.Redis的复制(Master/Slave)

    Redis的复制(Master/Slave) a)是什么 行话:也就是我们所说的主从复制,主机数据更新后根据配置和策略,自动同步到备机的master/slaver机制,Master以写为主,Slave ...

  10. [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)

    每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...