头像

Niku

专业代写bug


访客:1139

离线:17小时前


活动打卡代码 AcWing 1524. 最长回文子串

Niku
20小时前
#include <iostream>
using namespace std;
int dp[1010][1010];
int main() {
    string s;
    getline(cin, s);
    int len = s.length(), ans = 1;
    for(int i = 0; i < len; i++) {
        dp[i][i] = 1;
        if(i < len - 1 && s[i] == s[i+1]) {
            dp[i][i+1] = 1;
            ans = 2;
        }
    }
    for(int L = 3; L <= len; L++) {
        for(int i = 0; i + L - 1 < len; i++) {
            int j = i + L -1;
            if(s[i] == s[j] && dp[i+1][j-1] == 1) {
                dp[i][j] = 1;
                ans = L;
            }
        }
    }
    printf("%d", ans);
    return 0;
}



Niku
21小时前
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000;

struct Node
{
    int address, data, next, order = 2 * maxn;
}node[maxn];

bool exist[maxn];

bool cmp(Node a, Node b) {
    return a.order < b.order;
}

int main() {

    int first, n, cnt1 = 0, cnt2 = 0, temp;
    cin >> first >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> temp;
        cin >> node[temp].data >> node[temp].next;
        node[temp].address = temp;
    }
    for (int i = first; i != -1; i = node[i].next)
    {
        if (exist[abs(node[i].data)] == false)
        {
            exist[abs(node[i].data)] = true;
            node[i].order = cnt1;
            cnt1++;
        }
        else
        {
            node[i].order = maxn + cnt2;
            cnt2++;
        }
    }
    sort(node, node + maxn, cmp);
    int cnt = cnt1 + cnt2;
    for (int i = 0; i < cnt; i++)
    {
        if (i != cnt1 - 1 && i != cnt - 1)
            printf("%05d %d %05d\n", node[i].address, node[i].data, node[i + 1].address);
        else
            printf("%05d %d -1\n", node[i].address, node[i].data);
    }
    return 0;
}


活动打卡代码 AcWing 1535. 弹出序列

Niku
1天前
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int main() {
    int m, n, k;
    cin >> m >> n >> k;
    for (int i = 0; i < k; i++)
    {
        bool flag = false;
        stack<int> st;
        vector<int> v(n + 1);
        for (int j = 1; j <= n; j++)
            cin >> v[j];
        int now = 1;
        for (int j = 1; j <= n; j++)
        {
            st.push(j);
            if (st.size() > m) break;
            while (!st.empty()&&st.top() == v[now])
            {
                st.pop();
                now++;
            }
        }
        if (now == n + 1) flag = true;
        if (flag) cout << "YES" << '\n';
        else cout << "NO" << '\n';
    }
    return 0;
}


活动打卡代码 AcWing 1607. 爱丁顿数

Niku
1天前
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 1e5 + 10;

vector<int> dis;
int main() {
    int n;
    cin >> n;
    dis.resize(n);
    for (int i = 0; i < n; i++) cin >> dis[i];

    sort(dis.begin(), dis.end());

    for (int i = n; i; i--)
    {
        if (dis[n - i] > i){
            cout << i<<'\n';
            return 0;
        }
    }
    puts("0");
    return 0;
}


活动打卡代码 AcWing 1530. 最短距离

Niku
1天前
#include <cstdio>
#include <algorithm>

using namespace std;

const int MAXN = 100010;
int dis[MAXN], a[MAXN];

int main() {

    int sum = 0, query, n, left, right;

    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &a[i]);
        sum += a[i];
        dis[i] = sum;
    }

    scanf("%d", &query);
    for (int i = 0; i < query; i++)
    {
        scanf("%d%d", &left, &right);
        if (left > right) swap(left, right);
        int temp = dis[right - 1] - dis[left - 1];
        printf("%d\n", min(temp, sum - temp));
    }
    return 0;
}


活动打卡代码 AcWing 1626. 链表元素分类

Niku
2天前
#include <iostream>
#include <vector>
using namespace std;

const int M = 1e6 + 10;

int head, n, k;
int d[M], ne[M];

int main() {

    cin >> head >> n >> k;

    fill(ne, ne + M, -1);

    for (int i = 0; i < n; i++)
    {
        int address, key, next;
        cin >> address >> key >> next;
        d[address] = key;
        ne[address] = next;
    }
    vector<int> a, b, c;

    for (int i = head; i != -1; i = ne[i])
    {
        int v = d[i];
        if (v < 0) a.push_back(i);
        else if (v <= k) b.push_back(i);
        else c.push_back(i);
    }

    a.insert(a.end(), b.begin(), b.end());
    a.insert(a.end(), c.begin(), c.end());
    for (int i = 0; i < a.size(); i++)
    {
        printf("%05d %d ", a[i], d[a[i]]);
        if (i + 1 == a.size()) puts("-1");
        else printf("%05d\n", a[i + 1]);
    }
    return 0;
}



Niku
3天前

竟然枚举就行了···我太蠢了QAQ

#include <iostream>
#include <cstring>

using namespace std;

const int N = 110;

int n;
int q[N];

int judge(int k, int i, int j)  // 如果是假话,返回1;如果是真话,返回0
{
    int t = q[k];
    if (t > 0)
    {
        if (t == i || t == j) return 1;
        return 0;
    }

    t = -t;
    if (t == i || t == j) return 0;
    return 1;
}

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

    for (int i = 1; i <= n; i ++ )
        for (int j = i + 1; j <= n; j ++ )
        {
            int s = judge(i, i, j) + judge(j, i, j);
            if (s != 1) continue;

            s = 0;
            for (int k = 1; k <= n; k ++ )
                s += judge(k, i, j);

            if (s != 2) continue;

            cout << i << ' ' << j << endl;
            return 0;
        }

    puts("No Solution");

    return 0;
}


活动打卡代码 AcWing 1560. 反转链表

Niku
3天前
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int first, k, n, temp;
    cin >> first >> n >> k;
    int data[100005], next[100005], list[100005];
    for (int i = 0; i < n; i++) {
        cin >> temp;
        cin >> data[temp] >> next[temp];    //以地址为索引存储数据和下一个的地址
    }
    int sum = 0;//不一定所有的输入的结点都是有用的,加个计数器
    while (first != -1) {    //对合理的输入进行计数,并且存起来,筛选掉断开的节点输入
        list[sum++] = first;
        first = next[first];
    }
    for (int i = 0; i < (sum - sum % k); i += k)    //只需要对需要反转的部分反转即可
        reverse(begin(list) + i, begin(list) + i + k);
    for (int i = 0; i < sum - 1; i++)    //输出的时候注意最后一个元素的特殊性
        printf("%05d %d %05d\n", list[i], data[list[i]], list[i + 1]);
    printf("%05d %d -1", list[sum - 1], data[list[sum - 1]]);
    return 0;
}


活动打卡代码 AcWing 1618. 结绳

Niku
4天前
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 10010;

int n;
double a[N],res;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    res = a[0];
    for (int i = 1; i < n; i++)
    {
        res = (res + a[i]) / 2;
    }
    printf("%d\n", (int)res);
    return 0;
}


活动打卡代码 AcWing 1603. 整数集合划分

Niku
4天前
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100010;
int n, a[N];
int s1, s2;

int main() {

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

    sort(a, a + n);
    if (n % 2 == 0)cout << 0 << ' ';
    else cout << 1 << ' ';

    for (int i = 0; i < n; i++)
    {
        if (i < n / 2) s1 += a[i];
        else s2 += a[i];
    }
    cout << abs(s1 - s2);
    return 0;
}