总结

· · 个人记录

考试情况

题目编号(点击编号可跳转) 模拟赛分数
\color{black}{T1} 100
\color{black}{T2} 0
[\color{black}{T3}]() 0
[\color{black}{T4}]() 33
[\color{black}{T5}]() \color{red}{NULL}
[\color{black}{T6}]() \color{red}{NULL}
[\color{black}{T7}]() \color{red}{NULL}

题目总结

T1

正确思路

首先我们可以知道,一个奇数必须减去2^0才可以继续,而且负数也不是优秀的拆分,所以可以排除这些,接着秩序每次将n减去2^{log_2(n)}即可

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    if(n%2==1||n<=0){//判断不合理条件
        cout << -1;return 0;
    }
    while(n>0){//一个一个得出答案
        cout << (1<<int(log2(n))) << ' ';//输出2^log2(n)
        n-=(1<<int(log2(n)));//减少
    }

    return 0;
}

T2

错误原因

把题目看错

错误代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long n,a[1000000],maxn=INT_MIN,minn=INT_MAX;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(long long i = 1;i<=n;i++){
        cin >> a[i];
        maxn=max(maxn,a[i]);
        minn=min(minn,a[i]);
    }
    cout << maxn-minn;  
    return 0;
}

错误出在

for(long long i = 1;i<=n;i++){
        cin >> a[i];
        maxn=max(maxn,a[i]);
        minn=min(minn,a[i]);
    }

的 minn=min(minn,a[i]); 处。

正确思路

枚举得出最小

正确代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[100100];
int n;
int ans = INT_MAX;//输出的答案 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1;i<=n;i++)//录入 
        cin >> a[i];
    for(int i = 1;i<n;i++){//统计 
        ans = min(ans,max(a[i],a[i+1])-min(a[i],a[i+1]));//暴力得出最小字串值 
    }
    cout << ans;//输出 
}

T3

错误原因

时间

错误代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,t,a[10000000];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n >> t;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    while ( t -- ) {
        int x;
        cin >> x;
        int l = 0,r=n+1;
        a[l]=-1e9;a[r]=1e9;
        while(l+1<r){
            int mid=l+r>>1;
            if(a[mid]>x)r=mid;
            else l=mid;
        }
        if(r==n+1) cout << -1 << ' ';
        else cout << r << ' '; 
        l = 0,r=n+1;
        a[l]=-1e9;a[r]=1e9;
        while(l+1<r){
            int mid=l+r>>1;
            if(a[mid]>=x)r=mid;
            else l=mid;
        }
        if(l==0) cout << -1 << endl;
        else cout << l << endl;
    }
    return 0;
}

正确思路

用upper_bound和lower_bound

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,t,a[10000000];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n >> t;
    for(int i = 1;i<=n;i++){
        cin >> a[i];
    }
    sort(a+1,a+1+n);
    while ( t -- ) {
        int x;
        cin >> x;
        int pos=upper_bound(a+1,a+1+n,x)-a;//二分
        if(pos==n+1) cout << -1;
        else cout << a[pos];
        cout << ' ';
        pos=lower_bound(a+1,a+1+n,x)-a;
        if(pos==1) cout << -1;
        else cout << a[pos-1];
        cout << '\n';
    }
    return 0;
}

T4

错误原因

太暴力这孩子有暴力倾向

错误代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,a[1000006];
map<int,int> m;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i = 1;i<=n;i++) {cin >> a[i];m[a[i]]++;}
    for(int i = 1;i<=n;i++){
        if(m[a[i]]>n/2) {
            cout << a[i];
            return 0;
        }
    }
    return 0;
}

正确思路

使用摩尔投票

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,a[1000006];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    int cnt=0,x;
    for(int i = 1;i<=n;i++){
        int v;
        cin >> v;//输入
        if(x==v) {//相同
            cnt++;
        }
        else if(cnt==0){//没有数
            x=v;cnt++;
        }
        else if(x!=v)cnt--;//不同
    }
    cout << x;
    return 0;
}

T5

错误原因

???

错误代码

代码散落在了庄园里,快去寻找吧~

错误出在

代码散落在了庄园里,快去寻找吧~

的 代码散落在了庄园里,快去寻找吧~ 处。

正确思路

直接二分

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
long long a,b;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> a >> b;
    long long l=0,r=1e5+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if((long long)mid*(mid+1)>=2*(a+b))r=mid;//二分
        else l=mid;
    }
    if(r*(r+1)==2*(a+b)){
        cout << r << ' ';
        for(int i = r;i>=1;i--){
            if(a>=i){
                cout << i;
                a-=i;
                if(a!=0) cout << ' ';
            }
        }
    }else {
        cout << "No";
    }
    return 0;
}

T6

错误原因

代码散落在了庄园里,快去寻找吧~

错误代码

代码散落在了庄园里,快去寻找吧~

错误出在

代码散落在了庄园里,快去寻找吧~

的 代码散落在了庄园里,快去寻找吧~ 处。

正确思路

二分r

正确代码

#include<bits/stdc++.h>
using namespace std;
long long n, m, a[100005], b[100005], ans;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for(int i = 1;i<=n;i++){
        cin >> a[i];
    }
    sort(a+1,a+n+1);
    b[0] = -9e18;
    for (int i = 1;i<=m;i++){
        cin >> b[i];
    }
    sort(b+1,b+m+1);
    b[m+1] = 9e18;
    for(int i = 1;i<=n;i++){
        long long p = lower_bound(b+1,b+m+1,a[i]) - b;//二分
        ans = max(ans,min(a[i]-b[p-1],b[p]-a[i]));
    }
    cout << ans;
    return 0;
}