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 ...
随机推荐
- 机器学习笔记-----Fisher判别式
本文申明:本系列文章为本人原创,如有转载请注明文章原地址. 今天我们机器学习老师在说到周志华老师的<机器学习>这本书的时候,p60页讲到了LDA,但是其中的公式推导省略了很多,现在我来补充 ...
- 11月13日上午ajax返回数据类型为JSON数据的处理
ajax返回数据类型为JSON数据的处理 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- WeakReference
https://msdn.microsoft.com/en-us/library/ms404247(v=vs.110).aspx http://stackoverflow.com/questions/ ...
- c++11 中成员变量初始化的顺序
参考C++11FAQ https://www.chenlq.net/cpp11-faq-chs 11以后可以直接在类里面初始化成员变量,类似这样 class A { int a=1; const in ...
- [NHibernate]延迟加载
目录 写在前面 文档与系列文章 延迟加载 一个例子 总结 写在前面 上篇文章介绍了多对多关系的关联查询的sql,HQL,Criteria查询的三种方式.本篇文章将介绍nhibernate中的延迟加载方 ...
- EntityFramework 数据库的迁移
第一步:在程序包管理器控制台里: Enable-Migrations -ProjectName EF所在的项目名称 第二步:运行后会在字段生成Migrations文件夹,Migrations-> ...
- NoClassDefFoundError vs ClassNotFoundException
我们先来认识一下Error 和Exception, 两个都是Throwable类的直接子类. Javadoc 很好的说明了Error类: An Error is a subclass of Thro ...
- 每秒执行一个shell脚本(转载)
上周迁移了一台服务器,发现其中一个项目的数据没有更新,查询原服务器的数据,数据有更新,并找到了rsync服务,从其他服务器传输数据,那么如何找到这台服务器?因为是从远程传输到本地,而且不是很频繁, ...
- SQL SERVER 中 GO 的用法
用信号通知 Microsoft® SQL Server™ 实用工具一批 Transact-SQL 语句的结束.GO 不是 Transact-SQL 语句:而是可为 osql 和 isql 实用工具及 ...
- linux磁盘分区模式
linux磁盘分区模式 模式一:MBR 1)主分区不超过四个 2)单个分区容量最大2TB 模式二:GPT 1)主分区个数"几乎"没有限制(原因:在GPT的分区表中最多可以支持128 ...