数学 A - Infinite Sequence

等差数列,公差是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;
}

数学 B - Restoring Painting

题意: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;
}

数学 C - Money Transfers

题意: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;
}

set D - Tree Construction

题意:按照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;
}

DP E - Trains and Statistic

题意:第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)的更多相关文章

  1. Codeforces Round #353 (Div. 2) C Money Transfers

    题目链接: http://www.codeforces.com/contest/675/problem/C 题意: 给一个数组,每个数与他相邻的数相连,第一个与最后一个相连,每个数的数值可以左右移动, ...

  2. 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的最短距离之 ...

  3. Codeforces Round #353 (Div. 2) D. Tree Construction 二叉搜索树

    题目链接: http://codeforces.com/contest/675/problem/D 题意: 给你一系列点,叫你构造二叉搜索树,并且按输入顺序输出除根节点以外的所有节点的父亲. 题解: ...

  4. Codeforces Round #353 (Div. 2) C. Money Transfers (思维题)

    题目链接:http://codeforces.com/contest/675/problem/C 给你n个bank,1~n形成一个环,每个bank有一个值,但是保证所有值的和为0.有一个操作是每个相邻 ...

  5. Codeforces Round #353 (Div. 2) D. Tree Construction (二分,stl_set)

    题目链接:http://codeforces.com/problemset/problem/675/D 给你一个如题的二叉树,让你求出每个节点的父节点是多少. 用set来存储每个数,遍历到a[i]的时 ...

  6. 线段树+dp+贪心 Codeforces Round #353 (Div. 2) E

    http://codeforces.com/contest/675/problem/E 题目大意:有n个车站,每个车站只能买一张票,这张票能从i+1到a[i].定义p[i][j]为从i到j所需要买的最 ...

  7. 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 ...

  8. Codeforces Round #353 (Div. 2) D. Tree Construction 模拟

    D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...

  9. Codeforces Round #353 (Div. 2) C. Money Transfers 数学

    C. Money Transfers 题目连接: http://www.codeforces.com/contest/675/problem/C Description There are n ban ...

  10. Codeforces Round #353 (Div. 2) B. Restoring Painting 水题

    B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...

随机推荐

  1. java进行文件上传,带进度条

    网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...

  2. BZOJ3110: [Zjoi2013]K大数查询

    喜闻乐见的简单树套树= =第一维按权值建树状数组,第二维按下标建动态开点线段树,修改相当于第二维区间加,查询在树状数组上二分,比一般的线段树还短= =可惜并不能跑过整体二分= =另外bzoj上的数据有 ...

  3. SAX与DOM

    http://www.cnblogs.com/zhulin/archive/2012/05/03/2480962.html 在解析xml时(如浏览器解析html标签),主要存在两种方式:SAX模式和D ...

  4. C# Httpclient编程

    今天研究了一天C#如何添加cookie到httpcient里面,从而发请求时,能把cookie作为头部发出,最后发现根本加不进去. Httpclient的cookie是来自上一个请求的响应,httpc ...

  5. ActiveMQ支持的传输协议

    ------------------------------------------------------ ActiveMQ支持的client-broker通讯协议有:TCP.NIO.UDP.SSL ...

  6. VBA笔记(一)

    开启VBA编程环境--VBE 方法一:按<Alt+F11>组合建 方法二:查看代码 宏设置 当然启用宏的设置方式不同,宏的启动方式也不一样. 首先打开"office 按钮&quo ...

  7. 关于Spring中的<context:annotation-config/>配置

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  8. Android 网络编程

    HttpClient 发送get请求 创建一个客户端对象 HttpClient client = new DefaultHttpClient(); 创建一个get请求对象 HttpGet hg = n ...

  9. 聊聊Azure的安全性

    本来没打算写这篇博文,毕竟感觉太理论化,不像做技术的人应该写的东西,但是作为一名售前,发现很多不了解Azure的客户,上来的第一个问题竟然就是Azure如何保证客户数据的安全性,我清楚记得我第一次被问 ...

  10. XHR——XMLHttpRquest对象

    创建XMLHttpRequest对象 与之前众多DOM操作一样,创建XHR对象也具有兼容性问题:IE6及之前的版本使用ActiveXObject,IE7之后及其它浏览器使用XMLHttpRequest ...