Codeforces 1000 组合数可行线段倒dp 边双联通缩点求树直径

A

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
const int mod = 1e9 + 7, gakki = 5 + 2 + 1 + 19880611 + 1e9;
const int MAXN = 2e5 + 5, MAXM = 2e5 + 5, N = 2e5 + 5;
const int MAXQ = 100010;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
        to[++tot] = v;
        nxt[tot] = Head[u];
        Head[u] = tot;
}*/
inline void read(int &v)
{
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9')
        {
                if (c == '-')
                {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9')
        {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
int num[15];
int anser[15];
map<string, int> mp;
int main()
{
        mp["S"] = 1, mp["M"] = 2, mp["L"] = 3;
        mp["XL"] = 4, mp["XXL"] = 5, mp["XXXL"] = 6;
        mp["XS"] = 7, mp["XXS"] = 8, mp["XXXS"] = 9;
        int n;
        int ans = 0;
        read(n);
        string now;
        for (int i = 1; i <= n; i++)
        {
                cin >> now;
                num[mp[now]]++;
        }
        for (int i = 1; i <= n; i++)
        {
                cin >> now;
                anser[mp[now]]++;
        }
        for (int i = 1; i <= 14; i++)
        {
                ans += abs(num[i] - anser[i]);
        }
        cout << ans / 2 << endl;
        return 0;
}
View Code

B

分奇偶即可

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
const int mod = 1e9 + 7, gakki = 5 + 2 + 1 + 19880611 + 1e9;
const int MAXN = 2e5 + 5, MAXM = 2e5 + 5, N = 2e5 + 5;
const int MAXQ = 100010;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
        to[++tot] = v;
        nxt[tot] = Head[u];
        Head[u] = tot;
}*/
inline void read(int &v)
{
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9')
        {
                if (c == '-')
                {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9')
        {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
int num[200005];
int anser = 0;
int number[200005];
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
                cin >> num[i];
        }
        n++;
        num[n] = m;
        for (int i = 1; i <= n; i++)
        {
                number[i] = number[i - 1];
                if (i & 1)
                {
                        int add = num[i] - num[i - 1];
                        number[i] += add;
                }
        }
        anser = number[n];
        for (int i = 1; i <= n - 1; i++)
        {
                if (num[i] - 1 > num[i - 1] || num[i] + 1 < num[i + 1])
                {
                        if (i & 1)
                        {
                                int now = number[i] + m - num[i] + number[i] - number[n] - 1;
                                anser = max(anser, now);
                        }
                }
        }
        cout << anser << endl;
        return 0;
}
View Code

C

离散化前缀和

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
const int mod = 1e9 + 7, gakki = 5 + 2 + 1 + 19880611 + 1e9;
const int MAXN = 2e5 + 5, MAXM = 2e5 + 5, N = 2e5 + 5;
const int MAXQ = 100010;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
        to[++tot] = v;
        nxt[tot] = Head[u];
        Head[u] = tot;
}*/
inline void read(int &v)
{
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9')
        {
                if (c == '-')
                {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9')
        {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
ll number[600005];
ll ans[600005];
ll l[200005], r[200005];
ll anser[200005];
map<ll, ll> mp;
map<ll, ll> mpb;
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
                cin >> l[i] >> r[i];
                number[3 * i - 2] = l[i];
                number[3 * i - 1] = r[i];
                number[3 * i] = r[i] + 1;
        }
        sort(number + 1, number + 3 * n + 1);
        int pop = unique(number + 1, number + 3 * n + 1) - number - 1;
        //        for (int i = 1; i <= pop; i++)
        //        {
        //                cout << number[i] << " ";
        //        }
        //        cout << endl;
        for (int i = 1; i <= pop; i++)
        {
                mp[number[i]] = i;
                mpb[i] = number[i];
        }
        for (int i = 1; i <= n; i++)
        {
                ans[mp[l[i]]]++;
                ans[mp[r[i]] + 1]--;
        }
        for (int i = 1; i <= pop; i++)
        {
                ans[i] += ans[i - 1];
        }
        ll cur = ans[1];
        for (int i = 1; i <= pop - 1; i++)
        {
                cur = ans[i];
                anser[cur] += mpb[i + 1] - mpb[i];
        }
        cur = ans[pop];
        anser[cur]++;
        for (int i = 1; i <= n; i++)
        {
                cout << anser[i] << " ";
        }
        cout << endl;
        return 0;
}
View Code

D

倒着DP

/*Huyyt*/
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int dir[8][2] = {{0, 1}, {1, 0}, {0, -1}, { -1, 0}, {1, 1}, {1, -1}, { -1, -1}, { -1, 1}};
const int mod = 998244353, gakki = 5 + 2 + 1 + 19880611 + 1e9;
const int MAXN = 2e5 + 5, MAXM = 2e5 + 5, N = 2e5 + 5;
const int MAXQ = 100010;
/*int to[MAXM << 1], nxt[MAXM << 1], Head[MAXN], tot = 1;
inline void addedge(int u, int v)
{
        to[++tot] = v;
        nxt[tot] = Head[u];
        Head[u] = tot;
}*/
inline void read(int &v)
{
        v = 0;
        char c = 0;
        int p = 1;
        while (c < '0' || c > '9')
        {
                if (c == '-')
                {
                        p = -1;
                }
                c = getchar();
        }
        while (c >= '0' && c <= '9')
        {
                v = (v << 3) + (v << 1) + c - '0';
                c = getchar();
        }
        v *= p;
}
ll zuhe[1005][1005];
int n;
ll num[1005];
ll ans[1005];
ll anser = 0;
void init()
{
        zuhe[0][0] = 1;
        for (int i = 1; i <= n; i++)
        {
                zuhe[i][0] = 1;
                for (int j = 1; j <= i; j++)
                {
                        zuhe[i][j] = (zuhe[i - 1][j] + zuhe[i - 1][j - 1]) % mod;
                }
        }
}
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);

        cin >> n;
        ans[n + 1] = 1;
        init();
        for (int i = 1; i <= n; i++)
        {
                cin >> num[i];
        }
        for (int i = n; i >= 1; i--)
        {
                if (num[i] <= 0)
                {
                        continue;
                }
                for (int j = i + num[i] + 1; j <= n + 1; j++)
                {
                        ans[i] += zuhe[j - i - 1][num[i]] * ans[j];
                        ans[i] %= mod;
                }
        }
        for (int i = 1; i <= n; i++)
        {
                anser += 1LL * ans[i];
                anser %= mod;
        }
        cout << anser << endl;
        return 0;
}
View Code

E

边双联通分量缩点后是一颗树 再求树的直径即可

/*Huyyt*/
#include <bits/stdc++.h>
#include <vector>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N = 300000 + 5;
const int M = 600000 + 5;
struct EDGE
{
    int v, next;
} edge[M * 2];
int first[N], low[N], dfn[N], belong[N], degree[N], sta[M], instack[M];
vector<int> gra[300005];
int g, cnt, top, scc;
int min(int a, int b)
{
    return a < b ? a : b;
}
void AddEdge(int u, int v)
{
    edge[g].v = v;
    edge[g].next = first[u];
    first[u] = g++;
}
void Tarjan(int u, int fa)
{
    int i, v;
    low[u] = dfn[u] = ++cnt;
    sta[++top] = u;
    instack[u] = 1;
    for (i = first[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].v;
        if (i == (fa ^ 1))
        {
            continue;
        }
        if (!dfn[v])
        {
            Tarjan(v, i);
            low[u] = min(low[u], low[v]);
        }
        else if (instack[v])
        {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if (dfn[u] == low[u])
    {
        scc++;
        while (1)
        {
            v = sta[top--];
            instack[v] = 0;
            belong[v] = scc;
            if (v == u)
            {
                break;
            }
        }
    }
}
map<pair<int, int>, int> mp;
int ssssstart, eeeeeeend;
int distence[300005];
int visit[300005];
void getdis(int x)
{
    visit[x] = 1;
    int len = gra[x].size();
    for (int i = 0; i < len; i++)
    {
        int to = gra[x][i];
        if (visit[to])
        {
            continue;
        }
        distence[to] = distence[x] + 1;
        getdis(to);
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    memset(first, -1, sizeof(first));
    int n, m;
    int u, v;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> u >> v;
        AddEdge(u, v);
        AddEdge(v, u);
    }
    for (int i = 1; i <= n; i++)
    {
        if (!dfn[i])
        {
            Tarjan(i, -1);
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = first[i]; j != -1; j = edge[j].next)
        {
            v = edge[j].v;
            if (belong[i] != belong[v])
            {
                if (!mp[make_pair(belong[i], belong[v])])
                {
                    gra[belong[i]].push_back(belong[v]);
                    gra[belong[v]].push_back(belong[i]);
                    mp[make_pair(belong[i], belong[v])] = mp[make_pair(belong[v], belong[i])] = 1;
                }
            }
        }
    }
    getdis(1);
    int maxnn = -1;
    for (int i = 1; i <= scc; i++)
    {
        if (distence[i] > maxnn)
        {
            ssssstart = i;
            maxnn = distence[i];
        }
    }
    mem(visit, 0), mem(distence, 0);
    getdis(ssssstart);
    maxnn = -1;
    for (int i = 1; i <= scc; i++)
    {
        if (distence[i] > maxnn)
        {
            eeeeeeend = i;
            maxnn = distence[i];
        }
    }
    cout << distence[eeeeeeend] << endl;
    return 0;
}
View Code

 

posted @ 2018-06-28 13:44  Aragaki  阅读(264)  评论(0编辑  收藏  举报