Codeforces Round #286 (Div. 1) C, D,

Source: Internet
Author: User

Codeforces Round #286 (Div. 1) C, D,

C: The number of steps in the question seems to be many. In fact, the maximum number of steps increases by about 250, because the number of moving steps is 1 + 2 + 3 + .. n, so there will be only sqrt (n) steps, so dp [I] [j] indicates that the value of step j is increased at the I position, and then transferred.

D: in fact, for a Unicom block, a maximum of n sides and a minimum of n-1 are required. The condition for judging is whether the UNICOM block has a ring, use topological sorting to determine

Code:

C:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 30005;int n, d, cnt[N], dp[N][500];int dfs(int u, int cha) {    if (u > 30000) return 0;    if (dp[u][cha] != -1) return dp[u][cha];    int tmp = d + cha - 250;    dp[u][cha] = cnt[u];    int ans = 0;    if (tmp > 1)ans = max(ans, dfs(u + tmp - 1, cha - 1));    ans = max(ans, dfs(u + tmp, cha));    ans = max(ans, dfs(u + tmp + 1, cha + 1));    dp[u][cha] += ans;    return dp[u][cha];}int main() {    scanf("%d%d", &n, &d);    int tmp;    for (int i = 0; i < n; i++) {scanf("%d", &tmp);cnt[tmp]++;    }    memset(dp, -1, sizeof(dp));    printf("%d\n", dfs(d, 250));    return 0;}

D:

#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int N = 100005;int n, m, du[N], vis[N], have[N], hn;vector<int> g[N], g2[N];void dfs(int u) {    have[hn++] = u;    vis[u] = 1;    for (int i = 0; i < g[u].size(); i++) {        int v = g[u][i];        if (vis[v]) continue;        dfs(v);     }}bool find() {    queue<int> Q;    for (int i = 0; i < hn; i++)        if (!du[have[i]]) Q.push(have[i]);    while (!Q.empty()) {        int u = Q.front();        Q.pop();        int sz = g2[u].size();        for (int i = 0; i < sz; i++) {            int v = g2[u][i];            du[v]--;            if (!du[v]) Q.push(v);        }    }    for (int i = 0; i < hn; i++)        if (du[have[i]]) return true;    return false;}int main() {    scanf("%d%d", &n, &m);    int u, v;    while (m--) {        scanf("%d%d", &u, &v);        du[v]++;        g[u].push_back(v);        g[v].push_back(u);        g2[u].push_back(v);    }    int ans = n;    for (int i = 1; i <= n; i++) {        if (!vis[i]) {            hn = 0;            dfs(i);            if (!find()) ans--;        }    }    printf("%d\n", ans);    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.