求调 全wa最后一个tle 二分

P2249 【深基13.例1】查找

@[ive_wonyoung](/user/1043012) 如果你要二分的话,首先你要保证你二分的序列是单调的。你没有进行排序就二分是没有意义的。
by DAhhr76 @ 2024-04-14 23:09:02


@[ive_wonyoung](/user/1043012) 你可以开个结构体保存输入的每个元素的下标即该下标位置的权值,然后重载一下,排序后再二分。
by DAhhr76 @ 2024-04-14 23:11:24


@[DAhhr76](/user/1073341) 啊啊因为这道题给的是 - 输入 $n$ 个不超过 $10^9$ 的单调不减的(就是后面的数字不小于前面的数字)非负整数 $a_1,a_2,\dots,a_{n}$, 所以我感觉好像不用排序》》
by ive_wonyoung @ 2024-04-14 23:31:23


应该是while(l<=r)吧
by smallpeter @ 2024-04-15 07:49:49


@[ive_wonyoung](/user/1043012) mid=(l+r)/2吧
by hayoon @ 2024-04-15 10:36:25


@[DAhhr76](/user/1073341) 初始值稍微改一下,二分的check也稍微改一下就行了,主要是你这个马蜂不太方便改QwQ ```cpp #include<bits/stdc++.h> using namespace std; #define int long long int n,m,a[1000003],r,l,mid; signed main() { cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; while(m--) { int s; cin>>s; r=n,l=1; int flag=0; while(l<r) { mid=l+r>>1; if(a[mid]>=s) r=mid; else if(a[mid]<s) l=mid+1; } if(a[l]==s) { cout<<l<<" "; } else { cout<<-1<<" "; } } return 0; } ``` [ac](https://www.luogu.com.cn/record/155916524)
by LzxQwQ @ 2024-04-15 15:53:32


@[DAhhr76](/user/1073341) 然后你wa的大概原因一个是-1后面没有空格。tle是因为那个while,如果数据全是同一个数,你的二分相当于是一个暴力
by LzxQwQ @ 2024-04-15 16:01:53


@[LzxQwQ](/user/678073) 谢谢啊啊啊
by ive_wonyoung @ 2024-04-15 21:02:50


@[LzxQwQ](/user/678073) ?
by DAhhr76 @ 2024-04-15 21:26:57


@[DAhhr76](/user/1073341) 他的-1后面没有输出空格,输出了有40分,其他的就纯属二分的问题了
by LzxQwQ @ 2024-04-16 14:08:46


| 下一页