JZOJ 1619. 音乐节拍

目录:

题目:

单击看题目


分析:

作者为了迎合第一题的难度,所以将数据大小限制得十分友善,故此,我们可以暴力模拟直接AC(离线算法),也可以使用前缀和+二分(在线算法)


代码(离线):

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
inline LL read() {
    LL d=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){d=d*10+s-'0';s=getchar();}
    return d*f;
}
int x[50001];
struct node{
    int a,b;
}y[50001];
bool cmd(node w,node v)
{
    return w.a<v.a; 
}
int ans[50001];
int main()
{
    freopen("mnotes.in","r",stdin);
    freopen("mnotes.out","w",stdout);
    int n=read(),q=read();
    int music=0;
    for(int i=1;i<=n;i++) x[i]=read();
    for(int i=1;i<=q;i++)
      y[i].a=read(),y[i].b=i;
    sort(y+1,y+1+q,cmd);
    int k=1;
    for(int i=1;i<=n;i++)
    {
        music+=x[i]-1;//music用来确定每个音节的范围
        while(y[k].a<=music&&k<=q)
          {
            ans[y[k].b]=i;
            k++;
          }
        music++;
    }
    for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
    fclose(stdin);
    fclose(stdout);
    return 0;
}

代码(在线):

#include <cstdio>
#include <cctype>
#include <algorithm>
#define us unsigned short
using namespace std;
struct rec{int x; us y;}b[50005]; us n,q,t=1,a[50005],ans[50005]; int tosic=-1;
int in(){
    int ans=0; char c=getchar();
    while (!isdigit(c)) c=getchar();
    while (isdigit(c)) ans=ans*10+c-48,c=getchar();
    return ans;
}
bool cmp(rec x,rec y){return x.x<y.x;}
int main(){
    freopen("mnotes.in","r",stdin);
    freopen("mnotes.out","w",stdout);
    n=in(); q=in();
    for (int i=1;i<=n;i++) a[i]=in();
    for (int i=1;i<=q;i++) b[i]=(rec){in(),i};
    stable_sort(b+1,b+1+q,cmp);
    for (int i=1;i<=n;i++){
        tosic+=a[i];
        while (b[t].x<=tosic&&t<=q) ans[b[t++].y]=i;
    }
    for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
    return 0; 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值