Price List

题意:

有n件商品,每天只能买一件,并且会记录账本,问有多少次一定记多了?

题解:

就是求和,最后如果大于和就输出1,否则0。

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
#define SI(N) cin>>(N)
#define SII(N,M) cin>>(N)>>(M)
#define rep(i,b) for(int i=0;i<(b);i++) int n,m;
int main()
{
int o;
SI(o);
while(o--)
{
SII(n,m);
ll sum=0,a;
rep(i,n) SI(a),sum+=a;
rep(i,m)
{
SI(a);
if (a>sum) putchar('1');
else putchar('0');
}
puts("");
}
return 0;
}

NanoApe Loves Sequence(线段树)

题意:

在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情随便删了一个数,这样他得到了一个新的数列,然后他计算出了所有相邻两数的差的绝对值的最大值。

他当然知道这个最大值会随着他删了的数改变而改变,所以他想知道假如全部数被删除的概率是相等的话,差的绝对值的最大值的期望是多少。

题解:

这题可以简单的模拟下,先求出最大值,并记录位置,之后枚举每个要删除的数字,与最大值比较就好,但其中一定会到一个影响最大值的数字,这时候,重新算遍最大值就好了。这是精简的算法,我想的是线段树,有些麻烦:先算出各位之差,之后删除1个点会修改2个绝对值,之后在查询最大值,ans+=最大值,最后不要忘了在更新回来。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; typedef long long ll; #define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 100000+9;
int MAX[maxn<<2]; int inp[maxn];
int iab[maxn],cntb; void PushUP(int rt) {
MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]);
}
void build(int l,int r,int rt) {
if (l == r) {
MAX[rt]=iab[cntb++];
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update(int p,int sc,int l,int r,int rt) {
if (l == r) {
MAX[rt] = sc;
return ;
}
int m = (l + r) >> 1;
if (p <= m) update(p , sc , lson);
else update(p , sc , rson);
PushUP(rt);
}
int query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
return MAX[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if (L <= m) ret = max(ret , query(L , R , lson));
if (R > m) ret = max(ret , query(L , R , rson));
return ret;
} int main() {
int o;
scanf("%d",&o);
while(o--)
{
memset(iab,0,sizeof(iab));
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&inp[i]);
}
for(int i=1;i<=n-1;i++)
{
iab[i]=abs(inp[i+1]-inp[i]);
}
cntb=1;
n-=1;
build(1,n,1);
ll ans=0;
ans+=query(2 , n , 1 , n , 1);
for(int i=2;i<=n;i++)
{
update(i, abs(inp[i+1]-inp[i-1]), 1 , n , 1);
update(i-1, abs(inp[i+1]-inp[i-1]), 1 , n , 1);
int d=query(1 , n , 1 , n , 1);
update(i, abs(inp[i+1]-inp[i]), 1 , n , 1);
update(i-1, abs(inp[i]-inp[i-1]), 1 , n , 1);
ans+=d;
}
ans+=query(1 , n-1 , 1 , n , 1);
printf("%I64d\n",ans);
}
return 0;
}

NanoApe Loves Sequence Ⅱ(尺取法)

题意:

在数学课上,NanoApe 心痒痒又玩起了数列。他在纸上随便写了一个长度为 n 的数列,他又根据心情写下了一个数 m。

他想知道这个数列中有多少个区间里的第 k 大的数不小于 m,当然首先这个区间必须至少要有 k 个数啦。

题解:

首先你要知道,这是尺取法的问题。你要有这么一个思维转换:求第k大的数不小于m也就相当于有k个数是大于等于m的,那么这题用尺取法也就很简单了,最后注意ans的取法,ans+=n-尾指针,int会溢出,所以用ll,以后答案尽量用ll,因为int总是容易爆

代码:

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int MAXN= 200000 + 9 ;
int a[MAXN];
int n,m,k; int main()
{
int o;
scanf("%d",&o);
while(o--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
int pre=0,nex=0,cnt=0;
ll ans=0;
for(int i=0; i<n; i++)
{
if (a[i]>=m)
cnt++;
if (cnt==k)
{
while(a[nex]<m)
{
ans+=n-i;
nex++;
}
ans+=n-i;
nex++;
cnt--;
}
}
printf("%I64d\n",ans);
}
return 0;
}

