NOI.AC NOIP模拟赛 第一场 补记

candy

题目大意:

有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元。每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗糖果的愉悦度为\(A_i\),而第二家商店中的第\(i\)颗糖果的愉悦度为\(B_i\)。

在每家商店买的糖果会被打包到一个袋子中(可以在一家商店什么都不买,此时认为这家商店的袋子为空)。因为这两个袋子外观是一样的,所以会从两个袋子中随机选择一个,然后吃光里面的糖果。定义一种买糖果的方案的愉悦度为:吃到的糖果的愉悦度之和的最小可能值。

求买糖果的愉悦度与买糖果的花费之差的最大值。

思路:

显然对于一家店,购买相同数量的糖果,一定选择愉悦度尽量高的更优。

因此将\(A_i\)和\(B_i\)从大到小排序,求前缀和。答案就是\(\max\{\min(A_i,B_j)-(i+j)W\}\)。枚举每一个\(A_i,B_j\)作为\(\min\),然后另一个数就可以通过二分求出来。

时间复杂度\(\mathcal O(n\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=1e5+1;
int64 a[N],b[N];
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) a[i]=getint();
for(register int i=1;i<=n;i++) b[i]=getint();
std::reverse(&a[1],&a[n]+1);
std::reverse(&b[1],&b[n]+1);
for(register int i=1;i<=n;i++) a[i]+=a[i-1];
for(register int i=1;i<=n;i++) b[i]+=b[i-1];
int64 ans=0;
for(register int i=0;i<=n;i++) {
const int j=std::lower_bound(&b[0],&b[n]+1,a[i])-b;
if(j<=n) ans=std::max(ans,a[i]-(int64)(i+j)*m);
}
for(register int i=0;i<=n;i++) {
const int j=std::lower_bound(&a[0],&a[n]+1,b[i])-a;
if(j<=n) ans=std::max(ans,b[i]-(int64)(i+j)*m);
}
printf("%lld\n",ans);
return 0;
}

sort

来源:

Ufa SATU + Bucharest U Contest J. Reverse Sort

题目大意:

一个长度为\(n(n\le50000)\)的序列\(A\)。每次操作可以将一个区间翻转,定义翻转区间\([l,r]\)的代价为\(r-l+1\)。要通过翻转将这个序列排序,请你构造出代价小小于\(2\times10^7\)的一种方案。

思路:

当\(A_i\in\{0,1\}\)时,用归并排序的思想,每次归并时将左子区间的后缀\(1\)与右子区间的前缀\(0\)交换即可。

而没有\(A_i\in\{0,1\}\)的条件时,我们可以利用快速排序的思想,每次从区间内随机选取一个数\(x\)作为基准,\(\le x\)的数作为\(0\),\(>x\)的数作为\(1\)。然后内层套用上述归并排序的算法。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e4+1;
int a[N];
inline bool check(const int &b,const int &e) {
for(register int i=b;i<e;i++) {
if(a[i]>a[i+1]) return false;
}
return true;
}
void solve(const int &b,const int &e,const int &x) {
if(b==e) return;
const int mid=(b+e)>>1;
solve(b,mid,x);
solve(mid+1,e,x);
int p=b,q=e;
while(p<=mid&&a[p]<=x) p++;
while(q>mid&&a[q]>x) q--;
if(p<=mid&&q>mid) {
printf("%d %d\n",p,q);
std::reverse(&a[p],&a[q]+1);
}
}
void solve(const int &b,const int &e) {
if(b>=e) return;
if(check(b,e)) return;
const int x=a[b+rand()%(e-b+1)];
solve(b,e,x);
for(register int i=b;i<=e;i++) {
if(a[i]>x) {
solve(b,i-1);
solve(i,e);
return;
}
}
solve(b,e);
}
int main() {
srand(998244353);
const int n=getint();
for(register int i=1;i<=n;i++) a[i]=getint();
solve(1,n);
puts("-1 -1");
return 0;
}

NOI.AC NOIP模拟赛 第一场 补记的更多相关文章

  1. NOI.AC NOIP模拟赛 第二场 补记

    NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...

  2. NOI.AC NOIP模拟赛 第四场 补记

    NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...

  3. NOI.AC NOIP模拟赛 第三场 补记

    NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...

  4. NOI.AC NOIP模拟赛 第五场 游记

    NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...

  5. NOI.AC NOIP模拟赛 第六场 游记

    NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...

  6. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  7. NOI.AC NOIP模拟赛R3解题报告

    心路历程 预计得分:\(100+100+50=250\) 实际得分:\(10 +100 +50 = 160\) 三道原题,真好.T2做过,T1写了个错误思路,T3写了写50分状压dp. 整场考试实际在 ...

  8. 计蒜客 NOIP 提高组模拟竞赛第一试 补记

    计蒜客 NOIP 提高组模拟竞赛第一试 补记 A. 广场车神 题目大意: 一个\(n\times m(n,m\le2000)\)的网格,初始时位于左下角的\((1,1)\)处,终点在右上角的\((n, ...

  9. [NOI.AC 2018NOIP模拟赛 第三场 ] 染色 解题报告 (DP)

    题目链接:http://noi.ac/contest/12/problem/37 题目: 小W收到了一张纸带,纸带上有 n个位置.现在他想把这个纸带染色,他一共有 m 种颜色,每个位置都可以染任意颜色 ...

随机推荐

  1. Confluence wiki——CentOS6.8搭建详解

    参考资料:http://www.cnblogs.com/jackyyou/p/5534231.html http://www.ilanni.com/?p=11989 公司需要搭建WIKI方便员工将一些 ...

  2. HTML的文档类型:<!DOCTYPE >

    <!DOCTYPE> 声明:它不是 HTML 标签而且对大小写不敏感,而是指示 web 浏览器关于页面使用哪个 HTML 版本进行编写的指令.而且 声明必须是 HTML 文档的第一行,位于 ...

  3. 工欲善其事必先利其器,用Emmet提高HTML编写速度

    HTML代码写起来很费事,因为它的标签多. 一种解决方法是采用模板,在别人写好的骨架内,填入自己的内容.还有一种很炫的方法----简写法. 常用的简写法,目前主要是Emmet和Haml两种.这两种简写 ...

  4. 第8月第16天 django pil

    1. https://github.com/chaonet/forum/ sudo  easy_install --find-links http://www.pythonware.com/produ ...

  5. (FFT) A * B Problem Plus

    题目链接:https://cn.vjudge.net/contest/280041#problem/F 题目大意:给你两个数,求这俩数相乘的结果.(长度最长5000) 具体思路:硬算肯定是不行的,比如 ...

  6. 设置linux的console为串口【转】

    转自:http://blog.chinaunix.net/uid-27717694-id-4074219.html 以Grub2为例:1. 修改文件/etc/default/grub   #显示启动菜 ...

  7. 刘昕鑫 C# 特性详解

    C# 特性详解 特性(attribute)是被指定给某一声明的一则附加的声明性信息. 在C#中,有一个小的预定义特性集合.在学习如何建立我们自己的定制特性(custom attributes)之前,我 ...

  8. WCF服务安全控制之netTcpBinding的用户名密码验证【转】

    选择netTcpBinding WCF的绑定方式比较多,常用的大体有四种: wsHttpBinding basicHttpBinding netTcpBinding wsDualHttpBinding ...

  9. Algorithm类介绍(core)

    参考:http://blog.csdn.net/yang_xian521/article/details/7533922

  10. .NetCore SkyWalking APM实现服务器监控环境安装及基础使用

    下载Java 8 SDK wget  --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fw ...