头像

abinabin2dd




离线:3天前


最近来访(0)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre = NULL;//前一个
        ListNode *cur = head;//当前
        while(cur)
        {
            ListNode *tmp = cur->next;//记下当前的next
            cur->next = pre; //反转
            pre = cur,cur = tmp;//同时后移
        }
        return pre;
    }
};


活动打卡代码 AcWing 1855. 愤怒的奶牛

abinabin2dd
5个月前
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105;
int a[N], n;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    sort(a + 1, a + n + 1);

    int ans = 1;
    for (int i = 1; i <= n; ++i)
    {
        int cur = 1, step = 0;  
        int last = a[i]; 
        for (int j = i + 1; j <= n; ++j)
            if (a[j] - last <= step) cur++;
            else 
            {
                step++;
                last = a[j - 1];
                if (a[j] - last > step) break;
                else cur++;
            }
        step = 0;
        last = a[i];
        for (int j = i - 1; j >= 1; --j)
            if (last - a[j] <= step) cur++;
            else 
            {
                step++;
                last = a[j + 1];
                if (last - a[j] > step) break;
                else cur++;
            }
        ans = max(ans, cur);
    }
    cout << ans << endl;
    return 0;
}



活动打卡代码 AcWing 2041. 干草堆

abinabin2dd
5个月前

差分数组

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 1000010;
int b[N];

int main()
{
    int n,k;
    cin >> n >> k;
    while( k-- )
    {
        int l,r;
        cin >> l >>r;
        b[l]++,b[r+1]--;
    }
    for (int i = 1; i <= n; i ++ )b[i]+=b[i-1];
    sort(b+1,b+n+1);
    cout << b[(n+2)>>1] <<endl;
    return 0;
}


活动打卡代码 AcWing 843. n-皇后问题

abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 20;
bool col[N],gh[N],ugh[N];
int n;
char q[N][N];
void dfs(int u)//row
{
    if(u == n)
    {
        for (int i = 0; i < n; i ++ )puts(q[i]);
        puts("");
        return;
    }

    for (int i = 0; i < n; i ++ )
    {
        if(!col[i]&&!gh[u+i]&&!ugh[n-u+i])
        {
            q[u][i]='Q';
            col[i] = gh[u+i] =ugh[n-u+i] = true;
            dfs(u+1);
            col[i] = gh[u+i] =ugh[n-u+i] = false;
            q[u][i] = '.';
        }
    }
}

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
            q[i][j] = '.';
    dfs(0);
    return 0;
}



abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int lowbit(int x)  // 返回末尾的1
{
    return x & -x;
}

int main()
{
    int n;
    cin >> n;
    while (n -- )
    {
        int x;
        cin >> x;
        int ans = 0;
        while(x)
        {
            x = x-lowbit(x);
            ans++;
        }  
        cout << ans<<" ";
    }



    return 0;
}


活动打卡代码 AcWing 842. 排列数字

abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 100010;
int n;
int path[N];
bool st[N];
void dfs(int u)
{
    if(u == n)
    {
        for (int i = 0; i < n; i ++ ) printf("%d ",path[i]);
        puts("");
        return;
    }

    for (int i = 1; i <= n; i ++ )
    {
        if(!st[i])
        {
            path[u] = i;
            st[i] = true;
            dfs(u + 1);
            st[i] = false;
        }
    }

}

int main()
{
    cin >> n;
    dfs(0);
    return 0;
}


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

abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
int head,idx;
int e[N],ne[N];

void init()
{
    head = -1;
    idx = 0;
}

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

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

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

int main()
{
    int m;
    cin >> m;
    init();
    while (m -- )
    {
        char op;
        int k,x;
        cin >> op;
        if(op == 'H')
        {
            cin >> x;
            add(x);
        }else if(op == 'I')
        {
            cin >> k >> x;
            insert(k-1,x);
        }else
        {
            cin >> k;
            if(!k)head = ne[head];
            else remove(k-1);
        }
    }
    for (int i = head; i != -1; i = ne[i] )cout<<e[i]<<" ";
    return 0;
}


活动打卡代码 AcWing 2816. 判断子序列

abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
int a[N],b[N];

int main()
{
    int n,m;
    cin >> n >> m;
    for (int i = 0; i < n; i ++ )cin>>a[i];
    for (int i = 0; i < m; i ++ )cin>>b[i];
    int i = 0;
    int j = 0;
    while(i<n&&j<m)
    {
        if(a[i] == b[j])i++;
        j++;
    }
    if(i == n)puts("Yes");
    else puts("No");
    return 0;
}




abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
int a[N],b[N];

int main()
{
    int n,m,x;
    cin >> n >> m >> x;
    for (int i = 0; i < n; i ++ )cin>>a[i];
    for (int i = 0; i < m; i ++ )cin>>b[i];
    for (int i = 0,j=m-1; i < n; i ++ )
    {
        while(a[i]+b[j]>x)j--;
        if(a[i]+b[j]==x)cout<<i<<" "<<j<<endl;
    }
    return 0;
}



abinabin2dd
6个月前
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 100010;
int a[N],s[N];

int main()
{
    int n;
    scanf("%d", &n);
    int ans = 0;
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
    for (int i = 0,j = 0; i < n; i ++ )
    {
        s[a[i]]++;
        while(s[a[i]]>1)
        {
            s[a[j]]--;
            j++;
        }
        ans = max(ans,i-j+1);
    }
    cout << ans<<endl;
    return 0;
}