最大值(max)

Time Limit:1000ms   Memory Limit:128MB

题目描述

LYK有一本书,上面有很多有趣的OI问题。今天LYK看到了这么一道题目:

这里有一个长度为n的正整数数列ai(下标为1~n)。并且有一个参数k。

你需要找两个正整数x,y,使得x+k<=y,并且y+k-1<=n。并且要求a[x]+a[x+1]+…+a[x+k-1]+a[y]+a[y+1]+…+a[y+k-1]最大。

LYK并不会做,于是它把题扔给了你。

输入格式(max.in)

第一行两个数n,k。

第二行n个数,表示ai。

输出格式(max.out)

两个数表示x,y。若有很多种满足要求的答案,输出x最小的值,若x最小仍然还有很多种满足要求的答案,输出y最小的值。

输入样例

5 2

6 1 1 6 2

输出样例

1 4

对于30%的数据n<=100。

对于60%的数据n<=1000

对于100%的数据1<=n<=100000,1<=k<=n/2,1<=ai<=10^9。

思路:维护连续的k个数的和 的后缀最大值

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define max(a,b) a>b?a:b
#define MAXV 500010
long long ma,mx,my,ans;
long long lx[MAXV],ly[MAXV],lt[MAXV],rt[MAXV];
long long a[MAXV],rtm[MAXV],sum[MAXV],summ[MAXV];
void ch(long long &x,long long y,long long z,int pos,int poss,int i){
if(y<z){
x=z;
lx[i]=poss;
}
else{
x=y;
lx[i]=pos;
}
}
void hc(long long &x,long long y,long long z,int pos,int poss,int i){
if(y<z){
x=z;
ly[i]=poss;
}
else{
x=y;
ly[i]=pos;
}
}
void cc(long long &x,long long y,int i){
if(x<y){
x=y;
mx=lx[i-];
my=ly[i];
}
}
int main(){
freopen("max.in","r",stdin);
freopen("max.out","w",stdout);
int t,n,i,temp,k;
scanf("%d%d",&n,&k);
for(i=;i<=n;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
ans+=a[i];
}
for(int i=;i<=n;i++)
summ[i]=ans-sum[i-];
for(int i=;i<=k;i++)
lt[i]=sum[i],lx[i]=;
for(int i=n;i>=n-k+;i--)
rt[i]=summ[i],ly[i]=i;
for(int i=k+;i<=n;i++)
ch(lt[i],lt[i-],sum[i]-sum[i-k],lx[i-],i-k+,i);
for(int i=n-k;i>=;i--)
hc(rt[i],rt[i+],summ[i]-summ[i+k],ly[i+],i,i);
for(i=k+;i<=n-k+;i++)
cc(ma,lt[i-]+rt[i],i);
cout<<mx<<" "<<my;
return ;
}

吃东西(eat)

Time Limit:2000ms   Memory Limit:1024MB

题目描述

一个神秘的村庄里有4家美食店。这四家店分别有A,B,C,D种不同的美食。LYK想在每一家店都吃其中一种美食。每种美食需要吃的时间可能是不一样的。

现在给定第1家店A种不同的美食所需要吃的时间a1,a2,…,aA。

给定第2家店B种不同的美食所需要吃的时间b1,b2,…,bB。

以及c和d。

LYK拥有n个时间,问它有几种吃的方案。

输入格式(eat.in)

第一行5个数分别表示n,A,B,C,D。

第二行A个数分别表示ai。

第三行B个数分别表示bi。

第四行C个数分别表示ci。

第五行D个数分别表示di。

输出格式(eat.out)

一个数表示答案。

输入样例

11 3 1 1 1

4 5 6

3

2

1

输出样例

2

对于30%的数据A,B,C,D<=50

对于另外30%的数据n<=1000。

对于100%的数据1<=n<=100000000,1<=A,B,C,D<=5000,0<=ai,bi,ci,di<=100000000。

