codeforce 379(div.2)

A、B略

C题 ——贪心,二分查找:

对于每一个a[i], 在d中二分查找 s-b[i],注意不要忘记计算速度为x时需要花费的最小时间,以及整数范围为64位整数

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4
 5 using namespace std;
 6 const int maxn = 2*100000+10;
 7 typedef long long LL;
 8
 9 int n, m, k;
10 int x, s;
11 int a[maxn], b[maxn], c[maxn], d[maxn];
12
13 int main() {
14     scanf("%d%d%d", &n, &m, &k);
15     scanf("%d%d", &x, &s);
16     a[0] = x;
17     b[0] = 0;
18     for(int i = 1; i <= m; i++) scanf("%d",&a[i]);
19     for(int i = 1; i <= m; i++) scanf("%d",&b[i]);
20     for(int i = 0; i < k; i++) scanf("%d",&c[i]);
21     for(int i = 0; i < k; i++) scanf("%d",&d[i]);
22     LL  ans = LL(n)*x;
23     for(int i = 0; i <=  m; i++) {
24         if(b[i] <= s) {
25             int temp = b[i];
26             int tt = upper_bound(d, d+k, s - temp) - d;
27             if(tt) {
28                 tt--;
29                 LL  as = n - c[tt] > 0 ? LL(n - c[tt]) * a[i] : 0;
30                 ans = min(ans, as);
31             }
32             else {
33                 LL  as = LL(n) * a[i];
34                 ans = min(ans, as);
35             }
36         }
37     }
38     printf("%I64d\n", ans);
39 }

D 思路很简单,注意细节即可

  1 #include <cstdio>
  2 #include <vector>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cstdlib>
  6 #include <cmath>
  7 using namespace std;
  8 const int maxn = 500000+10;
  9
 10 struct P{
 11     char ch;
 12     double dist;
 13     P(char c, double d): ch(c), dist(d){}
 14     bool operator < (const P& b)const {
 15         return dist < b.dist;
 16     }
 17 };
 18 vector<P> dig1, dig2, dig3, dig4;
 19 vector<P> ver1, ver2, ver3, ver4;
 20
 21 int n;
 22 int x0, y0_;
 23 #define y0  y0_
 24 void ins(char c, int dx, int dy) {
 25     if(dx == dy) {
 26         if(dx > 0) {
 27             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 28             dig1.push_back(P(c, dis));
 29         }
 30         if(dx < 0) {
 31             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 32             dig3.push_back(P(c, dis));
 33
 34         }
 35     }
 36     if(dx == -dy) {
 37         if(dx > 0) {
 38             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 39             dig4.push_back(P(c, dis));
 40         }
 41         if(dx < 0) {
 42             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 43             dig2.push_back(P(c, dis));
 44
 45         }
 46     }
 47     if(dy == 0){
 48         if(dx > 0) {
 49             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 50             ver1.push_back(P(c,dis));
 51         }
 52         if(dx < 0) {
 53             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 54             ver3.push_back(P(c,dis));
 55
 56         }
 57     }
 58     if(dx == 0){
 59         if(dy > 0) {
 60             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 61             ver2.push_back(P(c, dis));
 62         }
 63         if(dy < 0) {
 64             double dis = sqrt(double(dy)*dy + double(dx)*dx);
 65             ver4.push_back(P (c, dis));
 66         }
 67     }
 68 }
 69 int main () {
 70     scanf("%d", &n);
 71     scanf("%d%d", &x0, &y0);
 72     for(int i = 0; i < n; i++) {
 73         char ch;
 74         int xx, yy;
 75         getchar();
 76         scanf("%c%d%d", &ch, &xx, &yy);
 77         int dy = yy - y0, dx = xx - x0;
 78         ins(ch , dx, dy);
 79     }
 80     sort(dig1.begin(), dig1.end());
 81     sort(dig2.begin(), dig2.end());
 82     sort(dig3.begin(), dig3.end());
 83     sort(dig4.begin(), dig4.end());
 84     sort(ver1.begin(), ver1.end());
 85     sort(ver2.begin(), ver2.end());
 86     sort(ver3.begin(), ver3.end());
 87     sort(ver4.begin(), ver4.end());
 88     if(dig1.size()) {
 89         if(dig1[0].ch == ‘Q‘ ||  dig1[0].ch == ‘B‘) {
 90             printf("YES\n");
 91             return 0;
 92         }
 93     }
 94     if(dig2.size()) {
 95         if(dig2[0].ch == ‘Q‘ ||  dig2[0].ch == ‘B‘) {
 96             printf("YES\n");
 97             return 0;
 98         }
 99     }
100     if(dig3.size()) {
101         if(dig3[0].ch == ‘Q‘ ||  dig3[0].ch == ‘B‘) {
102             printf("YES\n");
103             return 0;
104         }
105     }
106     if(dig4.size()) {
107         if(dig4[0].ch == ‘Q‘ ||  dig4[0].ch == ‘B‘) {
108             printf("YES\n");
109             return 0;
110         }
111     }
112     if(ver1.size()) {
113         if(ver1[0].ch == ‘Q‘ || ver1[0].ch == ‘R‘) {
114             printf("YES\n");
115             return 0;
116         }
117     }
118
119     if(ver2.size()) {
120         if(ver2[0].ch == ‘Q‘ || ver2[0].ch == ‘R‘) {
121             printf("YES\n");
122             return 0;
123         }
124     }
125     if(ver3.size()) {
126         if(ver3[0].ch == ‘Q‘ || ver3[0].ch == ‘R‘) {
127             printf("YES\n");
128             return 0;
129         }
130     }
131     if(ver4.size()) {
132         if(ver4[0].ch == ‘Q‘ || ver4[0].ch == ‘R‘) {
133             printf("YES\n");
134             return 0;
135         }
136     }
137     printf("NO\n");
138     return 0;
139
140 }

