前言:最近被线段树+简单递推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. Using-jqGrid-s-search-toolbar-with-multiple-filter

    http://www.codeproject.com/Articles/58357/Using-jqGrid-s-search-toolbar-with-multiple-filter

  2. python 使用联动优势支付接口的sign与verify

    直接上代码 if options.umpay_private_key is not None and len(options.umpay_private_key) > 0: try: with ...

  3. js实现对身份证校验

    var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古&quo ...

  4. rman 命令

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - ...

  5. Android--获取使用的总流量和每个App的上传、下载的流量

    获得每个App的上传.下载的流量. 思路就是获取到我们手机上的所有app,再获得app里面使用的权限,如果app有网络权限,就显示出来. 代码很简单,代码里面也有比较详细的注释,下面直接上代码 布局文 ...

  6. Android里面的命名规范

    前前后后接触安卓也有一段时间了,但是对于Android命名规范这块一直没有太注意过.导致有的时候写出来的代码,前后的风格根本不一样,今天在网上查了一下,正好对自己来说可以好好的总结一下. 首先在And ...

  7. android 下滤镜效果的实现

    android 下滤镜效果的实现 滤镜过滤颜色已实现,简单版本可通过下面代码的3个参数实现黑白.红.绿...等7种过滤(RGB的7种组合). 理论上讲可以过滤为任意颜色.调整混合结果的比值就行了. p ...

  8. go again

    Introducation (1)How to organize go code (2)How to develope go package (3)How to use go tool How to ...

  9. C#判断字符串为空

    string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } if ...

  10. 51.ISE中的DCM全局时钟转为普通IO

    在用DCM这个IP核时,它的输入时钟为全局时钟引脚输入,输出有两种情况,第一,可以直接接在全局时钟引脚:第二,可以通过ODDR2原语接在普通IO引脚:说下第二种是怎么用的: DCM DCM_INST ...