30分的暴力:

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 5010
using namespace std;
int n,A,B,C,D;
long long ans,cnt1,cnt2;
int num1[MAXN*MAXN],num2[MAXN*MAXN];
int a[MAXN],b[MAXN],c[MAXN],d[MAXN];
struct nond{
int x,y;
bool operator < (nond b) const{
return x>b.x;
}
}v;
priority_queue<nond>que,q,qu;
int main(){
freopen("eat.in","r",stdin);
freopen("eat.out","w",stdout);
scanf("%d%d%d%d%d",&n,&A,&B,&C,&D);
for(int i=;i<=A;i++)
scanf("%d",&a[i]);
sort(a+,a++A);
for(int i=;i<=B;i++){
scanf("%d",&b[i]);
v.x=b[i]+a[];v.y=;
que.push(v);
}
while(!que.empty()&&que.top().x<=n){
nond now=que.top();
num1[++cnt1]=now.x;
que.pop();
if(now.y+<=A){
now.x=now.x-a[now.y]+a[now.y+];
now.y+=;
que.push(now);
}
}
for(int i=;i<=C;i++)
scanf("%d",&c[i]);
sort(c+,c++C);
for(int i=;i<=D;i++){
scanf("%d",&d[i]);
v.x=d[i]+c[];v.y=;
q.push(v);
}
while(!q.empty()&&q.top().x<=n){
nond now=q.top();
num2[++cnt2]=now.x;
q.pop();
if(now.y+<=C){
now.x=now.x-c[now.y]+c[now.y+];
now.y+=;
q.push(now);
}
}
for(int i=;i<=cnt2;i++){
v.x=num2[i]+num1[];v.y=;
qu.push(v);
}
while(!qu.empty()&&qu.top().x<=n){
nond now=qu.top();
ans++;
qu.pop();
if(now.y+<=cnt1){
now.x=now.x-num1[now.y]+num1[now.y+];
now.y+=;
qu.push(now);
}
}
cout<<ans;
}

正解思路:分组背包。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 5005
#define M 25000005
#define N 100000005
using namespace std;
int A,B,C,D;
long long ans;
int n,cnt,cnt1,maxn;
int f[N],bns[M],cns[M];
int a[MAXN],b[MAXN],c[MAXN],d[MAXN];
int main(){
freopen("eat.in","r",stdin);
freopen("eat.out","w",stdout);
scanf("%d%d%d%d%d",&n,&A,&B,&C,&D);
for(int i=;i<=A;i++) scanf("%d",&a[i]);
for(int i=;i<=B;i++) scanf("%d",&b[i]);
maxn=;
for(int i=;i<=A;i++)
for(int j=;j<=B;j++)
if(a[i]+b[j]<=n){
f[a[i]+b[j]]++;
maxn=max(maxn,a[i]+b[j]);
}
for(int i=;i<=maxn;i++)
while(f[i]){
f[i]--;
bns[++cnt]=i;
}
for(int i=;i<=C;i++) scanf("%d",&c[i]);
for(int i=;i<=D;i++) scanf("%d",&d[i]);
maxn=;
for(int i=;i<=C;i++)
for(int j=;j<=D;j++)
if(c[i]+d[j]<=n){
f[c[i]+d[j]]++;
maxn=max(maxn,c[i]+d[j]);
}
for(int i=;i<=maxn;i++)
while(f[i]){
f[i]--;
cns[++cnt1]=i;
}
int now;
for(now=cnt1;now>=;now--)
if(bns[]+cns[now]<=n)
break;
for(int i=;i<=cnt;i++){
ans+=now;
while(now&&bns[i+]+cns[now]>n) now--;
}
cout<<ans;
}

分糖果(candy)

Time Limit:1000ms   Memory Limit:128MB

题目描述

