头像

我是谁

c++


访客:3527

离线:4个月前


活动打卡代码 AcWing 436. 立体图

我是谁
7个月前
#include<cstdio>
#include<iostream>

using namespace std;

const char cube[6][8]=
{
    "..+---+",
    "./   /|",
    "+---+ |",
    "|   | +",
    "|   |/.",
    "+---+.."
};
int a[55][55];
char map[305][505];
void put(int x,int y)
{
    int i,j;
    for (i=0;i<6;i++)
     for (j=0;j<7;j++)
      if (cube[6-i-1][j]!='.') map[x-i][y+j]=cube[6-i-1][j]; 
}

int main()
{
    int n,m,l,w,i,j,x,y;
    scanf("%d%d",&n,&m);
    w=4*m+2*n+1;
    for (i=1;i<=n;i++)
     for (j=1;j<=m;j++)
     {
        scanf("%d",&a[i][j]);
        l=max(l,a[i][j]*3+2*(n-i+1)+1);
     }
    for (i=1;i<=l;i++)
     for (j=1;j<=w;j++)
      map[i][j]='.';
    for (i=1;i<=n;i++)    
     for (j=1;j<=m;j++)
     {
        x=l-2*(n-i);
        y=2*(n-i)+4*(j-1)+1;
        while (a[i][j]--)
        {
            put(x,y); 
            x-=3;
        }
     }
    for (i=1;i<=l;i++)
    {
        for (j=1;j<=w;j++) printf("%c",map[i][j]);
        printf("\n");
    }
    return 0;
}


活动打卡代码 AcWing 445. 数字反转

我是谁
7个月前
#include<iostream>
using namespace std;
int main(){
    bool fu=false,done0=false;
    string a;
    cin>>a;
    if(a[0]=='-'){
        fu=true;
        cout << a[0];
    }
    for(int i=a.size()-1;i>=(fu?1:0);i--){
        if(!done0&&a[i]=='0'){
            continue;
        }else{
            done0=true;
            cout<<a[i];
        }
    }
    return 0;
}



活动打卡代码 AcWing 830. 单调栈

我是谁
8个月前
#include <iostream>

using namespace std;

const int N = 100010;

int n;
int a[N];
int stk[N], top;

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ ) cin >> a[i];

    for (int i = 0; i < n; i ++ )
    {
        while (top > 0 && stk[top] >= a[i]) top -- ;
        if (top > 0) printf("%d ", stk[top]);
        else printf("-1 ");
        stk[ ++ top] = a[i];
    }

    return 0;
}



活动打卡代码 AcWing 154. 滑动窗口

我是谁
8个月前
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],n,m,f,i,j,s_min[N],s_max[N];
deque<int> min_val,max_val;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    min_val.push_front(1);
    max_val.push_front(1);
    s_min[1]=1;
    s_max[1]=1;
    m--;
    for(int i=2;i<=n;i++)
    {
        while (min_val.size() && min_val.front()+m<i)
            min_val.pop_front();
        while (max_val.size() && max_val.front()+m<i)
            max_val.pop_front();

        while (min_val.size() && a[min_val.back()]>=a[i])
            min_val.pop_back();
        while (max_val.size() && a[max_val.back()]<=a[i])
            max_val.pop_back();

            min_val.push_back(i);

            max_val.push_back(i);

        s_min[i]=min_val.front();
        s_max[i]=max_val.front();
    }
    for(int i=m+1;i<=n;i++)
        cout<<a[s_min[i]]<<" ";
    cout<<endl;
    for(int i=m+1;i<=n;i++)
        cout<<a[s_max[i]]<<" ";
    return 0;

}


活动打卡代码 AcWing 154. 滑动窗口

我是谁
8个月前
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N],n,m,f,i,j,s_min[N],s_max[N];
deque<int> min_val,max_val;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    min_val.push_front(1);
    max_val.push_front(1);
    s_min[1]=1;
    s_max[1]=1;
    m--;
    for(int i=2;i<=n;i++)
    {
        while (min_val.size() && min_val.front()+m<i)
            min_val.pop_front();
        while (max_val.size() && max_val.front()+m<i)
            max_val.pop_front();

        while (min_val.size() && a[min_val.back()]>=a[i])
            min_val.pop_back();
        while (max_val.size() && a[max_val.back()]<=a[i])
            max_val.pop_back();

            min_val.push_back(i);

            max_val.push_back(i);

        s_min[i]=min_val.front();
        s_max[i]=max_val.front();
    }
    for(int i=m+1;i<=n;i++)
        cout<<a[s_min[i]]<<" ";
    cout<<endl;
    for(int i=m+1;i<=n;i++)
        cout<<a[s_max[i]]<<" ";
    return 0;

}


