头像

老实人_6




离线:21天前


最近来访(4)
用户头像
itdef
用户头像
acwing520
用户头像
天天睡不醒
用户头像
种花家的兔兔


老实人_6
2022-01-18 14:58

题目链接 https://www.acwing.com/problem/content/841/

错误的代码:

#include<iostream>
#include<string.h>

using namespace std;

const int N=1e5+10;

int h[N],ph[N],hp[N],cnt;

void heap_swap(int a,int b)
{
   swap( ph[hp[a]],ph[hp[b]]);
   swap(hp[a],hp[b]);
   swap(h[a],h[b]);
}

void down(int x)
{
    int t=x;
    if(2*x<=cnt && h[2*x]<h[t])t=2*x;
    if(2*x+1<=cnt && h[2*x+1]<h[t])t=2*x+1;
    if(x!=t)
    {
        heap_swap(t,x);
        down(t);
    }
}

void up(int x)
{
    if(x/2&&h[x/2]>h[x])
    heap_swap(x,x/2);
    x/=2;
}
int main()
{
    int n,x,m=0;
    while(n--)
    {
        char op[5];
        scanf("%s",op);

        if(!strcmp(op,"I"))
        {
            cin>>x;
            cnt++;m++;
            ph[m]=cnt;hp[cnt]=m;
            h[cnt]=x;up(cnt);
        }
        else if(!strcmp(op,"PM"))
        {
            printf("%d\n",h[1]);
        }
        else if(!strcmp(op,"DM"))
        {
            heap_swap(1,cnt--);down(1);
        }
        else if(!strcmp(op,"D"))
        {
            int k;
            cin>>k;
            heap_swap(ph[k],cnt--);down(ph[k]);up(ph[k]);
        }
        else 
        {
            int k,x;
            cin>>k>>x;
            h[hp[k]]=x;down(ph[k]);up(ph[k]);
        }
    }
    return 0;
}

为什么会运行超时




老实人_6
2022-01-15 12:20

题目链接 https://www.acwing.com/problem/content/submission/code_detail/9934433/

错误的代码:

#include<iostream>
#include<vector>
using namespace std;

bool cmp(vector<int> &A,vector<int> &B)
{
    if(A.size()!=B.size())return A.size()>B.size();

    for(int i=A.size()-1;i>=0;i--)
    {
        if(A[i]!=B[i])return A[i]>B[i];
    }
    return true;
}
vector<int> sub(vector<int> A,vector<int> B)
{
    vector<int> C;
    for(int i=0,t=0;i<A.size();i++)
    {
        /*t=A[i]-t;
        if(i<B.size())t-=B[i];
        C.push_back((t+10)%10);
        if(t<0)t=1;
        else t=0;*/
        if(A[i]>=B[i])C.push_back(A[i]-B[i]);
        else 
        {
            C.push_back(A[i]+10-B[i]);
            A[i+1]--;
        }

    }
    while(C.size()>1&&C.back()==0)C.pop_back();

    return C;
}
int main()
{
    string a,b;

    vector<int> A,B;

    cin>>a>>b;

    for(int i=a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
    for(int i=b.size()-1;i>=0;i--)B.push_back(b[i]-'0');

    if(cmp(A,B)){
    auto C=sub(A,B);

    for(int i=C.size()-1;i>=0;i--)cout<<C[i];}

    else 
    {
        auto C=sub(B,A);
        printf("-");
        for(int i=C.size()-1;i>=0;i--)cout<<C[i];
    }

    cout<<endl;

    return 0;
}

就把注释部分改成了下面的if else,感觉思路是一样的



活动打卡代码 AcWing 2058. 笨拙的手指

老实人_6
2022-01-11 22:03
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

int get(string s, int b)  
{
    int res = 0;
    for (auto c: s)
        res = res * b + c - '0';
    return res;
}

int main()
{
    string a, b;
    cin >> a >> b;

    unordered_set<int> S;

    for (auto& c: a)
    {
        c ^= 1;
        S.insert(get(a, 2));
        c ^= 1;
    }

    for (auto& c: b)
    {
        char t = c;
        for (int i = 0; i < 3; i ++ )
            if (i + '0' != t)
            {
                c = i + '0';
                int x = get(b, 3);
                if (S.count(x))
                {
                    cout << x << endl;
                    return 0;
                }
            }
        c = t;
    }

    return 0;
}


活动打卡代码 AcWing 785. 快速排序

老实人_6
2022-01-11 22:00
#include<iostream>
using namespace std;
const int N=1e6+10;
int q[N],n;
void sort(int q[],int l,int r){
    if(l>=r)return;
    int i=l-1,j=r+1,x=q[(l+r)/2];
    while(i<j){
        do i++ ;while(q[i]<x);
        do j--;while(q[j]>x);
        if(i<j){
            int t=q[i];q[i]=q[j];q[j]=t;
        }

    }   sort(q,l,j);sort(q,j+1,r);
}
int main()
{   
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    scanf("%d",&q[i]);
    sort(q,0,n-1);
    for(int i=0;i<n;i++)
    printf("%d ",q[i]);
      return 0;
    }