E题:首先使用并查集将相同颜色的结点缩成一点,此时树中结点黑白相间,只需要求出树的直径(最长路)除以2就是答案。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7
 8 const int maxn = 200000+10;
 9 vector<int> G[maxn];
10 void add(int v, int u) {
11     G[v].push_back(u);
12     G[u].push_back(v);
13 }
14 int n;
15 int col[maxn];
16 int col2[maxn];
17 int vis[maxn];
18 int pa[maxn];
19 int findpa(int x) {return x == pa[x] ? x : pa[x] = findpa(pa[x]);}
20 int from, ans;
21 void dfs(int u, int v, int d) {
22     if(d > ans) {
23         ans = d;
24         from = v;
25     }
26     for(int i = 0 ; i< G[v].size(); i++) {
27         int z = G[v][i];
28         if(z != u) {
29             dfs(v, z, d+1);
30         }
31     }
32 }
33 int U[maxn];
34 int V[maxn];
35 int main() {
36     scanf("%d", &n);
37     for(int i = 1; i <= n; i++)
38         scanf("%d", &col[i]);
39
40     for(int i = 1; i < n; i++)
41         scanf("%d%d", &U[i], &V[i]);
42     for(int i = 1; i <= n; i++) pa[i] = i;
43     for(int i = 1; i <= n; i++) {
44         int fu = findpa(U[i]);
45         int fv = findpa(V[i]);
46         if(col[fu] == col[fv]) {
47             pa[fu] = fv;
48         }
49     }
50     for(int i = 1; i <= n; i++) {
51         int fu = findpa(U[i]);
52         int fv = findpa(V[i]);
53         if(col[fu] != col[fv]) {
54             add(fu, fv);
55         }
56     }
57     ans = -1;
58     int u = pa[1];
59     for(int i = 0; i < G[u].size(); i++) {
60         int v = G[u][i];
61         dfs(u, v, 1);
62     }
63     u = from;
64     for(int i = 0; i < G[u].size(); i++) {
65         int v = G[u][i];
66         dfs(u, v, 1);
67     }
68     ans = (ans+1)/2;
69     printf("%d\n", ans);
70 }

F题:注意到  a&b + a|b = a+b

即可根据数组b, c的定义求出a,然后用求出的a反推数组b,c判断是否是解

详见代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4
 5 using namespace std;
 6 const int maxn = 200000 + 10;
 7 typedef long long LL;
 8
 9 LL  a[maxn], b[maxn], c[maxn];
10 int num[64];
11 int n;
12 int main() {
13     ios::sync_with_stdio(false);
14     cin >> n;
15     for(int i = 0; i < n; i++) cin >> b[i];
16     for(int i = 0; i < n; i++) cin >> c[i];
17     LL sum = 0;
18     for(int i = 0; i < n; i++) {
19         a[i] = b[i] + c[i];
20         sum += a[i];
21     }
22     if(sum % (2*n)) {
23         cout << -1 << endl;
24         return 0;
25     }
26     sum /= 2*n;
27     for(int i = 0; i < n; i++) {
28         a[i] = (a[i] - sum);
29         if(a[i] % n) {
30             cout << -1 << endl;
31             return 0;
32         }
33         else{
34             a[i] /= n;
35         }
36     }
37     for(int i = 0; i < n; i++) {
38         LL temp = a[i];
39         for(int j = 0; j < 64; j++) {
40             num[j] += temp%2;
41             temp /= 2;
42         }
43     }
44     for(int i = 0; i < n; i++) {
45         LL b_ = 0, c_ = 0;
46         for(int j = 0; j < 64; j++) {
47             if(a[i] & (1LL << j)) {
48                 b_ += num[j] * (1LL << j);
49                 c_ += n * (1LL << j);
50             }
51             else c_ += num[j] * (1LL << j);
52         }
53         if(b_ != b[i] || c_ != c[i]) {
54                 cout << -1 << endl;
55                 return 0;
56         }
57     }
58     cout << a[0];
59     for(int i = 1; i < n; i++) {
60         cout << " " << a[i];
61     }
62     cout << endl;
63     return 0;
64 }
时间: 2024-10-10 14:28:27