活动打卡代码 AcWing 827. 双链表

我是谁
10个月前
#include<cstdio>
#include<iostream>

using namespace std;

const int maxn=100005;

struct node{
    int pre,nxt,data;   //前驱和后继
}a[maxn];
int head,tot,rear;//head和rear指向头元素和尾元素

void L(int x)
{
    a[++tot].data=x;
    a[head].pre=tot;
    a[tot].nxt=head;
    head=tot;
    if(!rear) rear=tot;
}

void R(int x)
{
    a[++tot].data=x;
    a[rear].nxt=tot;
    a[tot].pre=rear;
    rear=tot;
    if(!head) head=tot;
}

void D(int k)
{
    if(head==k) head=a[k].nxt;
    if(rear==k) rear=a[k].pre;
    int ll=a[k].pre;
    int rr=a[k].nxt;
    a[ll].nxt=rr;
    a[rr].pre=ll;
}

void IL(int k,int x)
{
    a[++tot].data=x;
    a[tot].pre=a[k].pre;
    a[tot].nxt=k;
    a[a[k].pre].nxt=tot;
    a[k].pre=tot;
    if(head==a[tot].nxt) head=tot;
}

void IR(int k,int x)
{
    a[++tot].data=x;
    a[tot].nxt=a[k].nxt;
    a[tot].pre=k;
    a[a[k].nxt].pre=tot;
    a[k].nxt=tot;
    if(rear==a[tot].pre) rear=tot;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        char s[5];
        scanf("%s",s);
        if(s[0]=='L'){
            int x;
            scanf("%d",&x);
            L(x);
        }else if(s[0]=='R'){
            int x;
            scanf("%d",&x);
            R(x);
        }else if(s[0]=='D'){
            int k;
            scanf("%d",&k);
            D(k);
        }else if(s[1]=='L'){
            int k,x;
            scanf("%d%d",&k,&x);
            IL(k,x);
        }else{
            int k,x;
            scanf("%d%d",&k,&x);
            IR(k,x);
        }
    }
    int t=head;
    while(t)
    {
        printf("%d ",a[t].data);
        t=a[t].nxt;
    }
    cout<<endl;
    return 0;
}




活动打卡代码 AcWing 829. 模拟队列

我是谁
10个月前
#include <iostream>

using namespace std;

const int N = 100010;

int m;
int q[N], hh, tt = -1;

int main()
{
    cin >> m;

    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q[ ++ tt] = x;
        }
        else if (op == "pop") hh ++ ;
        else if (op == "empty") cout << (hh <= tt ? "NO" : "YES") << endl;
        else cout << q[hh] << endl;
    }

    return 0;
}


活动打卡代码 AcWing 828. 模拟栈

我是谁
10个月前
#include <iostream>

using namespace std;

const int N = 100010;

int m;
int stk[N], tt;

int main()
{
    cin >> m;
    while (m -- )
    {
        string op;
        int x;

        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[ ++ tt] = x;
        }
        else if (op == "pop") tt -- ;
        else if (op == "empty")
        {
            if (tt) puts("NO");
            else puts("YES");
        }
        else cout << stk[tt] << endl;
    }

    return 0;
}


活动打卡代码 AcWing 826. 单链表

我是谁
10个月前

```#include[HTML_REMOVED]

using namespace std;
const int N = 1e5 + 10;

int ne[N], e[N], idx;

void init() {
ne[0] = -1, idx = 1;
}

void add(int k, int x) {
e[idx] = x, ne[idx] = ne[k], ne[k] = idx ++;
}

void remove(int k){
ne[k] = ne[ne[k]];
}

int main(){
int m;
scanf(“%d”, &m);

init(); 
while(m --){ 
    string op;
    cin >> op;

    int k, x;       
    if(op == "H"){
        cin >> x;
        add(0, x);  
    }
    else if(op == "I"){
        cin >> k >> x;
        add(k, x);  
    }
    else {
        cin >> k;
        remove(k);  
    }
}

for(int i = ne[0]; i != -1; i = ne[i]) printf("%d ", e[i]);  
return 0;

}

```



题目讨论 AcWing 430. 430

我是谁
10个月前
#include<bits/stdc++.h>
using namespace std;
int W,ans=0;
int n,a[30001];
int l,r,i;
int main()
{
    scanf("%d%d",&W,&n);
    for(i=1;i<=n;i++)
      scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    l=1;  r=n;
    while(l<=r)
    {
        if(a[l]+a[r]<=W)   
          l++,r--,ans++;
        else
          r--,ans++;   
    }
    printf("%d",ans);
    return 0;
}