558C

题意:给你n个数,可对每一个数进行操作(乘2或者除以2)。求最少的操作使得全部的数都相等。

思路 : dp[ t ] 表示全部的数转化到 t 所需的最少操作, vis[ t ] 表示有多少数能够转化成 t 。

对于一个数 num , 把它所能到达的数用上述的数组记录下即可了(详细看代码)。

注意 :

输入:

3

5 4 4

输出 : 2  (5/2*2=4)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100010;
int n,vis[maxn],num[maxn];
void initial()
{
memset(num,0,sizeof(num));
memset(vis,0,sizeof(vis));
}
void deal(int op)
{
int tp=op,ct=0;
while(tp)
{
vis[tp]++;
num[tp]+=ct;
if(tp%2==1 && tp!=1)
{
int yp=tp/2*2,cnt=ct+2;
while(yp<maxn)
{
vis[yp]++;
num[yp]+=cnt;
yp*=2;
cnt++;
}
}
tp/=2;
ct++;
}
tp=op*2,ct=1;
while(tp<maxn)
{
vis[tp]++;
num[tp]+=ct;
tp*=2;
ct++;
}
}
void input()
{
int u;
for(int i=0;i<n;i++)
{
scanf("%d",&u);
deal(u);
}
}
void solve()
{
int Min=1<<30;
for(int i=0;i<maxn;i++) if(vis[i]==n) Min=min(Min,num[i]);
cout<<Min<<endl;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
initial();
input();
solve();
}
return 0;
}

558D

题意:给出n条信息。要你推断信息是否矛盾。或是否有多个出口,或是否有唯一出口。

信息有两种类型,一个是出口的若干区间,一个不是出口若干区间。

思路: 先通过出口的若干区间找出出口所在的树中根节点的区间。

然后在通过不是出

口的若干区间来推断。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define ll long long
using namespace std; struct node
{
ll l,r;
node(){}
node(ll _l,ll _r):l(_l),r(_r){}
};
vector <node> G;
int n,m;
ll st,ed;
bool cmp(node p,node q)
{
if(p.l==q.l) return p.r<q.r;
return p.l<q.l;
}
void initial()
{
G.clear();
st=(ll)1<<(n-1);
ed=((ll)1<<n)-1;
}
void input()
{
int tp,t;
ll u,v;
for(int i=0;i<m;i++)
{
scanf("%d %I64d %I64d %d",&tp,&u,&v,&t);
u=u<<(n-tp);
for(int j=0;j<n-tp;j++) v=v<<1|1;
if(t)
{
st=max(st,u);
ed=min(ed,v);
}
else G.push_back(node(u,v));
}
G.push_back(node(ed+1,ed+1));
}
void solve()
{
ll ans=-1;
sort(G.begin(),G.end(),cmp);
for(int i=0;i<G.size();i++)
{
if(st>ed) break;
if(st<G[i].l)
{
if(ans!=-1 || st+1<G[i].l)
{
cout<<"Data not sufficient!"<<endl;
return ;
}
ans=st;
}
st=max(st,G[i].r+1);
}
if(ans==-1) cout<<"Game cheated!"<<endl;
else cout<<ans<<endl;
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
initial();
input();
solve();
}
return 0;
}

558E

题意:给你一个长度为n的字符串(下标从1開始),然后给你m个操作。每一个操作有三个值 l,r,t。

假设t=1。表示将字符串中[ l ,r ]的部分依照升序排列。

假设t=0。表示将字符串中[
l ,r ]的部分依照降序排列。

最后要你输出原字符串经过m次操作后所形成的新的字符串。

思路:对于26个小写字母(a-z),分别建立线段树,即建26个线段树。

即每次改动 [ l , r ] 区间,则先通过26课线段树分别求出这个区间内的a–z的个数。然后将26课线

段树内的这一区间和置为0。

最后再依据顺序又一次给26课线段树的这一区间赋值即可了。