总共有n颗糖果,有3个小朋友分别叫做L,Y,K。每个小朋友想拿到至少k颗糖果,但这三个小朋友有一个共同的特点:对3反感。也就是说,如果某个小朋友拿到3颗,13颗,31颗,333颗这样数量的糖果,他就会不开心。(也即它拿到的糖果数量不包含有一位是3)

LYK掌管着这n颗糖果,它想问你有多少种合理的分配方案使得将这n颗糖果全部分给小朋友且没有小朋友不开心。

例如当n=3,k=1时只有1种分配方案,当n=4,k=1时有3种分配方案分别是112,121,211。当n=7,k=2时则不存在任何一种合法的方案。

当然这个答案可能会很大,你只需输出答案对12345647取模后的结果就可以了。

输入格式(candy.in)

第一行两个数表示n,k。

输出格式(candy.out)

一个数表示方案总数。

输入样例

99999 1

输出样例

9521331

对于30%的数据n<=100

对于50%的数据n<=1000。

对于另外30%的数据k=1。

对于100%的数据3<=n<=10^10000,1<=k<=n/3,且n,k不包含前导0。

数位DP

dp[i][j][k][l][t] 表示n的前i位,分完后的余数为j,第1/2/3个小朋友的第i位能否随便填 的方案数

再枚举3个小朋友分别分多少转移

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 10003
#define mod 12345647
using namespace std;
char s[MAXN];
int a[MAXN],b[MAXN];
int dp[MAXN][][][][];
void ADD(int &i,int j){
i+=j;
if(i>=mod) i-=mod;
}
int main(){
freopen("candy.in","r",stdin);
freopen("candy.out","w",stdout);
scanf("%s",s+);
int len1=strlen(s+);
for(int i=;i<=len1;++i)
a[i]=s[i]-'';
scanf("%s",s+);
int len2=strlen(s+);
for(int i=;i<=len2;++i)
b[i+len1-len2]=s[i]-'';
dp[][][][][]=;
for(int i=;i<len1;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
for(int l=;l<;l++)
for(int t=;t<;t++)
if(dp[i][j][k][l][t])
for(int s1=;s1<=;s1++)
if(s1!=)
for(int s2=;s2<=;s2++)
if(s2!=)
for(int s3=;s3<=;s3++)
if(s3!=){
int I=i+;
int J=j*+a[i+]-s1-s2-s3;
if(J<||J>) continue;
if(!k&&s1<b[i+]) continue;
int K=(k||s1>b[i+]);
if(!l&&s2<b[i+]) continue;
int L=(l||s2>b[i+]);
if(!t&&s3<b[i+]) continue;
int T=(t||s3>b[i+]);
ADD(dp[I][J][K][L][T],dp[i][j][k][l][t]);
}
int ans=;
for(int k=;k<;k++)
for(int l=;l<;l++)
for(int t=;t<;t++)
ADD(ans,dp[len1][][k][l][t]);
printf("%d",ans);
}

国庆 day 2 下午的更多相关文章

  1. 国庆 day 7 下午

    思路:见博客. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

  2. 国庆 day 1 下午

    一道图论好题(graph) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图, ...

  3. 国庆 day 6 下午

    1.数组异或 (xorarray.pas/c/cpp) (xorarray.in/out) 时间限制:2s/空间限制:256M [题目描述] xor——异或,和 and 与or 一样,是一种重要的逻辑 ...

  4. 国庆 day 3 下午

    a[问题描述] 你是能看到第一题的 friends 呢. ——hja 给你一个只有小括号和中括号和大括号的括号序列,问该序列是否合法.[输入格式] 一行一个括号序列.[输出格式] 如果合法,输出 OK ...

  5. 2018国庆YALI集训游记

    想了想,像之前那样简略地叙述题意和做法,根本没讲清楚,没有任何意义,还不如写写自己的感受. 感觉YALI真的是一所挺不错的学校吧.总是能有一机房的julao轮番吊打你,总是能有集训队的奆佬来给你出dl ...

  6. 2016.10.4初中部下午NOIP普及组比赛总结

    2016.10.4初中部下午NOIP普及组比赛总结 这次的题有些水,只是第四题有点坑. 题目: 比赛:AC+0+AC+50=250 改题:AC+AC+AC+50=350 一.Bill 满地都是水 题目 ...

  7. 票房和口碑称霸国庆档,用 Python 爬取猫眼评论区看看电影《我和我的家乡》到底有多牛

    今年的国庆档电影市场的表现还是比较强势的,两名主力<我和我的家乡>和<姜子牙>起到了很好的带头作用. <姜子牙>首日破 2 亿,一举刷新由<哪吒之魔童降世&g ...

  8. 搞了我一下午竟然是web.config少写了一个点

    Safari手机版居然有个这么愚蠢的bug,浪费了我整个下午,使尽浑身解数,国内国外网站搜索解决方案,每一行代码读了又想想了又读如此不知道多少遍,想破脑袋也想不通到底哪里出了问题,结果竟然是web.c ...

  9. System.DateUtils 3. IsPM、IsAM 判断是否为上、下午

    编译版本:Delphi XE7 function IsPM(const AValue: TDateTime): Boolean; inline;function IsAM(const AValue: ...

随机推荐

  1. NOI 2018 归程 (Kruskal重构树)

    题目大意:太长了,略 Kruskal重构树,很神奇的一个算法吧 如果两个并查集被某种条件合并,那么这个条件作为一个新的节点连接两个并查集 那么在接下来的提问中,如果某个点合法,它的所有子节点也都合法, ...

  2. python中try…except的使用,处理程序异常

    通常情况下,在python中运行程序,多多少少会出现程序异常的问题,try……except能很好的解决程序中的异常.以下是其用法,在不同位置时进行什么样的工作和起到什么样的作用. try: 可能出现异 ...

  3. Mysql学习总结(33)——阿里云centos配置MySQL主从复制

    1.安装jdk1.8 首先确定没有安装过jdk 2.yum –y list java*查询系统自带的jdk安装包情况. 3.安装jdk1.8 4. 验证安装结果. 安装mysql 1. rpm -Uv ...

  4. 洛谷 P1825 [USACO11OPEN]玉米田迷宫Corn Maze

    P1825 [USACO11OPEN]玉米田迷宫Corn Maze 题目描述 This past fall, Farmer John took the cows to visit a corn maz ...

  5. [Google Guava] 2.2-新集合类型

    转自:并发编程网 原文链接:http://ifeve.com/google-guava-newcollectiontypes/ 链接博客其他文章中还有更多的guava其他功能的描述,有空可慢慢看. G ...

  6. [Angular] Provide Feedback to Progress Events with Angular’s HttpRequest Object

    In some cases your application might need to upload large amounts of data, such as files. Obviously ...

  7. Remove Duplicates from Sorted List II 解答(有个比較特殊的case leetcode OJ没有覆盖)

    昨天被考了一道数据结构题,当时的实现比較一般.回来翻看leetcode,果然是上面的题.遂解之. accept之后翻看discuss别人的解法.发现非常多能够accept的代码都过不了我设计的一个ca ...

  8. storm-安装

            storm有两种操作模式: 本地模式和远程模式.使用本地模式的时候,你能够在你的本地机器上开发測试你的topology, 一切都在你的本地机器上模拟出来; 用远端模式的时候你提交的to ...

  9. Mongodb简单介绍

    1. 简单介绍 Mongodb是一种强大.灵活,可扩展的数据存储方式.属于nosql.非关系型数据库的一种. mongodb是面向文档的数据库. 尽管是非关系型数据库.可是它保留了很多关系型数据库的特 ...

  10. 小贝_mysql三种子查询

    mysql三种子查询 简要: 一.三种子查询 二.三种子查询理解模型 一.mysql 三种子查询 where子查询.from子查询.exists子查询 二.理解模型: 2.1.一个好的模型,便于我们去 ...