CFgym: Laying Cables(单调栈)


题意:给出N个村庄,第i个村庄编号为i,坐标为xi,人数为p[i],要求输出每个村庄离自己最近的,人数比自己多的村庄编号,若有一样,输出人数较多的那个。

思路:用单调栈处理出每个村庄左边和右边最近的,比自己人多的村庄编号即可。

# include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5+3;
struct node
{
    int p, v, id;
    bool operator < (const node &a)const
    {
        return p < a.p;
    }
}a[maxn];
int l[maxn], r[maxn], que[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; ++i)
        scanf("%d%d",&a[i].p, &a[i].v), a[i].id = i;
    sort(a+1, a+n+1);
    memset(que, 0, sizeof(que));
    int h=0, t=0;
    for(int i=1; i<=n; ++i)
    {
        int tmp = a[i].v;
        while(t>h && a[que[t]].v<=tmp) --t;
        if(t>h) l[i]=que[t];
        else l[i]=-1;
        que[++t] = i;
    }
    memset(que, 0, sizeof(que));
    h = t = 0;
    for(int i=n; i>=1; --i)
    {
        int tmp = a[i].v;
        while(t>h && a[que[t]].v<=tmp) --t;
        if(t>h) r[i]=que[t];
        else r[i]=-1;
        que[++t] = i;
    }
    for(int i=1; i<=n; ++i)
    {
        if(l[i]==-1 && r[i]==-1) que[a[i].id] = -1;
        else if(l[i]==-1) que[a[i].id] = a[r[i]].id;
        else if(r[i]==-1) que[a[i].id] = a[l[i]].id;
        else if(a[i].p-a[l[i]].p>a[r[i]].p-a[i].p) que[a[i].id] = a[r[i]].id;
        else if(a[i].p-a[l[i]].p<a[r[i]].p-a[i].p) que[a[i].id] = a[l[i]].id;
        else if(a[l[i]].v > a[r[i]].v) que[a[i].id] = a[l[i]].id;
        else que[a[i].id] = a[r[i]].id;
    }
    for(int i=1; i<=n; ++i)
        printf("%d ",que[i]);
    puts("");
    return 0;
}