BestCoder Round #86 部分题解的更多相关文章

  1. [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP)

    [HDU5807] [BestCoder Round #86 1004] Keep In Touch (DP) 题面 有三个人从一张N个点无重边的有向无环图上的三个点出发,每单位时间,他们分别选择当前 ...

  2. BestCoder Round #86 二,三题题解(尺取法)

    第一题太水,跳过了. NanoApe Loves Sequence题目描述:退役狗 NanoApe 滚回去学文化课啦! 在数学课上,NanoApe 心痒痒又玩起了数列.他在纸上随便写了一个长度为 nn ...

  3. HDU 5805 NanoApe Loves Sequence (思维题) BestCoder Round #86 1002

    题目:传送门. 题意:题目说的是求期望,其实翻译过来意思就是:一个长度为 n 的数列(n>=3),按顺序删除其中每一个数,每次删除都是建立在最原始数列的基础上进行的,算出每次操作后得到的新数列的 ...

  4. HDU5808Price List Strike Back (BestCoder Round #86 E) cdq分治+背包

    严格按题解写,看能不能形成sum,只需要分割当前sum怎么由两边组成就好 #include <cstdio> #include <cstring> #include <c ...

  5. [BC]BestCoder Round#86小结

    1001 [题意] 给定一个长度为n(n<=100000)的正整数序列,给出m(m<=100000)个子集合和的记录,问哪些一定比正确的记录多了 [题解] 对正整数序列求和,记录比和大的一 ...

  6. BestCoder Round #86 A B C

    这次BC终于不像上次一样惨烈 终于A了三题…… 终测ing…… 发一波题解…… A.Price List A题十分无脑 只要把所有数加起来存到sum里 询问的时候大于sum输出1 否则输出0就行了…… ...

  7. BestCoder Round #86

    A题 Price List 巨水..........水的不敢相信. #include <cstdio> typedef long long LL; int main() { int T; ...

  8. BestCoder Round #86 解题报告

    A.Price List Sol 求和查询 Code #include<cstdio> #include<algorithm> #include<iostream> ...

  9. HDU5807 Keep In Touch (BestCoder Round #86 D ) 分布式dp

    #include <cstdio> #include <cstring> #include <cmath> #include <vector> #inc ...

随机推荐

  1. Android——GridView(显示文字)

    activity_test9的layout文件: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  2. (转) Deep Learning in a Nutshell: Core Concepts

    Deep Learning in a Nutshell: Core Concepts Share:   Posted on November 3, 2015by Tim Dettmers 7 Comm ...

  3. lua 初接触 --- The first time use Lua for programing

    The first time use Lua for programing Wang Xiao 1. 关于 lua 的变量类型:  lua 变量的定义与matlab有点不同: local d , f ...

  4. caffe: train error: Serializing 25 layers--- Check failed: proto.SerializeToOstream(&output)

    I0221 21:47:41.826748  6797 solver.cpp:259]     Train net output #0: loss = 0.00413362 (* 1 = 0.0041 ...

  5. C++ 遇到的问题小结

    1. cannot convert 'std::basic_string<char>' to 'int' in assignment ... 原始code如下: int id2; std: ...

  6. HttpWebRequest's Timeout and ReadWriteTimeout — What do these mean for the underlying TCP connection?

    http://stackoverflow.com/questions/7250983/httpwebrequests-timeout-and-readwritetimeout-what-do-thes ...

  7. MicroSoft Visual C++ 6.0怎么建立C++文件工程?

    1.打开VC6.02.选择菜单中的"文件"->"新建",弹出"新建"对话框3.在"新建"对话框中选择四个Sheet ...

  8. sed详解

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...

  9. jquery选取iframe

    $(window).load(function(){ $('iframe').contents().find('form'); })

  10. 爬虫入门---Python2和Python3的不同

    Python强大的功能使得在写爬虫的时候显得十分的简单,但是Python2和Python3在这方面有了很多区别. 本人刚入门爬虫,所以先写一点小的不同. 以爬取韩寒的一篇博客为例子: 在Python2 ...