8VC Venture Cup 2016 - Final Round (Div. 2 Edition)
import java.io.*;
import java.util.*; public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner (System.in);
int r = cin.nextInt ();
int c = cin.nextInt ();
int n = cin.nextInt ();
int k = cin.nextInt ();
int[][] a = new int[12][12];
for (int i=0; i<n; ++i) {
int x = cin.nextInt ();
int y = cin.nextInt ();
a[x][y] = 1;
}
int ans = 0;
for (int sx=1; sx<=r; ++sx) {
for (int lx=0; sx+lx<=r; ++lx) {
for (int sy=1; sy<=c; ++sy) {
for (int ly=0; sy+ly<=c; ++ly) {
int cnt = 0;
for (int i=sx; i<=sx+lx; ++i) {
for (int j=sy; j<=sy+ly; ++j) {
if (a[i][j] == 1) cnt++;
if (cnt >= k) break;
}
}
if (cnt >= k) ans++;
}
}
}
}
System.out.println (ans);
}
}
相对位置不变,0的位置任意,当做不存在.查看每个点到对应的点是否能一起移动到.
#include <bits/stdc++.h> const int N = 2e5 + 5;
int a[N], b[N];
int n; int main() {
scanf ("%d", &n);
int tot1 = 0, tot2 = 0;
for (int x, i=0; i<n; ++i) {
scanf ("%d", &x);
if (x != 0) a[tot1++] = x;
}
for (int x, i=0; i<n; ++i) {
scanf ("%d", &x);
if (x != 0) b[tot2++] = x;
}
int s = 0;
for (; s<tot2; ++s) {
if (b[s] == a[0]) break;
}
bool flag = true;
for (int i=0; i<tot1; ++i) {
if (b[(i+s)%(n-1)] != a[i]) {
flag = false; break;
}
}
if (flag) puts ("YES");
else puts ("NO"); return 0;
}
数位DP C - XOR Equation
题意:求多少对(a, b)满足a + b == s && a ^ b == x
分析:s和x拆分成二进制,dp[i][4][0/1]第一维是二进制长度,第二维是a和b二进制下第i位的方案(01,10,00,11),第三维是是否第i位会进位,递推一下能够在log(N)解决.
#include <bits/stdc++.h> typedef long long ll;
int s[41], x[41];
ll dp[41][4][2]; void updata(int id) {
if (s[id] == x[id]) {
if (s[id] == 0) { //0 0
if (id == 0) {
dp[id][2][0] = dp[id][3][1] = 1;
}
else {
for (int j=0; j<4; ++j) {
dp[id][2][0] += dp[id-1][j][0];
dp[id][3][1] += dp[id-1][j][0];
}
}
}
else { //1 1
if (id == 0) {
dp[id][0][0] = dp[id][1][0] = 1;
}
else {
for (int j=0; j<4; ++j) {
dp[id][0][0] += dp[id-1][j][0];
dp[id][1][0] += dp[id-1][j][0];
}
}
}
}
else {
if (s[id] == 0) { //0 1
for (int j=0; j<4; ++j) {
dp[id][0][1] += dp[id-1][j][1];
dp[id][1][1] += dp[id-1][j][1];
}
}
else { //1 0
for (int j=0; j<4; ++j) {
dp[id][2][0] += dp[id-1][j][1];
dp[id][3][1] += dp[id-1][j][1];
}
}
}
} int main() {
ll S, X; std::cin >> S >> X;
if (X == 0) {
puts ("1"); return 0;
}
bool same = false;
if (S == X) same = true;
int n = 0, m = 0;
while (S) {
if (S & 1) s[n++] = 1;
else s[n++] = 0;
S >>= 1;
}
while (X) {
if (X & 1) x[m++] = 1;
else x[m++] = 0;
X >>= 1;
}
int len = std::max (n, m);
for (int i=0; i<len; ++i) {
updata (i);
}
ll ans = 0;
for (int i=0; i<4; ++i) {
ans += dp[len-1][i][0];
}
if (same && ans >= 2) ans -= 2;
std::cout << ans << '\n'; return 0;
}
树状数组 D - Factory Repairs
单点更新以及区间求和,不过不知道该点是k天前还是后,树状数组开二维.
import java.io.*;
import java.util.*; public class Main {
static int[][] C;
static int[] A;
static int n;
static void updata(int i, int j, int x) {
while (i <= n) {
C[i][j] += x;
i += i & -i;
}
}
static int query(int i, int j) {
int ret = 0;
while (i > 0) {
ret += C[i][j];
i -= i & -i;
}
return ret;
}
static int min(int a, int b) {
if (a < b) return a;
else return b;
}
public static void main(String[] args) {
Scanner cin = new Scanner (new BufferedInputStream (System.in));
n = cin.nextInt ();
int k = cin.nextInt ();
int a = cin.nextInt ();
int b = cin.nextInt ();
int q = cin.nextInt ();
C = new int[n+5][2];
A = new int[n+5];
for (int i=0; i<q; ++i) {
int op = cin.nextInt ();
if (op == 1) {
int d = cin.nextInt ();
int e = cin.nextInt ();
int tmp = A[d]; A[d] += e;
updata (d, 0, min (b, A[d]) - min (b, tmp));
updata (d, 1, min (a, A[d]) - min (a, tmp));
}
else {
int p = cin.nextInt ();
int ans = query (p - 1, 0) + query (n, 1) - query (p+k-1, 1);
System.out.println (ans);
}
}
}
}
贪心+双端队列 E - Package Delivery
题意:m个加油站,每单位油有价格,汽车有油的容量,初始满油,问到终点最少消费多少.
分析: 假设起点也算是一个加油站,免费,且汽车初始空油,那么每一个加油站最多能给汽车充n单位油,那么到下一个加油站要选择之前加油站价格最少的加油,这里用双端队列维护最低价格的加油站.
#include <bits/stdc++.h> typedef long long ll;
const int N = 2e5 + 5;
int dque[N];
std::pair<int, int> station[N];
int d, n, m; int main() {
scanf ("%d%d%d", &d, &n, &m);
for (int i=0; i<m; ++i) {
scanf ("%d%d", &station[i].first, &station[i].second);
}
station[m++] = std::make_pair (0, 0);
station[m++] = std::make_pair (d, 0);
std::sort (station, station+m);
for (int i=0; i<m-1; ++i) {
if (station[i].first + n < station[i+1].first) {
puts ("-1"); return 0;
}
}
int qs = 0, qe = 0, now = 0; ll ans = 0;
for (int i=0; i<m; ++i) {
int x = station[i].first, p = station[i].second;
while (qs < qe && station[dque[qs]].first + n < x) {
ans += 1ll * (station[dque[qs]].first + n - now) * station[dque[qs]].second;
now = station[dque[qs]].first + n;
qs++;
}
ans += 1ll * (x - now) * station[dque[qs]].second;
now = x;
while (qs < qe && station[dque[qe-1]].second > p) {
qe--;
}
dque[qe++] = i;
}
std::cout << ans << '\n'; return 0;
}
树形DP F - Preorder Test
题意:选择一条k长的DFS先序遍历路径,使得其中的最小值最大
分析:二分枚举最小值,然后枚举每个点出发的大于最小值的路径长度.求路径长度用到树形DP
首先down[u]表示u的子树下最多能走的路径长度,然后第二次dp[u]表示以u为根节点下最多能走的路径长度,up是u以上完整的最大长度
#include <bits/stdc++.h> const int N = 2e5 + 5;
struct DP {
int sum, mx;
DP operator + (const DP &rhs) const {
return DP {sum + rhs.sum, std::max (mx, rhs.mx)};
}
};
int a[N], total[N], down[N], dp[N];
bool good[N];
std::vector<int> edge[N];
int n, k; void DFS(int u, int fa) {
total[u] = 1;
int sum = 0, mx = 0;
for (auto v: edge[u]) {
if (v == fa) continue;
DFS (v, u);
total[u] += total[v];
if (down[v] == total[v]) {
sum += total[v];
} else {
mx = std::max (mx, down[v]);
}
}
down[u] = sum + mx + 1;
if (!good[u]) down[u] = 0;
} void DFS2(int u, int fa, int up) { //learn from tourist
std::vector<int> children;
int sum = 0, mx = 0;
for (auto v: edge[u]) {
if (v == fa) continue;
if (down[v] == total[v]) {
sum += down[v];
} else {
mx = std::max (mx, down[v]);
}
children.push_back (v);
}
if (up == n - total[u]) {
sum += up;
} else {
mx = std::max (mx, up);
}
dp[u] = sum + mx + 1;
if (!good[u]) dp[u] = 0; int sz = children.size ();
if (sz == 0) return ;
std::vector<DP> predown (sz + 1);
predown[0] = {0, 0};
for (int i=0; i<sz; ++i) {
int v = children[i];
predown[i+1] = predown[i];
if (down[v] == total[v]) {
predown[i+1].sum += down[v];
} else {
predown[i+1].mx = std::max (predown[i+1].mx, down[v]);
}
}
std::vector<DP> sufdown (sz + 1);
sufdown[sz] = {0, 0};
for (int i=sz-1; i>=0; --i) {
int v = children[i];
sufdown[i] = sufdown[i+1];
if (down[v] == total[v]) {
sufdown[i].sum += down[v];
} else {
sufdown[i].mx = std::max (sufdown[i].mx, down[v]);
}
}
for (int i=0; i<sz; ++i) {
int v = children[i];
DP now = predown[i] + sufdown[i+1];
if (up == n - total[u]) {
now.sum += up;
} else {
now.mx = std::max (now.mx, up);
}
int new_up = now.sum + now.mx + 1;
if (!good[u]) new_up = 0;
DFS2 (v, u, new_up);
}
} bool check(int v) {
for (int i=1; i<=n; ++i) good[i] = (a[i] >= v);
DFS (1, 0);
DFS2 (1, 0, 0);
for (int i=1; i<=n; ++i) {
if (dp[i] >= k) return true;
}
return false;
} int main() {
scanf ("%d%d", &n, &k);
for (int i=1; i<=n; ++i) {
scanf ("%d", a+i);
}
for (int u, v, i=1; i<n; ++i) {
scanf ("%d%d", &u, &v);
edge[u].push_back (v);
edge[v].push_back (u);
}
int low = 0, high = 1000005;
while (low < high) {
int mid = (low + high + 1) >> 1;
if (check (mid)) {
low = mid;
} else {
high = mid - 1;
}
}
printf ("%d\n", low); return 0;
}
8VC Venture Cup 2016 - Final Round (Div. 2 Edition)的更多相关文章
- 8VC Venture Cup 2016 - Final Round (Div. 1 Edition) E - Preorder Test 树形dp
E - Preorder Test 思路:想到二分答案了之后就不难啦, 对于每个答案用树形dp取check, 如果二分的值是val, dp[ i ]表示 i 这棵子树答案不低于val的可以访问的 最多 ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A
A. Orchestra time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组
D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学
C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)B. sland Puzzle 水题
B. sland Puzzle 题目连接: http://www.codeforces.com/contest/635/problem/B Description A remote island ch ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) A. Orchestra 水题
A. Orchestra 题目连接: http://www.codeforces.com/contest/635/problem/A Description Paul is at the orches ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题
http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) D - Travel Card
D - Travel Card 思路:dp,类似于单调队列优化. 其实可以写的更简单... #include<bits/stdc++.h> #define LL long long #de ...
- Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition)A 水 B 二分 C并查集
A. Petr and a calendar time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- swift选择类或结构体
按照通用的准则,当符合一条或多条以下条件时,请考虑构建结构体: 结构体的主要目的是用来封装少量相关简单数据值. 有理由预计一个结构体实例在赋值或传递时,封装的数据将会被拷贝而不是被引用. ? 任何在结 ...
- EF – 1.模式
3种数据库 code first model first database first 创建EF http://www.cnblogs.com/tangge/p/3834578.htm ...
- CSS3–1.css3 新增选择器
1.后代级别选择器 2.同辈级别选择器 3.伪类选择器 4.属性选择器 5.UI伪类选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- MVC – 9.mvc整体请求流程
1.请求管道 2~5微软自己的验证,我们一般不用. 在全局配置文件中-已经配置一个路由过滤器-为第7个事件注册了路由方法 1.在application_start中向静态路由表注册了路由数据,在管 ...
- android 屏幕旋转
转自:http://blog.csdn.net/oyzhizhong/article/details/8131799 屏是LANDSCAPE的,要让它默认显示为PORTRAIT. 1.kernel里要 ...
- Redis简介、与memcached比较、存储方式、应用场景、生产经验教训、安全设置、key的建议、安装和常用数据类型介绍、ServiceStack.Redis使用(1)
1.NOSQL简介 nosql的产生并不是要彻底的代替关系型数据库,而是作为传统关系型数据库的一个补充. Facebook和360使用Cassandra来存储海量社交数据 Twitter在其url抓取 ...
- Validform 学习笔记---基础知识整理
面对表单的验证,自己写大量的js毕竟不是一个明智的做法.不仅仅是代码很长而且不便于梳理.Validform就是一款开源的第三方验证js的控件,通过添加相应的js以及css能够有效的验证表单,维护起来也 ...
- oracle限制ip訪問
我這oracle版本為11.2.0.4,裝的GRID,所以在grid用戶下編輯sqlnet.ora 1.cd /grid/product/11.2.0/network/admin 2.編輯sqlne ...
- 动软MySQL存储过程模板
<#@ template language="c#" HostSpecific="True" #><#@ output extension= ...
- Uncaught ReferenceError: console is not defined
今天写javascript代码遇到了这个极其神奇的问题,居然报错说内置的console不存在,而且后来我换成了alert也不行.照例说这些都是js代码内置的东西不应该出现这种错误.不过百度之发现貌似没 ...