<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int N=100010;
const int M=26;
struct node
{
int l,r,sum,cover;
} a[M][N*4];
string str;
int n,m;
void build(int cnt,int l,int r,int k)
{
a[cnt][k].l=l;
a[cnt][k].r=r;
a[cnt][k].sum=0;
a[cnt][k].cover=-1;
if(l==r) return ;
int mid=(l+r)>>1;
build(cnt,l,mid,2*k);
build(cnt,mid+1,r,2*k+1);
}
void push_down(int cnt,int k)
{
if(a[cnt][k].cover!=-1)
{
a[cnt][k*2].cover=a[cnt][k*2+1].cover=a[cnt][k].cover;
a[cnt][k*2].sum=(a[cnt][k*2].r+1-a[cnt][k*2].l)*a[cnt][k*2].cover;
a[cnt][k*2+1].sum=(a[cnt][k*2+1].r+1-a[cnt][k*2+1].l)*a[cnt][k*2+1].cover;
a[cnt][k].cover=-1;
}
}
void update(int cnt,int l,int r,int k,int num)
{
if(l==a[cnt][k].l && r==a[cnt][k].r)
{
a[cnt][k].cover=num;
a[cnt][k].sum=(a[cnt][k].r+1-a[cnt][k].l)*num;
return ;
}
push_down(cnt,k);
int mid=(a[cnt][k].l+a[cnt][k].r)>>1;
if(r<=mid) update(cnt,l,r,2*k,num);
else if(l>mid) update(cnt,l,r,2*k+1,num);
else
{
update(cnt,l,mid,2*k,num);
update(cnt,mid+1,r,2*k+1,num);
}
a[cnt][k].sum=a[cnt][k*2].sum+a[cnt][k*2+1].sum;
}
int query(int cnt,int l,int r,int k)
{
if(l==a[cnt][k].l && r==a[cnt][k].r) return a[cnt][k].sum;
push_down(cnt,k);
int mid=(a[cnt][k].l+a[cnt][k].r)>>1;
if(r<=mid) return query(cnt,l,r,2*k);
else if(l>mid) return query(cnt,l,r,2*k+1);
else return query(cnt,l,mid,2*k)+query(cnt,mid+1,r,2*k+1);
}
void input()
{
for(int i=0; i<M; i++) build(i,1,n,1);
getchar();
getline(cin,str);
for(int i=1; i<=n; i++) update(str[i-1]-'a',i,i,1,1);
}
void solve()
{
int l,r,t;
while(m--)
{
int num[M];
scanf("%d %d %d",&l,&r,&t);
for(int i=0; i<M; i++)
{
num[i]=query(i,l,r,1);
update(i,l,r,1,0);
}
int pos=l;
if(t==1)
{
for(int i=0; i<M; i++)
if(num[i])
{
update(i,pos,pos+num[i]-1,1,1);
pos=pos+num[i];
}
}
else
{
for(int i=M-1; i>=0; i--)
if(num[i])
{
update(i,pos,pos+num[i]-1,1,1);
pos=pos+num[i];
}
}
}
for(int i=1; i<=n; i++)
for(int j=0; j<M; j++)
if(query(j,i,i,1))
{
printf("%c",j+'a');
break;
}
printf("\n");
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
input();
solve();
}
return 0;
}
</span>

