Codeforces 999

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[105];
int visit[105];
int main()
{
        int n, k;
        read(n), read(k);
        for (int i = 1; i <= n; i++)
        {
                read(num[i]);
        }
        int anser = 0;
        for (int i = 1; i <= n; i++)
        {
                if (num[i] <= k)
                {
                        visit[i] = 1;
                        anser++;
                }
                else
                {
                        break;
                }
        }
        for (int i = n; i >= 1; i--)
        {
                if (visit[i])
                {
                        break;
                }
                if (num[i] <= k)
                {
                        anser++;
                }
                else
                {
                        break;
                }
        }
        cout << anser << 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;
}
string s;
void doit(int x)
{
        for (int i = 0; i <= x / 2 - 1; i++)
        {
                swap(s[i], s[x - 1 - i]);
        }
}
int main()
{
        int n;
        read(n);
        cin >> s;
        for (int i = 2; i <= n; i++)
        {
                if (n % i == 0)
                {
                        doit(i);
                }
        }
        cout << s << 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;
}
string s;
int visit[400005];
queue<int> ch[30];
int main()
{
        int n, k;
        read(n), read(k);
        cin >> s;
        for (int i = 0; i < s.size(); i++)
        {
                ch[s[i] - 'a'].push(i);
                //cout<<s[i]-'a'<<" "<<i<<endl;
        }
        for (int j = 1; j <= k; j++)
        {
                for (int i = 0; i <= 25; i++)
                {
                        if (ch[i].size() > 0)
                        {
                                visit[ch[i].front()] = 1;
                                ch[i].pop();
                                break;
                        }
                }
        }
        for (int i = 0; i < s.size(); i++)
        {
                if(!visit[i])
                {
                        cout<<s[i];
                }
        }
        return 0;
}
View Code

D

磨了半天 没想到直接暴力也能过

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 202020;
int n, m, a[N];
vector<int> c[N];
LL ans;
int main()
{
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++)
        {
                scanf("%d", &a[i]);
                c[a[i] % m].push_back(i);
        }
        int ptr = 0;
        int goal = n / m;
        for (int i = 0; i < m; i++)  //从0开始检查比goal大的
        {
                while ((int)c[i].size() > goal)  //如果枚举到的比水平线高
                {
                        ptr = max(ptr, i); //保证找到的要在其后
                        while ((int)c[ptr % m].size() >= goal) //找到小于水平线的
                        {
                                ptr ++;
                        }
                        int tk = min((int)c[i].size() - goal, goal - (int)c[ptr % m].size());  //取多出与缺少的最小值
                        int dlt = ptr - i; //多的和少的之间的距离
                        while (tk--)
                        {
                                ans += dlt;
                                a[c[i].back()] += dlt;
                                c[ptr % m].push_back(c[i].back());
                                c[i].pop_back();
                        }
                }
        }
        printf("%lld\n", ans);
        for (int i = 0; i < n; i++)
        {
                printf("%d%c", a[i], " \n"[i + 1 == n]);
        }
}
View Code

E

暴力染色即可

/*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 = 5e3 + 5, MAXM = 5e3 + 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 du[MAXN], visit[MAXN];
int visit2[MAXN];
int ans = 0;
void dfs(int x, int pre)
{
        visit[x] = pre;
        visit2[x] = 1;
        for (int i = Head[x]; i; i = nxt[i])
        {
                int v = to[i];
                if (!visit2[v])
                {
                        dfs(v, pre);
                }
        }
}
int vis[MAXN];
int main()
{
        int n, m, s, u, v;
        read(n), read(m), read(s);
        for (int i = 1; i <= m; i++)
        {
                read(u), read(v);
                addedge(u, v);
                du[v]++;
        }
        for (int i = 1; i <= n; i++)
        {
                if (!visit[i])
                {
                        mem(visit2, 0);
                        dfs(i, i);
                }
        }
        mem(visit2,0);
        dfs(s, s);
        for (int i = 1; i <= n; i++)
        {
                if (vis[visit[i]] == 0)
                {
                        vis[visit[i]] = 1;
                        ans++;
                }
        }
        cout << ans - 1 << endl;
}
View Code

tarjan缩点后找入度为0的点 可以O(n+m)的做

F

dp

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 101010;
int n, k, c[N], f[N], h[N];
const int K = 514;
int dp[10 * K];
int cal(int x)
{
        if (f[x] == 0)
        {
                return 0;
        }
        int atmst = min(f[x] * k, c[x]);
        for (int i = 0; i <= atmst; i++)
        {
                dp[i] = 0;
        }
        while (f[x]--)
        {
                for (int i = atmst; i >= 0; i--)
                        for (int j = 1; j <= k and i - j >= 0; j++)
                        {
                                dp[i] = max(dp[i], dp[i - j] + h[j]);
                        }
        }
        return dp[atmst];
}
int main()
{
        scanf("%d%d", &n, &k);
        for (int i = 0; i < n * k; i++)
        {
                int ci;
                scanf("%d", &ci);
                c[ci]++;
        }
        for (int i = 0; i < n; i++)
        {
                int fi;
                scanf("%d", &fi);
                f[fi]++;
        }
        for (int i = 1; i <= k; i++)
        {
                scanf("%d", &h[i]);
        }
        int ans = 0;
        for (int i = 0; i < N; i++)
        {
                ans += cal(i);
        }
        cout << ans << endl;
}
View Code
posted @ 2018-06-22 19:00  Aragaki  阅读(172)  评论(0编辑  收藏  举报