Codeforces Round #353 (Div. 2)
等差数列,公差是0的时候特判
#include <bits/stdc++.h> typedef long long ll;
const int N = 1e5 + 5; int main() {
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
bool flag = true;
if (c == 0) {
if (a == b) {
flag = true;
} else {
flag = false;
}
} else {
int d = (b - a) / c;
if (d >= 0 && (b - a) % c == 0) {
flag = true;
} else {
flag = false;
}
}
if (flag) {
puts ("YES");
} else {
puts ("NO");
}
return 0;
}

题意:3*3的矩阵,已经填了a,b,c,d四个数字,问填完数后四个2*2的子矩阵的和相等的方案数,所有数字范围在[1, n].
分析:蛮有意思的题目,很明显中心的数字是公有的,?从上到下从左到右设为x1,x2,x3,x4x5,那么满足x1+a+b=x4+b+d -> x4 = x1 + (a - d),x2=x1+(b-c), x5=x1+(a+b-c-d),因为x2, x4, x5范围在[1, n],能得到一个x1的最小可行区间,然后*x3的可行区间(n).当然O(n)枚举也可以.
#include <bits/stdc++.h> typedef long long ll;
const int N = 1e5 + 5; int main() {
int n, a, b, c, d;
scanf ("%d%d%d%d%d", &n, &a, &b, &c, &d);
int d1 = a - d, d2 = b - c, d3 = a + b - c - d;
int l = 1, r = n;
l = std::max (l, 1 - d1);
r = std::min (r, n - d1); l = std::max (l, 1 - d2);
r = std::min (r, n - d2); l = std::max (l, 1 - d3);
r = std::min (r, n - d3);
if (l <= r) {
long long ans = 1ll * (r - l + 1) * n;
std::cout << ans << '\n';
} else {
puts ("0");
}
return 0;
}
题意:n个数字有正有负,总和0,相邻数字可以分配,问最小操作数使得所有数字变成0.
分析:如果一段长度为L数字总和为0,最多L-1次可以使得每个数字为0.将n个数字分成m段都是0的,那么答案是n-m,所以求cnt[k]最大,表示m最大(最后一组为-k+k).
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio (false);
std::cin.tie (0);
std::map<long long, int> mp;
int n;
std::cin >> n;
long long sum = 0;
int mx = 0;
for (int i=0; i<n; ++i) {
int x;
std::cin >> x;
sum += x;
mp[sum]++;
mx = std::max (mx, mp[sum]);
}
std::cout << n - mx << '\n';
return 0;
}
题意:按照BST建一棵树二叉树,问当前插入的点的父节点.
分析:set模拟平衡树,lower_bound查找位置.
#include <bits/stdc++.h> const int N = 1e5 + 5;
std::set<int> st;
std::map<int, int> left, right; int main() {
int n, x;
scanf ("%d", &n);
scanf ("%d", &x);
st.insert (x);
int ans;
for (int i=0; i<n-1; ++i) {
scanf ("%d", &x);
auto it = st.lower_bound (x);
if (it != st.end () && left.count (*it) == 0) {
left[*it] = x;
ans = *it;
} else {
--it;
right[*it] = x;
ans = *it;
}
st.insert (x);
printf ("%d ", ans);
} return 0;
}
题意:第i个车站能到[i+1, a[i]]的位置,p(i, j)表示从i到j最少搭几次车.求
分析:定义dp[i]表示的最小搭车数,从[i+1, n]中a[m]最大的dp[m]转移来,...
#include <bits/stdc++.h> const int N = 1e5 + 5;
int a[N];
long long dp[N];
std::pair<int, int> mx[N][20];
int n; void init_ST() {
for (int i=0; i<n; ++i) {
mx[i][0] = {a[i], i};
}
for (int j=1; (1<<j)<=n; ++j) {
for (int i=0; i+(1<<j)-1<n; ++i) {
mx[i][j] = std::max (mx[i][j-1], mx[i+(1<<(j-1))][j-1]);
}
}
} int query_max(int l, int r) {
int k = 0; while (1<<(k+1) <= r - l + 1) k++;
return std::max (mx[l][k], mx[r-(1<<k)+1][k]).second;
} int main() {
scanf ("%d", &n);
a[n-1] = n - 1;
for (int i=0; i<n-1; ++i) {
scanf ("%d", a+i);
a[i]--;
}
init_ST ();
long long ans = 0;
dp[n-1] = 0;
for (int i=n-2; i>=0; --i) {
int p = query_max (i + 1, a[i]);
dp[i] = dp[p] - (a[i] - p) + n - i - 1;
ans += dp[i];
}
printf ("%I64d\n", ans);
return 0;
}
Codeforces Round #353 (Div. 2)的更多相关文章
- Codeforces Round #353 (Div. 2) C Money Transfers
题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic 线段树+dp
题目链接: http://www.codeforces.com/contest/675/problem/E 题意: 对于第i个站,它与i+1到a[i]的站有路相连,先在求所有站点i到站点j的最短距离之 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树
题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...
- Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)
题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...
- Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)
题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...
- 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E
http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- Codeforces Round #353 (Div. 2) C. Money Transfers 数学
C. Money Transfers 题目连接: http://www.codeforces.com/contest/675/problem/C Description There are n ban ...
- Codeforces Round #353 (Div. 2) B. Restoring Painting 水题
B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...
随机推荐
- 【原】用css让一个容器水平垂直
这种css布局平时用的比较多,也是面试题常出的一个题,网上一搜一大丢,不过还是想自己总结一下. 这种方法比较多,本文只总结其中的几种,以便加深印象. 效果图都为这个: 方法一:position加mar ...
- XInitThreads与XLIB
XInitThreads函数通常需要尽早调用,一般要在XLIB的其他函数前调用 否则XLIB的函数可能会在调用时直接崩溃(多线程程序中) 最好的做法是,在main入口即调用XInitThreads函数
- css3 动画效果 定义和绑定执行
首先要定义一个动画效果 keyframes 关键字 这里动画效果执行完毕后 恢复本身的css样式 有的动画效果 移动到位置 要保持 就需要写好css 元素的位置 css里直接写 (这里是一般的 ...
- 探索ASP.NET MVC框架之路由系统
引言 对于ASP.NET MVC的路由系统相信大家肯定不陌生.今天我们就深入ASP.NET的框架内部来看一下路由系统到底是怎么通过我们给出的地址(例如:/Home/Index)解析出Controlle ...
- Bootstrap学习笔记
Bootstrap提供了一套响应式.移动设备优先的流式栅格系统. Bootstrap把一个容器或整个网页平均分成了12列. 栅格系统必须放在.container或container-fluid中 样式 ...
- 调整Virtual Box硬盘大小
我在Mac下使用Virtual Box安装Win7的虚拟机.因为之前装过Win7的32位版.现在因为机器内存升到8G,就可以划出4G来支持Win7虚拟机.所以就重新安装了Win7的64位版.在创建虚拟 ...
- 大熊君学习html5系列之------requestAnimationFrame(实现动画的另一种方案)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- 如何自定义iphone个性铃音
准备工作:itunes.(Netease Cloud Music).iphone 1.下载你想要的铃音原音乐: 2.打开itunes,向音乐库中添加刚刚下载的音乐: "文件"-&g ...
- Javascript中闭包问题(转载)
学习Javascript闭包(Closure) 作者: 阮一峰 日期: 2009年8月30日 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现 ...
- shell--4.echo和printf
1. echo (1) echo ,显示普通字符串 echo "HelloWorld" 打印:HelloWorld (2) \ ,显示转义字符串 echo "\" ...