前言:最近被线段树+简单递推DP虐的体无完肤!真是弱!

A:简单题,照着模拟就可以,题目还特意说不用处理边界

B:二分查找即可,用lower_lound()函数很好用

 #include<string.h>
#include<math.h>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<string>
#include<iostream>
using namespace std;
#define N 200005
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1 int a[N],s[N];
int main()
{
int n;
scanf("%d",&n);
for (int i=;i<=n;i++)
scanf("%d",&a[i]); for (int i=;i<=n;i++)
a[i]+=a[i-];
int m;
scanf("%d",&m);
while (m--)
{
int x;
scanf("%d",&x);
int p=lower_bound(a+,a+n+,x)-a;
printf("%d\n",p);
}

C:没写,但是是一道比较难处理的模拟题吧,求每个点绕它中轴点顺时针旋转90度,求能形成的最小旋转次数,4^4次,还有面积要大于0这个坑点。

D:简单DP,居然推到数学公式,真是SB,我们要使连续的字符串里面个W个数为x*k;当时想到求排列组合,以为可以化简,结果自己掉坑里!

正确做法,定义一维数组,就是求前导和。

方程:DP[I]=DP[I-1]+DP[I-K];(i>=k);(表示:I不为好的话,和为好的发时的总数)

DP[I]=DP[I-1];

s[i]=s[i-1]+DP[I]; (对前i段累加)

中间有Mod 操作,有很多询问,所以要一次做,然后之间求区间值即可!

 #include<bits/stdc++.h>
typedef long long ll;
using namespace std; #define mod 1000000007
#define N 100007
int a[N];
ll s[N]; int main()
{
int k,t;
scanf("%d%d",&t,&k);
a[]=;
for (int i=;i<N;i++)
{
if (i>=k) {a[i]=a[i-]+a[i-k];a[i]%=mod;}
else {a[i]=a[i-];a[i]%=mod;} s[i]=s[i-]+a[i];
s[i]%=mod;
} while (t--)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",(s[y]-s[x-]+mod)%mod);
} return ;

E:题目简短,二维可以写出,但会TLE。

然后看到一个神奇骗AC代码,可以看看

 include<bits/stdc++.h>
#define N 111111
using namespace std;
typedef long long ll;
int n;
ll h[N],ans[N],next[N];
ll d;
int main()
{
cin>>n>>d;
for (int i=;i<=n;i++)
{
cin>>h[i];
ans[i]=;
next[i]=-;
}
int mx=;
int start=n;
for (int i=n;i>=;i--){
for (int j=i+;j<=min(i+,n);j++)
{
if (abs(h[j]-h[i])>=d&&ans[j]+>ans[i])
{
next[i]=j;
ans[i]=ans[j]+;
}
} if (ans[i]>mx)
{
mx=ans[i];
start=i;
}
} cout<<mx<<endl;
int i=start;
while (i!=-)
{
cout<<i<<" ";
i=next[i];
} return ;
}

它这里限制了第二重循环的次数,实际上可以第二重循环可以弄到300,或者更少,数据太弱吧!

正解:线段树单点跟新+二分!

分析写到代码里吧!

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l, m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 111111
ll h[N],hh[N],k;
int n;
int pre[N],ss[N];
int sum[N<<];
int dp[N]; void pushup(int rt)//求区间最大值
{
sum[rt]=max(sum[rt<<],sum[rt<<|]);
} void update(int p,int y,int l,int r,int rt)//单点更新
{ if (l==r)
{
if (sum[rt]<y) sum[rt]=y;
return;
}
int m=(l+r)>>;
if (p<=m) update(p,y,lson);
else update(p,y,rson);
pushup(rt);
} //询问L,R区间最大值
int query(int L,int R,int l,int r,int rt)
{
if (R<L) return ;
if (L<=l&&R>=r) return sum[rt]; int m=(l+r)>>;
int ret=;
if (L<=m) ret=query(L,R,lson);
if (m<R) ret=max(ret,query(L,R,rson));
return ret;
} int main()
{
scanf("%d%d",&n,&k);
for (int i=;i<=n;i++)
scanf("%I64d",&h[i]),hh[i]=h[i];
sort(h+,h+n+);
int len=;
for (int i=;i<=n;i++)
if (h[i]!=h[i-]) h[++len]=h[i];//排序处理 int ans=;
//整体说一下思路
//我们先排序出去重,把每个数据抽象变成一个点,然后维护。
//我们知道DP[I]=MAX(DP[J])+1,abs(h[i]-h[j])>=k;
//于是我们在前面找到h[i]-h[j]>=k中左右区间然后跟新,这应该是线段树+DP的共同点
//对H[J]-H[J]>=K同样处理。
for (int i=;i<=n;i++)
{
int l=lower_bound(h+,h+len+,hh[i]+k)-h;//这里的lower_bound与upper_bound不能弄错
int r=len;
int x=query(l,r,,len,);
r=upper_bound(h+,h+len+,hh[i]-k)-h;//求出左右区间,这里得多看看 x=max(x,query(,r-,,len,));
dp[i]=x+;
int y=lower_bound(h+,h+len+,hh[i])-h;
update(y,dp[i],,len,);
ans=max(ans,dp[i]);
}
printf("%d\n",ans);
ll hp=1e17;
int inx=;//输出的处理
for (int i=n;i>=;i--)
{
if (ans==) break;
if (dp[i]==ans&&abs(hh[i]-hp)>=k)
{
hp=hh[i];
ans--;
ss[++inx]=i; }
}
for (int i=inx;i>=;i--) printf("%d ",ss[i]);
return ;
}

F:可以这么认为求出(L,R)区间的GCD,然后问(L,R)有多少个值=GCD();

线段树求GCD()不是难点,

求一段区间有多少值=GCD();要用到神奇处理

int x=upper_bound(a+1,a+n+1,mp(mi,r))-lower_bound(a+1,a+n+1,mp(mi,l));

这里用pair()容器,可以知道有多少个

 #include<bits/stdc++.h>

 #define lson l,m,rt<<1
#define rson m+1,r, rt<<1|1
#define N 111111 #define mp make_pair
using namespace std;
pair<int,int>a[N]; int s[N<<]; int gcd(int x,int y)
{
if (y==) return x;
if (x%y==) return y;
return gcd(y,x%y);
} void pushup(int rt)
{
s[rt]=gcd(s[rt<<],s[rt<<|]);
} void build(int l,int r,int rt)
{
if (l==r)
{
s[rt]=a[l].first;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
pushup(rt);
} int query(int L,int R,int l,int r,int rt)
{
if (L<=l&&R>=r)
return s[rt];
int m=(l+r)>>;
int ret=;
if (L<=m) ret=gcd(ret,query(L,R,lson));
if (R>m) ret=gcd(ret,query(L,R,rson));
return ret;
} int main()
{
int n;
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i].first),a[i].second=i;
build(,n,);
sort(a+,a+n+); int Q;
scanf("%d",&Q);
while (Q--)
{
int l,r;
scanf("%d%d",&l,&r);
int mi=query(l,r,,n,);
int x=upper_bound(a+,a+n+,mp(mi,r))-lower_bound(a+,a+n+,mp(mi,l));
printf("%d\n",r-l+-x);
}
return ;
}