codeforce 379(div.2)的相关文章

Codeforce 214 Div 2 B.Hometask

题目描述: Description Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? Y

Codeforces Round #370 - #379 (Div. 2)

题意: 思路: Codeforces Round #370 A - Memory and Crow 题意:有一个序列,然后对每一个进行ai?=?bi?-?bi?+?1?+?bi?+?2?-?bi?+?3.... 的操作,最后得到了a 序列,给定 a 序列,求原序列. 思路:水. 1 #include <set> 2 #include <map> 3 #include <stack> 4 #include <queue> 5 #include <cstd

Codeforces Round #379 (Div. 2) Analyses By Team:Red &amp; Black

A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lucky_ji: 水题,模拟统计A和D的数目比较大小输出结果即可 Tags: Implementation B.Anton and Digits Problems: 给定k2个2,k3个3,k5个5及k6个6,可以组成若干个32或256,求所有方案中Sigma的最大值 Analysis: lucky_ji: 同

Codeforces Round #379(div 2)

A.B:=w= C: 题意: 你需要制作n瓶药水,每一瓶药水需要x秒. 你现在有m种A魔法,花费b[i],使得每一瓶药水的花费代价降为a[i],只能用一次. 有K种B魔法,花费d[i],使得瞬间制作好c[i]瓶药水,只能用一次. 你最多花费s的魔法值 问你最快完成要多少秒. 分析:题目所给的B魔法都是递增的,容易想到枚举用哪个A魔法,二分对应的B魔法,因为在MP充足的情况下,减少的药水越多对ans越有利 D: 题意:一个无限大的平面,给你一个King的位置,再给你其他棋子的位置,问你这个King

Codeforces Round #379 (Div. 2) 总结分享

前言 初入acm的新手,打算在cf混.这几天没有比赛,就做了个最新的Virtual participation.虽然说div2比较简单,但还是被虐得体无完肤...Orz.两个小时,共6道题.最后只AC了AB两道,花了20分钟,剩下的100分钟也不知道哪去了(逃 A.B两道水题没什么说的.那我们从C题开始吧: C. Anton and Making Potions 题意: 制作n瓶药水,初始时每制作一瓶花费x秒,有两类法术,第一类(包含m个法术)使制作每瓶药的时间由x变为a[i] (a[i] <

Codeforces Round #379 (Div. 2) 解题报告

题目地址 本次CF是在今天早上深夜进行,上午有课就没有直接参加.今天早上上课坐到后排参加了virtual participation.这次CF前面的题目都非常的水,不到10分钟就轻松过了前两题,比较郁闷的是之后一直卡在C,开始是脑残的没有用二分TLE,后来又是因为一个常数打错而一直WA--于是模拟赛就只过了2道题(太弱了orz).时间到了后很快发现了脑残错误,终于A了C题.下午上完课回到宿舍看D题才发现D题水的不行,很快就A了.不过E和F就比较超出我现在知识范围了,暂时就先放下.第一次参加vir

Codeforces Round #379 (Div. 2) C. Anton and Making Potions(二分)

Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass to the next level he has to prepare npotions. Anton has a special kettle, that can prepare one potions in x seconds. Also, he knows spells of two typ

Codeforces Round #379 (Div. 2) D. Anton and Chess(模拟)

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead. The first task he faced is to check

codeforce 380(div.2)

A B 略 C:二分,贪心 设d(i, v)为 剩余油量为v时,车开距离i 所需要的最小时间,使用线性规划不难算出: if v < i return INF; //无法到达 if v > 2*i return i; if i <= v <= 2*i return LL(3)*i - v; 那么一辆车开到终点的最短时间等于 ∑d(g[i]-g[i-1], v)这样只需要二分能开到终点的最小油量,取其中价格最低的即是答案代码如下: 1 #include <cstdio> 2