文章
1
粉丝
0
获赞
0
访问
96
(1) 根据题目可知,因为相同元素一定相邻,单独的元素会占一个位置,总个数是奇数。所以显而易见:如果偶数位与其后面的数相等,说明仅出现一次的元素在后面;如果奇数位与其后面的数相等,则说明仅出现一次的元素在前面。因为答案具有单调性。所以,可以使用二分进行查找。
(2)
#include <iostream>
using namespace std;
const int N=1e5+9;
typedef long long LL;
bool check(int x,int a[]){
return a[x] == a[x + 1];
}
void solve(){
int n;
cin >> n;
int a[n];
for(int i = 0; i < n; i++) cin >> a[i];
int left = -1, right = n;
while(left + 1 < right){
int mid = left + ((right - left) >> 1);
if(mid % 2 == 1) mid --;
if(check(mid,a)) left = mid + 1;
else right = mid;
}
cout << a[right];
}
int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
while(t--){
solve();
}
return 0;
}
(3) O(log n)(n 是数组长度)
评分及理由
(1)得分及理由(满分3分)
得分:3分
理由:学生的设计思想正确,虽然与标准答案的线性遍历不同,但提出了二分查找的思路,利用了题目中“相同元素一定相邻”的特性,逻辑正确且高效。
(2)得分及理由(满分8分)
得分:7分
理由:学生的代码实现基本正确,使用了二分查找的方法,但在边界条件的处理上存在一些问题。例如,`left`初始化为-1可能导...
登录后发布评论
暂无评论,来抢沙发