Codeforces 558(C、D、E)总结的更多相关文章

  1. codeforces 558/C Amr and Chemistry(数论+位运算)

    题目链接:http://codeforces.com/problemset/problem/558/C 题意:把n个数变成相同所需要走的最小的步数易得到结论,两个奇数不同,一直×2不可能有重叠枚举每个 ...

  2. codeforces 558 E A Simple Task

    题目大意就是给一个字符串,然后多个操作.每次操作能够把每一段区间的字符进行升序或者降序排序,问终于的字符串是如何的. 做法的话就是用线段树维护区间和 一開始仅仅考虑字符串中字符'a'的情况.如果操作区 ...

  3. Codeforces Round #558 (Div. 2)

    目录 Codeforces Round #558 (Div. 2) 题解 A Eating Soup B Cat Party C Power Transmission D Mysterious Cod ...

  4. Codeforces Round #558 (Div. 2)-Cat Party (Hard Edition)-(前缀和 + 模拟)

    http://codeforces.com/problemset/problem/1163/B2 题意:有n天,每天有一个颜色,截取前x天,随便抽掉一天,使剩下的各个颜色出现的次数相等. 解题,也可以 ...

  5. Codeforces Round #558 (Div. 2)B(SET,模拟)

    #include<bits/stdc++.h>using namespace std;int a[100007];int cnt[100007];int main(){    int n; ...

  6. Codeforces Round #558 (Div. 2)C(计算几何,排列组合,模拟)

    #include<bits/stdc++.h>using namespace std;typedef struct{ double k,b;}node;node k[1000007];bo ...

  7. Codeforces Round 558(Div 2)题解

    这场比赛没有打,后来和同学们一起开了场镜像打…… B是SB题结果WA了5发…… C是SB题结果差5min调出……虽然中间有个老师讲题吃掉了1h D是比较神仙的题(2200),但是做出来了?算是比较超常 ...

  8. Codeforces Round #558 B2. Cat Party (Hard Edition)

    题面: 传送门 题目描述: 题意:确定最大的x,使去除掉前x天的其中一天后,所有不同数字的数量相等.   题目分析: 可能是我太久没打cf了,水题都做不出来. 这道题的关键在于:要记录相同数量,的不同 ...

  9. codeforces 558B. Amr and The Large Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...

随机推荐

  1. 程序员编写API遵循法则

    Api理解: 1.遵循函数.数据库字段命名规则 2.思考项目的远景(扩展性) 3.思考团队调用是否方便及舒服 4.思考是否有灵动性

  2. BZOJ 2728: [HNOI2012]与非

    2728: [HNOI2012]与非 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 786  Solved: 371[Submit][Status][ ...

  3. 使用python将ppm格式转换成jpg【转】

    转自:http://blog.csdn.net/hitbeauty/article/details/48465017 最近有个很火的文章,叫 有没有一段代码,让你觉得人类的智慧也可以璀璨无比? 自己试 ...

  4. JavaScript 之类型转换

    数值转换成字符串类型 1.利用 “+” 将数值加上一个长度为零的空字符串. 2.通过toString()方法.加入参数可以直接进行进制的转换. <script language="ja ...

  5. Python学习杂记_3_字符串操作的常用方法

    字符串操作 字符串是可以通过下标来进行取值的,但是由于字符串是不可变变量,不能通过下标来修改它的值(形式如 字符串[下标]),下标从0开始,最大下标值是字符串长度减1,即len(string)-1 P ...

  6. 牛客网 牛客小白月赛2 G.文

    G.文 链接:https://www.nowcoder.com/acm/contest/86/G 这个题wa了一发,有点智障,浮点数,式子里面要*1.0,忘了,然后wa了,改了就过了(脑子有坑) 代码 ...

  7. FZU 1057 a^b 【数论/九余定理】

    Accept: 1164    Submit: 3722Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Description 对于任 ...

  8. FZU 1078 计算循环冗余码【模拟】

    计算机网络中采用循环冗余码来校验数据的正确性.其原理是:发送方计算出待发送的二进制数据的循环冗余码,并随同原数据一起发送到接收方:接收方通过重新计算接收到的数据的循环冗余码,并和收到的循环冗余码进行比 ...

  9. JMeter性能测试常用之事务控制器实例

    通常进行性能测试时,我们一般仅考虑主要的数据返回,不考虑页面渲染所需要的数据(例如:css.js.图片等).但当我们需要衡量打开一个页面(页面渲染完成)的性能时,我们就需要考虑完成页面渲染所需要的图片 ...

  10. java 之webmagic 网络爬虫

    webmagic简介: WebMagic是一个简单灵活的Java爬虫框架.你可以快速开发出一个高效.易维护的爬虫. http://webmagic.io/ 准备工作: Maven依赖(我这里用的Mav ...