数学 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. WMPlayer

    WMPlayer视频播放器,AVPlayer的封装,继承UIView,想怎么玩就怎么玩.支持播放mp4.m3u8.3gp.mov,网络和本地视频同时支持.全屏和小屏播放同时支持.自动感应旋转屏幕. 1 ...

  2. log4Net控制台输出

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  3. 我的SqlHelper类!

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. SQL Server 数据库查找重复记录的几种方法

    http://www.hanyu123.cn/html/c61/6790.html 一.查某一列(或多列)的重复值.(只可以查出重复记录的值,不能查出整个记录的信息) 例如:查找stuid,stuna ...

  5. NAT穿越

    1.NAT类型 目前主要的NAT类型有如下几种: 1)Full-cone NAT, also known as one-to-one NAT 一旦一个内网地址 (iAddr:iPort) 被映射到一个 ...

  6. SQL中SET和SELECT赋值的区别

    最近的项目写的SQL比较多,经常会用到对变量赋值,而我使用SET和SELECT都会达到效果. 那就有些迷惑,这两者有什么区别呢?什么时候哪该哪个呢? 经过网上的查询,及个人练习,总结两者有以下几点主要 ...

  7. 新一代记事本“Notepad++”个性化设置备份

    Notepad++是一套非常有特色的自由软件的纯文字编辑器(许可证:GPL),有完整的中文化接口及支援多国语言撰写的功能(UTF8 技术).它的功能比 Windows 中的 Notepad(记事簿)强 ...

  8. Linux串口中的超时设置

    在Linux下使用串口通信时,默认的阻塞模式是不实用的.而采用select或epoll机制的非阻塞模式,写代码有比较麻烦.幸好Linux的串口自己就带有超时机制. Linux下使用termios.h中 ...

  9. js倒计时,显示NaN天NaN时NaN分(或显示天时分)

    最近在开发跨平台的应用,在做秒杀功能时,倒计时出现了问题.默认在Chrome浏览器中运行,倒计时没出现问题.而在IE浏览器,火狐浏览器,safari浏览器上运行时,则显示NaN天NaN时NaN分(或显 ...

  10. [教程] 【玩转终端1:apt-get】

    进来工作比较清闲,所以写点东西,给喜欢折腾的朋友.本文及后面将要介绍的一些终端命令,其实对于玩过linux的人来说,是很基础的东西,我可能是班门弄斧了(拍砖的请轻点,有愿意补充/纠正的,本人求知不得) ...