CodeForces Round #250 Div2
A. The Child and Homework
注意仔细读题,WA了好多次,=_=
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- const int maxn = ;
- char s[][maxn];
- int l[], r[];
- bool cmp(int a, int b) { return l[a] < l[b]; }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- for(int i = ; i < ; i++) r[i] = i;
- for(int i = ; i < ; i++) scanf("%s", s[i]);
- for(int i = ; i < ; i++) { l[i] = strlen(s[i]); l[i] -= ; }
- sort(r, r + , cmp);
- int ans = -;
- if(l[r[]] * <= l[r[]]) ans = r[];
- if(l[r[]] >= l[r[]] * ) { if(ans == -) ans = r[]; else ans = ; }
- if(ans == -) ans = ;
- printf("%c\n", 'A' + ans);
- return ;
- }
代码君
B. The Child and Set (贪心)
把那些lowbit都算出来,然后从大到小排个序,依次往里面填就好了。
- #include <cstdio>
- #include <algorithm>
- using std::sort;
- const int maxn = + ;
- int lowbit[maxn], r[maxn], ans[maxn], cnt;
- bool cmp(int a, int b) { return lowbit[a] > lowbit[b]; }
- int main()
- {
- int s, n;
- scanf("%d%d", &s, &n);
- for(int i = ; i <= n; i++) { r[i] = i; lowbit[i] = i & (-i); }
- sort(r + , r + + n, cmp);
- int t = ;
- for(int i = ; i <= n; i++)
- {
- if(t == s) break;
- if(lowbit[r[i]] + t <= s) { t += lowbit[r[i]]; ans[cnt++] = r[i]; }
- }
- if(t != s) { puts("-1"); return ; }
- printf("%d\n%d", cnt, ans[]);
- for(int i = ; i < cnt; i++) printf(" %d", ans[i]);
- puts("");
- return ;
- }
代码君
C. The Child and Toy (贪心)
最优的删点方式就是从最大的点开始删起。
不妨从边的角度考虑更为方便,最终没有任意两个点事连通的,所以每条边都会被删去。考虑边(u, v),它被删去的时候贡献的一定是uv中较小的权值。
所以算法就是,读进每条边累加它权值较小的那个端点的权值即可。
- #include <cstdio>
- #include <algorithm>
- using std::min;
- const int maxn = + ;
- const int maxm = + ;
- int a[maxn], u[maxm], v[maxm];
- int main()
- {
- int n, m, ans = ;
- scanf("%d%d", &n, &m);
- for(int i = ; i <= n; i++) scanf("%d", &a[i]);
- for(int i = ; i < m; i++) { scanf("%d%d", &u[i], &v[i]); ans += min(a[u[i]], a[v[i]]); }
- printf("%d\n", ans);
- return ;
- }
代码君
D. The Child and Zoo (贪心 含秩并查集 最大生成树)
给每条边赋一个权值,为两端顶点权值较小的那个,然后将这些边从大到小排序。
一开始图是空的,每加进一条权值为w的边可能会将两个连通分量连通起来,那么对于分别位于这两个连通分量中的(u, v),f(u, v) = w,而这样的f(u, v)一共有 p * q个,其中pq分别为这两个连通分量的秩。
- #include <cstdio>
- #include <algorithm>
- using namespace std;
- const int maxn = + ;
- int a[maxn], u[maxn], v[maxn], w[maxn], r[maxn], cnt[maxn];
- int pa[maxn];
- int findset(int x) { return x == pa[x] ? x : pa[x] = findset(pa[x]); }
- bool cmp(int a, int b) { return w[a] > w[b]; }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- int n, m;
- scanf("%d%d", &n, &m);
- for(int i = ; i <= n; i++) scanf("%d", &a[i]);
- for(int i = ; i <= m; i++) { scanf("%d%d", &u[i], &v[i]); w[i] = min(a[u[i]], a[v[i]]); }
- for(int i = ; i <= n; i++) { pa[i] = i; cnt[i] = ; }
- for(int i = ; i <= m; i++) r[i] = i;
- sort(r + , r + + m, cmp);
- double ans = 0.0;
- int c = n;
- for(int i = ; i <= m && c > ; i++)
- {
- int px = findset(u[r[i]]);
- int py = findset(v[r[i]]);
- if(px != py)
- {
- ans += (double) cnt[px] * cnt[py] * w[r[i]];
- pa[px] = py; cnt[py] += cnt[px];
- c--;
- }
- }
- printf("%.6f\n", ans * / (n * 1.0 * (n - )));
- return ;
- }
代码君
E. The Child and Polygon (区间DP 叉积)
不妨将这些点按照逆时针顺序编号0 ~ n-1。d(i, j)表示多边形 i, i+1 ,..., j, i 的三角剖分的方法数。
则有状态转移方程 d(i, j) = sum{ d(i, k) * d(k, j) | i < k < j 且 线段ik在多边形内部 }
可以通过叉积来判断线段是否在多边形内部,具体就是判断 ji × jk 是否为正。
总时间复杂度为O(n3)
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const LL MOD = ;
- const int maxn = + ;
- int n;
- struct Point
- {
- LL x, y;
- Point(LL x = , LL y = ):x(x), y(y) {}
- }p[maxn];
- Point operator - (const Point& A, const Point& B)
- { return Point(A.x - B.x, A.y - B.y); }
- LL Cross(const Point& A, const Point& B)
- { return A.x * B.y - A.y * B.x; }
- LL d[maxn][maxn];
- LL dp(int L, int R)
- {
- if(d[L][R] != -) return d[L][R];
- LL& ans = d[L][R];
- if(R - L == ) return ans = ;
- ans = ;
- for(int i = L + ; i < R; i++)
- if(Cross(p[L] - p[R], p[i] - p[R]) > )
- ans = (ans + dp(L, i) * dp(i, R)) % MOD;
- return ans;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- scanf("%d", &n);
- LL x, y;
- for(int i = ; i < n; i++)
- {
- scanf("%I64d%I64d", &x, &y);
- p[i] = Point(x, y);
- }
- LL area = ;
- for(int i = ; i + < n; i++)
- area += Cross(p[i] - p[], p[i+] - p[]);
- if(area < ) reverse(p, p + n);
- memset(d, -, sizeof(d));
- printf("%I64d\n", dp(, n - ));
- return ;
- }
代码君
最后看了下Div1的两道题:
D. The Child and Sequence (线段树 单点修改)
动态询问连续子序列的和。操作有 单点修改 和 将 连续区间的每个数都模上x
重点说一下取模的操作,可以维护一个区间的最大值,当区间的最大值小于x的时候,那么便不用取模了。
而且每个数每次取模以后的值至少要减半,所以每个数取模的次数不会太多。
balabala...
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- typedef long long LL;
- const int maxn = + ;
- int n, m, qL, qR, p, op;
- LL v;
- LL sumv[maxn << ], maxv[maxn << ];
- void pushup(int o)
- {
- sumv[o] = sumv[o<<] + sumv[o<<|];
- maxv[o] = max(maxv[o<<], maxv[o<<|]);
- }
- void build(int o, int L, int R)
- {
- if(L == R) { scanf("%I64d", &sumv[o]); maxv[o] = sumv[o]; return; }
- int M = (L + R) / ;
- build(o<<, L, M);
- build(o<<|, M+, R);
- pushup(o);
- }
- void update1(int o, int L, int R)
- {
- if(L == R) { sumv[o] = maxv[o] = v; return; }
- int M = (L + R) / ;
- if(p <= M) update1(o<<, L, M);
- else update1(o<<|, M+, R);
- pushup(o);
- }
- void update2(int o, int L, int R)
- {
- if(maxv[o] < v) return;
- if(L == R) { sumv[o] %= v; maxv[o] %= v; return; }
- int M = (L + R) / ;
- if(qL <= M) update2(o<<, L, M);
- if(qR > M) update2(o<<|, M+, R);
- pushup(o);
- }
- LL query(int o, int L, int R)
- {
- if(qL <= L && qR >= R) return sumv[o];
- int M = (L + R) / ;
- LL ans = ;
- if(qL <= M) ans += query(o<<, L, M);
- if(qR > M) ans += query(o<<|, M+, R);
- return ans;
- }
- int main()
- {
- //freopen("in.txt", "r", stdin);
- cin >> n >> m;
- build(, , n);
- while( m-- )
- {
- scanf("%d", &op);
- if(op == )
- {
- scanf("%d%d", &qL, &qR);
- printf("%I64d\n", query(, , n));
- }
- else if(op == )
- {
- scanf("%d%d%I64d", &qL, &qR, &v);
- update2(, , n);
- }
- else
- {
- scanf("%d%I64d", &p, &v);
- update1(, , n);
- }
- }
- return ;
- }
代码君
E题看到用什么母函数,多项式求根,FFT,就打算把这道题放一放了,毕竟太弱Q_Q
CodeForces Round #250 Div2的更多相关文章
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- BZOJ 3625: [Codeforces Round #250]小朋友和二叉树
3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 304 Solved: 13 ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
- CodeForces Round 192 Div2
This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...
- Codeforces Round#250 D. The Child and Zoo(并差集)
题目链接:http://codeforces.com/problemset/problem/437/D 思路:并差集应用,先对所有的边从大到小排序,然后枚举边的时候,如果某条边的两个顶点不在同一个集合 ...
随机推荐
- jQuery scroll事件
scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素. $(document).ready(function () { //本人习惯 ...
- UITableView多选删除
设置一个在编辑状态下点击可改变图片的cell FileItemTableCell.h #import <UIKit/UIKit.h> @interface FileItemTableCel ...
- ***Jquery下Ajax与PHP数据交换
一.前台传递字符串变量,后台返回字符串变量(非json格式) Javascript代码: 这里,为了解决Ajax数据传递出现的汉字乱码,在字符串传递之前,使用javascript函数escape()对 ...
- poj 3317 Stake Your Claim 极大极小搜索
思路:为了方便,当c1>c2时将0变为1,1变为0. 空格最多有10个,每个空格有3个状态,如果不状态压缩,会TLE的.所以最多有3^10种情况 代码如下: #include<iostre ...
- hdu 4726 Kia's Calculation
思路:刚开始想复杂了. 看解题报告后才知道这题挺简单的,看来还是要多训练啊!!! 单独处理首位的数字,不能为0.其他的就好处理了,从大到小依次找下去就可以了…… 代码如下: #include<i ...
- maven本地仓库.m2文件夹路径讲解
Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Depen ...
- lintcode:交换链表当中两个节点
题目 给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点.保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做. 注意事项 你需要交换两个节点而不是改变节点的权值 ...
- 读取本地excel发短信
package com.cmcc.zysoft.sellmanager.controller; import java.io.File; import java.io.FileInputStream; ...
- ps小技巧
一.加色与减色 电脑显示器和电视是加色法最常见的形式,而在油漆.颜料和彩色滤光片会用减色. 二.怎么把背景变成透明:其实就是抠图. 1.魔术棒+delete,缺点:应用于边界明显的图片,否则容差不好控 ...
- CentOS7区域设置
区域设置的配置文件在/etc/locale.conf,通过localectl命令进行设置: systemd服务在启动的时候读取区域配置文件,完成系统的设置. 命令的几个常用方法如下: 1 查看当前配置 ...