io-3.35 Starting 8 processes randread: Laying out IO file (1 file / 10240MiB) randread: Laying out IO file (1 file / 10240MiB) randread: Laying out IO file (1 file / 10240MiB) fio: ENOSPC on laying out file, stopping fio: pid=0, err=28/file:filesetup.c:240, func=write, error=No space left on device randread: Laying out IO file (1 file / 10240MiB) fio: pid=0, err=28/file:filesetup.c:174, func=open, error=No space left on device randwrite: Laying out IO file (1 file / 10240MiB) fio: pid=0, err=28/file:filesetup.c:174, func=open, error=No space left on device randwrite: Laying out IO file (1 file / 10240MiB) fio: pid=0, err=28/file:filesetup.c:174, func=open, error=No space left on device randwrite: Laying out IO file (1 file / 10240MiB) fio: pid=0, err=28/file:filesetup.c:174, func=open, error=No space left on device randwrite: Laying out IO file (1 file / 10240MiB) fio: pid=0, err=28/file:filesetup.c:174, func=open, error=No space left on device Jobs: 2 (f=2): [r(2),X(6)][100.0%][r=279KiB/s][r=69 IOPS][eta 00m:00s] randread: (groupid=0, jobs=1): err= 0: pid=4012: Wed Jul 16 22:21:23 2025 read: IOPS=36, BW=144KiB/s (148kB/s)(8668KiB/60032msec) slat (nsec): min=1960, max=423031k, avg=27609589.07, stdev=25336672.45 clat (msec): min=9, max=8015, avg=411.63, stdev=396.00 lat (msec): min=35, max=8015, avg=439.24, stdev=395.86 clat percentiles (msec): | 1.00th=[ 89], 5.00th=[ 262], 10.00th=[ 288], 20.00th=[ 309], | 30.00th=[ 330], 40.00th=[ 347], 50.00th=[ 359], 60.00th=[ 380], | 70.00th=[ 418], 80.00th=[ 468], 90.00th=[ 535], 95.00th=[ 625], | 99.00th=[ 768], 99.50th=[ 1921], 99.90th=[ 7684], 99.95th=[ 7752], | 99.99th=[ 8020] bw ( KiB/s): min= 24, max= 211, per=49.95%, avg=144.88, stdev=31.58, samples=114 iops : min= 6, max= 52, avg=35.96, stdev= 7.92, samples=114 lat (msec) : 10=0.05%, 20=0.14%, 50=0.37%, 100=0.65%, 250=2.86% lat (msec) : 500=81.50%, 750=13.15%, 1000=0.60%, 2000=0.23%, >=2000=0.46% cpu : usr=0.32%, sys=99.26%, ctx=735, majf=0, minf=26 IO depths : 1=0.1%, 2=0.1%, 4=0.2%, 8=0.4%, 16=99.3%, 32=0.0%, >=64=0.0% submit : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0% complete : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.1%, 32=0.0%, 64=0.0%, >=64=0.0% issued rwts: total=2167,0,0,0 short=0,0,0,0 dropped=0,0,0,0 latency : target=0, window=0, percentile=100.00%, depth=16 randread: (groupid=0, jobs=1): err= 0: pid=4013: Wed Jul 16 22:21:23 2025 read: IOPS=36, BW=144KiB/s (148kB/s)(8680KiB/60180msec) slat (nsec): min=1979, max=438546k, avg=27562216.01, stdev=25336850.71 clat (msec): min=6, max=8611, avg=411.96, stdev=584.57 lat (msec): min=33, max=8632, avg=439.52, stdev=583.69 clat percentiles (msec): | 1.00th=[ 128], 5.00th=[ 236], 10.00th=[ 257], 20.00th=[ 284], | 30.00th=[ 305], 40.00th=[ 321], 50.00th=[ 342], 60.00th=[ 368], | 70.00th=[ 393], 80.00th=[ 435], 90.00th=[ 502], 95.00th=[ 575], | 99.00th=[ 1636], 99.50th=[ 7013], 99.90th=[ 8423], 99.95th=[ 8490], | 99.99th=[ 8658] bw ( KiB/s): min= 32, max= 247, per=49.95%, avg=144.90, stdev=32.09, samples=115 iops : min= 8, max= 61, avg=36.01, stdev= 8.06, samples=115 lat (msec) : 10=0.09%, 20=0.05%, 50=0.37%, 100=0.28%, 250=7.00% lat (msec) : 500=81.94%, 750=8.39%, 1000=0.74%, 2000=0.32%, >=2000=0.83% cpu : usr=0.36%, sys=98.93%, ctx=813, majf=0, minf=27 IO depths : 1=0.1%, 2=0.1%, 4=0.2%, 8=0.4%, 16=99.3%, 32=0.0%, >=64=0.0% submit : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0% complete : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.1%, 32=0.0%, 64=0.0%, >=64=0.0% issued rwts: total=2170,0,0,0 short=0,0,0,0 dropped=0,0,0,0 latency : target=0, window=0, percentile=100.00%, depth=16 Run status group 0 (all jobs): READ: bw=288KiB/s (295kB/s), 144KiB/s-144KiB/s (148kB/s-148kB/s), io=16.9MiB (17.8MB), run=60032-60180msec Disk stats (read/write): dm-0: ios=4336/6, merge=0/0, ticks=469085/1169, in_queue=470254, util=100.00%, aggrios=4337/5, aggrmerge=0/1, aggrticks=477332/1114, aggrin_queue=478446, aggrutil=79.54% sda: ios=4337/5, merge=0/1, ticks=477332/1114, in_queue=478446, util=79.54%
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值