Codeforces Round #287 (Div. 2) A, B, C, D, E,

Source: Internet
Author: User

Codeforces Round #287 (Div. 2) A, B, C, D, E,

A: sign-in questions. Just sort and judge the number of questions you can learn.

B: You can move the 2 * r distance to the center of a circle in any direction. Therefore, the answer is ceil (two points/2/r)

C: recursion. dfs (h, n, flag), h indicates the current layer, n indicates the relative position of the exit under the current layer, and flag indicates whether the next step is left or right.

D: digit DP. Place the number from the decimal bit to the highest bit. If the modulus is 0, you can directly calculate the number of digits after the input number, the last digit can be 1-9, and the other digits are 0-9. Otherwise, the memory will continue to be searched.

E: the shortest path is used to create a graph. Assume that the total number of one side is x, and the number of one side is a and the number of zero sides is B in the selected path. The answer is x-a + B, the answer should be as small as possible, so the priority of selecting 1 in the shortest path should be greater than the priority of selecting 0. This processing method can directly increase the edge weight by times, and then add more than 1 in the 0 side.

Code:

A:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 105;int n, k;struct SB {int a, id;} sb[N];bool cmp(SB a, SB b) {return a.a < b.a;}int main() {scanf("%d%d", &n, &k);for (int i = 0; i < n; i++) {scanf("%d", &sb[i].a);sb[i].id = i;}sort(sb, sb + n, cmp);int tot = 0;int cnt = 0;for (int i = 0; i < n; i++) {if (tot + sb[i].a > k) break;tot += sb[i].a;cnt++;}printf("%d\n", cnt);for (int i = 0; i < cnt; i++)printf("%d ", sb[i].id + 1);return 0;}


B:

#include <cstdio>#include <cstring>#include <cmath>double r, xa, ya, xb, yb;const double eps = 1e-8;int main() {scanf("%lf%lf%lf%lf%lf", &r, &xa, &ya, &xb, &yb);double d = sqrt((xb - xa) * (xb - xa) + (yb - ya) * (yb - ya));printf("%.0lf\n", ceil(d / r / 2));return 0;}

C:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;int h;ll n;ll dfs(int d, ll n, int lr) {if (d == h) return 0;if (!lr && n > (1LL<<(h - d - 1)))return (1LL<<(h - d)) + dfs(d + 1, n - (1LL<<(h - d - 1)), 0);if (lr && n <= (1LL<<(h - d - 1)))return (1LL<<(h - d)) + dfs(d + 1, n, 1);if (!lr && n <= (1LL<<(h - d - 1)))return dfs(d + 1, n, 1) + 1;if (lr && n > (1LL<<(h - d - 1)))return dfs(d + 1, n - (1LL<<(h - d - 1)), 0) + 1;}int main() {scanf("%d%lld", &h, &n);printf("%lld\n", dfs(0, n, 0));return 0;}


D:

#include <cstdio>#include <cstring>typedef long long ll;const int N = 1005;const int M = 105;int n, k;ll m, pow10[N];ll dp[N][M][2];ll pow_mod(ll x, int k) {ll ans = 1;while (k) {if (k&1) ans = ans * x % m;x = x * x % m;k >>= 1;}return ans;}ll dfs(int u, int mod, int flag) {ll &ans = dp[u][mod][flag];if (ans != -1) return ans;ans = 0;if (u == n) return ans;int s = 0;if (u == n - 1) s = 1;for (int i = s; i <= 9; i++) {int next = (pow10[u] * i + mod) % k;int f = flag;if (i > 0) f = 1;if (next == 0 && f) {if (n - u - 2 >= 0)ans = (ans + pow_mod(10LL, n - u - 2) * 9 % m) % m;else ans = (ans + 1) % m;}else {ans = (ans + dfs(u + 1, next, f)) % m;}}return ans;}int main() {scanf("%d%d%lld", &n, &k, &m);pow10[0] = 1;for (int i = 1; i < N; i++)pow10[i] = pow10[i - 1] * 10 % k;memset(dp, -1, sizeof(dp));printf("%lld\n", dfs(0, 0, 0));return 0;}

E:

#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;#define INF 0x3f3f3f3f3f3f3ftypedef long long ll;const int MAXNODE = 100005;const int N = 100005;struct Edge {int u, v, use;ll dist;Edge() {}Edge(int u, int v, ll dist) {this->u = u;this->v = v;this->dist = dist;this->use = 0;}};Edge out[N];int on;struct HeapNode {ll d;int u;HeapNode() {}HeapNode(ll d, int u) {this->d = d;this->u = u;}bool operator < (const HeapNode& c) const {return d > c.d;}};struct Dijkstra {int n, m;vector<Edge> edges;vector<int> g[MAXNODE];bool done[MAXNODE];ll d[MAXNODE];int p[MAXNODE];void init(int tot) {n = tot;for (int i = 1; i <= n; i++)g[i].clear();edges.clear();}void add_Edge(int u, int v, ll dist) {edges.push_back(Edge(u, v, dist));m = edges.size();g[u].push_back(m - 1);}void dijkstra(int s) {priority_queue<HeapNode> Q;for (int i = 1; i <= n; i++) d[i] = INF;d[s] = 0;p[s] = -1;memset(done, false, sizeof(done));Q.push(HeapNode(0, s));while (!Q.empty()) {HeapNode x = Q.top(); Q.pop();int u = x.u;if (done[u]) continue;done[u] = true;for (int i = 0; i < g[u].size(); i++) {Edge& e = edges[g[u][i]];if (d[e.v] > d[u] + e.dist) {d[e.v] = d[u] + e.dist;p[e.v] = g[u][i];Q.push(HeapNode(d[e.v], e.v));}}}}void print() {int u = n;on = 0;while (p[u] != -1) {edges[p[u]].use = 1;edges[p[u]^1].use = 1;u = edges[p[u]].u;}for (int i = 0; i < edges.size(); i += 2) {if (edges[i].use == 0 && edges[i].dist == 100000) {out[on] = edges[i];out[on].dist = 0;on++;} else if (edges[i].use == 1 && edges[i].dist == 100001) {out[on] = edges[i];out[on].dist = 1;on++;}}printf("%d\n", on);for (int i = 0; i < on; i++) {int a = out[i].u;int b = out[i].v;ll c = out[i].dist;printf("%d %d %lld\n", a, b, c);}}} gao;int n, m;int main() {scanf("%d%d", &n, &m);gao.init(n);int u, v, w;while (m--) {scanf("%d%d%d", &u, &v, &w);if (w == 1) {gao.add_Edge(u, v, 100000);gao.add_Edge(v, u, 100000);} else {gao.add_Edge(u, v, 100001);gao.add_Edge(v, u, 100001);}}gao.dijkstra(1);gao.print();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.