Codeforces Round #404 (Div. 2)——ABCDE
A.map裸题
#include <bits/stdc++.h> using namespace std; map <string, int> p; string s[] = {
"Tetrahedron",
"Cube",
"Octahedron" ,
"Dodecahedron",
"Icosahedron"
}; int ss[] = {, , , , }; int main() {
ios::sync_with_stdio(false);
for(int i = ;i < ;i ++) p[s[i]] = ss[i];
int n, m = ;
string str;
cin >> n;
while(n --) cin >> str, m += p[str];
cout << m;
return ;
}
B.贪心,先上的课尽早下课,后上的课尽量晚点上课
#include <bits/stdc++.h> using namespace std; int n, l, r, l1, l2, r1, r2, ans; int main() {
ios::sync_with_stdio(false);
r1 = r2 = 1e9;
cin >> n;
while(n --) {
cin >> l >> r;
l1 = max(l1, l);
r1 = min(r1, r);
}
cin >> n;
while(n --) {
cin >> l >> r;
l2 = max(l2, l);
r2 = min(r2, r);
}
ans = max(max(, l2 - r1), max(, l1 - r2));
cout << ans;
return ;
}
C.先讨论一下
如果n <= m ,那么前 n - 1 天都是当天补满,然后第 n 天被吃光
当 n > m , 前 m 天肯定吃不完的
然后第 m + 1 天鸟走之后(没吃完)下一天再补上,就只有 n - 1 了
第 m + 2 天鸟走之后(没吃完)再补上,就只有 n - 1 - 2 了
......
第 ans 天鸟走之后仓库已经被吃光了
那么就有 n - (ans - m) * (ans - m - 1) / 2 - ans <= 0
令 t = ans - m , 化简以后就有 t * t + t >= 2 * (n - m)
这时候可以选择二分,右边界肯定是sqrt(2 * 1e18)啦,再大会爆long long
当然可以直接开根,然后左右小小调整一下就能出来结果
#include <bits/stdc++.h> using namespace std; int main() {
unsigned long long n, m, ans;
cin >> n >> m;
if(n <= m) cout << n;
else {
n = (n - m) * , ans = sqrt(n);
while(ans * ans + ans >= n) ans --;ans ++;
while(ans * ans + ans < n) ans ++;
cout << ans + m;
}
return ;
}
D.为了避免重复,我们选择这样一个策略:
每当遇到一个左括号,我们计算包含这个左括号的合法序列数量加入ans
显然如果当前左括号左边有 L 个左括号(不含这个)
右边有 R 个右括号,那么对ans贡献为
sigma(C(L,i ) * C(R,i + 1) ),0 <= i < R
然后我们用一个范德蒙恒等式就变成了求 C(L + R ,R - 1)
剩下求组合数就O(nlogn)预处理,O(1)计算即可
#include <cstdio>
#include <iostream> const int Mod = 1e9 + ; char s[]; long long ans, fac[], v[]; int calc(long long x, int k = Mod - ) {
long long ret = ;
for(;k;k >>= , x = x * x % Mod)
if(k & ) ret = ret * x % Mod;
return ret;
} long long C(int n, int m) {
return fac[n] * v[m] % Mod * v[n - m] % Mod;
} int main() {
fac[] = v[] = ;
for(int i = ;i <= ;i ++)
fac[i] = fac[i - ] * i % Mod, v[i] = calc(fac[i]);
int l = , r = ;
scanf("%s", s);
for(int i = ;s[i];i ++) r += (s[i] == ')');
for(int i = ;s[i];i ++) {
if(s[i] == ')') r --;
else ans += C(l + r, r - ), ans %= Mod, l ++;
}
printf("%lld", ans);
return ;
}
E.原序列为 1 - n 的排列,每次操作交换两数位置,并操作后求出逆序对个数
转化为二维模型就能很熟悉的联系到cdq上,于是 cdq + 树状数组 解决即可
当然树套树也可做,不再分析思路
用cdq来做呢,我们每次计算这次操作对逆序对数的改变量即为
( l, a[r] )左上和右下矩阵中点的个数 + ( r, a[l] ) 左上和右下矩阵中点的个数
- ( l, a[l] ) 左上和右下矩阵中点的个数 - ( r, a[r] ) 左上和右下矩阵中点的个数
当然我们需要注意避免逆序对的重复计算
另外需要注意我们计算的是改变量,所以答案是需要累加的
#include <bits/stdc++.h> #define lb(x) (x & (-x))
#define rep(i, j, k) for(int i = j;i <= k;i ++) using namespace std; const int maxn = , maxm = maxn * ; struct node {
int x, y, op, bel, id; bool operator < (const node &a) const {
if(x != a.x) return x < a.x;
if(y != a.y) return y < a.y;
return id < a.id;
}
}q[maxm], q0[maxm]; int n, m, k, a[maxn], c[maxn];
long long ans[maxn]; void add(int i, int x) {
while(i <= n) c[i] += x, i += lb(i);
} int ask(int i) {
int ret = ;
while(i > ) ret += c[i], i -= lb(i);
return ret;
} void solve(int l, int r) {
if(l == r) return;
int mid = (l + r) >> , cnt = ;
solve(l, mid), solve(mid + , r);
rep(i, l, mid) if(q[i].op == || q[i].op == -) q0[++ cnt] = q[i];
rep(i, + mid, r) if(q[i].op != && q[i].op != -) q0[++ cnt] = q[i];
sort(q0 + , q0 + cnt + );
rep(i, , cnt) {
if(q0[i].op == || q0[i].op == -) add(q0[i].y, q0[i].op / );
else ans[q0[i].bel] += ask(q0[i].y) * q0[i].op;
}
rep(i, , cnt) if(q0[i].op == || q0[i].op == -) add(q0[i].y, q0[i].op / -);
} int main() {
ios::sync_with_stdio(false);
int l, r; cin >> n >> m;
rep(i, , n) a[i] = i, q[++ k] = (node){i, i, , , k};
rep(i, , m) {
cin >> l >> r;
if(l != r) {
q[++ k] = (node){l - , n, -, i, k};
q[++ k] = (node){n, a[l] - , -, i, k};
q[++ k] = (node){l - , a[l] - , , i, k};
q[++ k] = (node){l, a[l], -, , k};
q[++ k] = (node){r - , n, -, i, k};
q[++ k] = (node){n, a[r] - , -, i, k};
q[++ k] = (node){r - , a[r] - , , i, k};
q[++ k] = (node){r, a[r], -, , k};
q[++ k] = (node){l - , n, , i, k};
q[++ k] = (node){n, a[r] - , , i, k};
q[++ k] = (node){l - , a[r] - , -, i, k};
q[++ k] = (node){l, a[r], , , k};
q[++ k] = (node){r - , n, , i, k};
q[++ k] = (node){n, a[l] - , , i, k};
q[++ k] = (node){r - , a[l] - , -, i, k};
q[++ k] = (node){r, a[l], , , k};
swap(a[l], a[r]);
}
}
solve(, k);
rep(i, , m) {
ans[i] += ans[i - ];
cout << ans[i] << endl;
}
return ;
}
代码写的很丑很暴力,仅供参考
Codeforces Round #404 (Div. 2)——ABCDE的更多相关文章
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #404 (Div. 2) DE
昨晚玩游戏竟然不小心错过了CF..我是有多浪啊. 今天总算趁着下课时间补了,感觉最后两题还是挺有意思的,写个题解. D: 题目大意: 给出一个括号序列,问有多少个子序列 是k个'(' + k个')' ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
- Codeforces Round #404 (Div. 2) A,B,C,D,E 暴力,暴力,二分,范德蒙恒等式,树状数组+分块
题目链接:http://codeforces.com/contest/785 A. Anton and Polyhedrons time limit per test 2 seconds memory ...
- Codeforces Round #404 (Div. 2)(A.水,暴力,B,排序,贪心)
A. Anton and Polyhedrons time limit per test:2 seconds memory limit per test:256 megabytes input:sta ...
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- Codeforces Round #404 (Div. 2)A,B,C
A. Anton and Polyhedrons 题目链接:http://codeforces.com/contest/785/problem/A 智障水题 实现代码: #include<bit ...
随机推荐
- CodeForces - 557D Vitaly and Cycle(二分图)
Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- bzoj 1601 灌水
题目大意: 决定把水灌到n块农田,农田被数字1到n标记 把一块土地进行灌水有两种方法,从其他农田饮水,或者这块土地建造水库 建造一个水库需要花费wi,连接两块土地需要花费Pij. 计算所需的最少代价 ...
- JDK8中函数式流编程推荐
强烈推荐使用Java8中函数流API库来处理集合相关的数据,今天又看来一个项目代码里面用到来很多这样的处理,基本上可以解决大部分遍历问题.并且代码简洁清晰. JAVA8与lambda表达式 JDK8 ...
- js:正则表达
一:正则表达对象方法 1:compile()方法 //编译正则表达式 实例:在字符串中全局搜索“man”,并用“person”替换,然后通过compile()方法,改变正则表达式,用person替 ...
- [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Lambda表达式怎么写SQL中的in?
ambda表达式查询没有IN这个方法,可以变通一下,in查询的数组是否包含在映射对象里面的集合里 直接贴代码吧,一看就懂了 class Program { static void Main(strin ...
- SQL SERVER 获取给定时间段内的所有日期列表
declare @StartDate DATETIME = '2018/08/01'declare @EndDate DATETIME ='2018/09/27'SELECT CONVERT (VAR ...
- 【转】Linux GCC常用命令
转自:http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2287738.html 1简介 2简单编译 2.1预处理 2.2编译为汇编代码(Comp ...
- vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例
第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...
- 开始玩qt,使用代码修改设计模式生成的菜单
之前制作菜单时,不是纯代码便是用设计模式 直接图形化完成. 今天我就是想用代码修改已经存在的菜单项,如果是用代码生成的可以直接调用指针完成: 但通过设计模式完成的没有暴露指针给我,至少我没发现. 在几 ...