Codeforces Round #271 (Div. 2) F ,E, D, C, B, A的更多相关文章

  1. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  2. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  3. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round #271 (Div. 2)题解【ABCDEF】

    Codeforces Round #271 (Div. 2) A - Keyboard 题意 给你一个字符串,问你这个字符串在键盘的位置往左边挪一位,或者往右边挪一位字符,这个字符串是什么样子 题解 ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  6. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  7. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

随机推荐

  1. TE.TYCO.AMP/JST 现货资源备份库存表日志记录-2015-04-13

    行号 品牌 料号 品名规格 库存 1 molex 09-65-2028 Molex 5273-02A 600 2 tyco 103648-2 AMP 03 MTE RCPT HSG SR RIB .1 ...

  2. 我的WPF控件库——KAN.WPF.XCtrl(141105)

    自己开发的WPF控件库,只是初版,有扩展的Button,TextBox,Window.详细参见前几篇博文. WPF自定义控件(一)——Button:http://www.cnblogs.com/Qin ...

  3. CF 191 总结

    A. Flipping Game 链接:http://codeforces.com/contest/327/problem/A 题意:从 i 到 j 翻转一次使得 1 的 个数最多~ 直接暴力搞~ # ...

  4. js的reduce方法,改变头等函数

    头等函数:把编程变成了类似搭积木的方式编码,可以使用很少的代码,实现强大的功能函数. eg: getTotal:数组的求和运算. var myArray = [1,2,3,4]; var add = ...

  5. expr命令

    expr命令的兩大作用:1)四则运算:2)字符串的操作: 1.四则运算 [tough@localhost ~]$ + + [tough@localhost ~]$ + [tough@localhost ...

  6. 数据库事务故障恢复undo日志检查点

      checkpoint 检查点 checkpoint,即检查点.在undolog中写入检查点,表示在checkpoint前的事务都已经完成commit或者rollback 了,也就是检查点前面的事务 ...

  7. Redis 五:配置主从复制功能

    redis的主从复制事实上是非常简单的一件事情,甚至比mysql的配置还简单,因为基本不需要在主服务器上做任何操作 我们在同一台服务器上开不同的端口进行测试操作(安装部分就不说啦,前面的文章有::) ...

  8. SystemServer相关

    SystemServer分析 由Zygote通过Zygote.forkSystemServer函数fork出来的.此函数是一个JNI函数,实现在dalvik_system_Zygote.c中. 1.S ...

  9. android开发遇到SDK无法访问谷歌而安装不了的情况

    遇到SDK无法访问谷歌而安装不了的情况 1.修改C:\Windows\System32\drivers\etc的HOSTS文件,添加 #google_android更新203.208.46.146 d ...

  10. Linux: uid/euid/suid的关系

    三种进程用户的简单解释:三种用户/组ID:uid/gid: 实际用户/组IDeuid/egid: 有效用户/组ID, 进程执行某个应用的用户/组ID.suid/sgid: 设置用户/组ID, 应用所属 ...