数学 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. js 中的基本类型和引用类型的区别

    js中的基本类型赋值之后,只有值相等的时候,二者才会相等,例如 var  a='123'; var b=a; console.log(a===b); 返回的是true ,说明他们是相等的, 此时改变a ...

  2. Android开发笔记之《JNI常用知识汇总》

    参考资料: Android Studio中NDK开发 : http://www.tuicool.com/articles/NBjQnyAndroid Studio使用新的Gradle构建工具配置NDK ...

  3. kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)

    问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的原因是什么? 3.kafka集群consumer和producer状 ...

  4. 学习Javascript

    分别归类为: javascript变量 javascript运算符 javascript数组 javascript流程语句 javascript字符串函数 javascript函数基础 javascr ...

  5. 初学Java 精简知识点总结

    面对Java丰富的知识资料,很多初学者难免觉得迷惘,该学什么,怎么去学?下面给大家讲Java基础知识做了精简总结,来帮助你梳理学习思路,赶快看看吧! 方法/步骤1 对象的初始化(1) 非静态对象的初始 ...

  6. Table

    Table tb = new Table();TableRow r = new TableRow(); TableCell c = new TableCell();c.Text = "Sta ...

  7. css 实现三角形 实现过程

     1.纯色的全等的三角形实现 下面的就是实际实现  没有宽高 只有边框 都是透明 根据箭头的方向 给边框方法加颜色  比如需要像右箭头 只需要给border-right-color:颜色值; 即可 c ...

  8. fatal: could not read Username for 'https://github.com': No such file or directo

    Git push origin master报错 fatal: could not read Username for 'https://github.com': No such file or di ...

  9. System.Properties和System.getenv区别

    网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...

  10. 【转载】借助GitHub托管你的项目代码

    PS:自己关注博客园有2年之久了,不久前才申请注册账号.GitHub也差不多一年多了,因英语水平刚刚及格,所以去GitHub没有博客园多,也是几个月前才注册了账号,前几天休息时看到 EdisonCho ...