Codeforces 364
A
第一题明显统计,注意0和long long(我WA,RE好几次)
/*
* Problem: A. Matrix
* Author: Shun Yao
*/ #include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h> #include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> //using namespace std; int a, n, b[4444], c[44444];
char s[4444]; int main(/*int argc, char **argv*/) {
int i, j, x;
long long ans; scanf("%d", &a);
scanf(" %s", s + 1);
n = strlen(s + 1);
for (i = 1; i <= n; ++i)
b[i] = s[i] - '0';
memset(c, 0, sizeof c);
ans = 0;
for (i = 1; i <= n; ++i) {
x = 0;
for (j = i; j <= n; ++j) {
x += b[j];
++c[x];
}
}
if (a) {
for (i = 1; i <= 40000 && i * i <= a; ++i)
if (a % i == 0 && a / i <= 40000)
ans += static_cast<long long>(c[i]) * c[a / i] * (i * i == a ? 1 : 2);
} else
ans = static_cast<long long>(c[0]) * (n * (n + 1) - c[0]);
printf("%I64d", ans); fclose(stdin);
fclose(stdout);
return 0;
}
B
用背包算出每个价值是否出现过,然后贪心即可。
/*
* Problem: B. Free Market
* Author: Shun Yao
*/ #include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h> #include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> //using namespace std; int n, d, c[55], f[500010]; int main(/*int argc, char **argv*/) {
int i, j, sum, ans1, ans2; scanf("%d%d", &n, &d);
sum = 0;
for (i = 1; i <= n; ++i) {
scanf("%d", c + i);
sum += c[i];
}
memset(f, 0, sizeof f);
f[0] = 1;
for (i = 1; i <= n; ++i)
for (j = sum; j >= c[i]; --j)
f[j] |= f[j - c[i]];
ans1 = 0;
ans2 = 0;
while (ans1 < sum) {
for (j = ans1 + d <= sum ? ans1 + d : sum; j > ans1; --j)
if (f[j])
break;
if (j <= ans1)
break;
ans1 = j;
++ans2;
}
printf("%d %d", ans1, ans2); fclose(stdin);
fclose(stdout);
return 0;
}
C
不懂证明,求解释。。。
D
做法是随机算法(没有想到啊。)
/*
* Problem: D. Ghd
* Author: Shun Yao
*/ #include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h> #include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> //using namespace std; const int MAXN = 1000010; int n;
long long a[MAXN], ans;
std::vector<long long> e, v; long long gcd(long long a, long long b) {
return !b ? a : gcd(b, a % b);
} int main(/*int argc, char **argv*/) {
int i, j, x; // freopen("D.in", "r", stdin);
// freopen("D.out", "w", stdout); scanf("%d", &n);
for (i = 1; i <= n; ++i)
scanf("%I64d", a + i);
srand((unsigned)time(0));
ans = 0;
while (clock() < 3000L) {
x = static_cast<long long>(rand()) * rand() % n + 1;
e.clear();
v.clear();
for (i = 1; static_cast<long long>(i) * i <= a[x]; ++i)
if (a[x] % i == 0) {
e.push_back(i);
if (static_cast<long long>(i) * i != a[x])
e.push_back(a[x] / i);
}
std::sort(e.begin(), e.end());
v.resize(e.size(), 0);
for (i = 1; i <= n; ++i)
++v[std::lower_bound(e.begin(), e.end(), gcd(a[x], a[i])) - e.begin()];
for (i = 0; i < static_cast<int>(e.size()); ++i) {
for (j = i + 1; j < static_cast<int>(e.size()); ++j)
if (e[j] % e[i] == 0)
v[i] += v[j];
if (v[i] + v[i] >= n)
ans = std::max(ans, e[i]);
}
}
printf("%I64d", ans); fclose(stdin);
fclose(stdout);
return 0;
}
E
二维分治。
/*
* Problem: E. Empty Rectangles
* Author: Shun Yao
*/ #include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <time.h> #include <map>
#include <set>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <bitset>
#include <utility>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional> //using namespace std; int n, m, k;
char s[2505][2505];
long long a[2505][2505]; inline long long sum(int r1, int c1, int r2, int c2) {
return a[r2][c2] - a[r1 - 1][c2] - a[r2][c1 - 1] + a[r1 - 1][c1 - 1];
} long long go(int r1, int c1, int r2, int c2) {
if (r1 == r2 && c1 == c2)
return sum(r1, c1, r2, c2) == k;
if (r2 - r1 > c2 - c1) {
long long cnt = 0;
int m = (r1 + r2) >> 1;
int up[k + 1], dw[k + 1];
for (int i = c1; i <= c2; i++) {
for (int x = 0; x <= k; x++)up[x] = m - r1 + 1, dw[x] = r2 - m;
for (int j = i; j <= c2; j++) {
for (int x = 0; x <= k; x++) {
while (up[x] && sum(m - up[x] + 1, i, m, j) > x)
--up[x];
while (dw[x] && sum(m + 1, i, m + dw[x], j) > x)
--dw[x];
}
for (int x = 0; x <= k; x++) {
cnt += (long long)(up[x] - (x ? up[x - 1] : 0)) * (dw[k - x] - (k - x ? dw[k - x - 1] : 0));
}
}
}
return cnt + go(r1, c1, m, c2) + go(m + 1, c1, r2, c2);
} else {
long long cnt = 0;
int m = (c1 + c2) >> 1;
int up[k + 1], dw[k + 1];
for (int i = r1; i <= r2; i++) {
for (int x = 0; x <= k; x++)up[x] = m - c1 + 1, dw[x] = c2 - m;
for (int j = i; j <= r2; j++) {
for (int x = 0; x <= k; x++) {
while (up[x] && sum(i, m - up[x] + 1, j, m) > x)up[x]--;
while (dw[x] && sum(i, m + 1, j, m + dw[x]) > x)dw[x]--;
}
for (int x = 0; x <= k; x++) {
cnt += (long long)(up[x] - (x ? up[x - 1] : 0)) * (dw[k - x] - (k - x ? dw[k - x - 1] : 0));
}
}
}
return cnt + go(r1, c1, r2, m) + go(r1, m + 1, r2, c2);
}
} int main() {
freopen("E.in", "r", stdin);
freopen("E.out", "w", stdout); scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++)scanf("%s", &s[i][1]);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
a[i][j] = a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1] + s[i][j] - '0';
}
}
printf("%I64d", go(1, 1, n, m)); fclose(stdin);
fclose(stdout);
return 0;
}
Codeforces 364的更多相关文章
- Codeforces #364 (Div. 2) D. As Fa(数学公式推导 或者二分)
数学推导的博客 http://codeforces.com/contest/701/problem/D 题目 推导的思路就是 : 让每个人乘车的时间相等 ,让每个人走路的时间相等. 在图上可以这么表 ...
- Codeforces #364 DIV2
~A题 A. Cards time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #364 (Div. 2)
这场是午夜场,发现学长们都睡了,改主意不打了,第二天起来打的virtual contest. A题 http://codeforces.com/problemset/problem/701/A 巨水无 ...
- Codeforces Round #364
http://codeforces.com/contest/701 A - Cards 水 // #pragma comment(linker, "/STACK:102c000000,102 ...
- Codeforces Round #364 (Div.2) D:As Fast As Possible(模拟+推公式)
题目链接:http://codeforces.com/contest/701/problem/D 题意: 给出n个学生和能载k个学生的车,速度分别为v1,v2,需要走一段旅程长为l,每个学生只能搭一次 ...
- Codeforces Round #364 (Div.2) C:They Are Everywhere(双指针/尺取法)
题目链接: http://codeforces.com/contest/701/problem/C 题意: 给出一个长度为n的字符串,要我们找出最小的子字符串包含所有的不同字符. 分析: 1.尺取法, ...
- 树形dp Codeforces Round #364 (Div. 1)B
http://codeforces.com/problemset/problem/700/B 题目大意:给你一棵树,给你k个树上的点对.找到k/2个点对,使它在树上的距离最远.问,最大距离是多少? 思 ...
- Codeforces Round #364 As Fast As Possible
二分思想,对所要花费的时间进行二分,再以模拟的形式进行验证是否可行. 使用二分法,可以将一个求最优解的问题转化为一个判定问题,优雅的暴力. #include<cstdio> #includ ...
- Codeforces Round #364 (Div. 2) B. Cells Not Under Attack
B. Cells Not Under Attack time limit per test 2 seconds memory limit per test 256 megabytes input st ...
随机推荐
- 项目用到异步加载头像LasyList
package com.leo.proforjob; import android.content.Context; import android.graphics.drawable.Drawable ...
- Shuffle和排序
MapReduce确保每个reducer的输入都按键排序.系统执行排序的过程——将map输出作为输入传给reducer——称为shuffle.shuffle属于不断被优化和改进的代码库的一部分,从许多 ...
- python小问题记录:
numpy.chararray.flatten chararray.flatten(order='C') Return a copy of the array collapsed into one d ...
- IIS Web服务器支持高并发设置
适用的IIS版本:IIS 7.0, IIS 7.5, IIS 8.0 适用的Windows版本:Windows Server 2008, Windows Server 2008 R2, Windows ...
- Hibernate开发之二 映射主键-
<class name="cn.itcast.e_hbm_id.User" table="user"> <!-- 映射主 ...
- "hadoop namenode -format"命令的作用和影响的文件
在hadoop部署好了之后是不能马上应用的,而是对配置的文件系统进行格式化.这里的文件系统,在物理上还未存在,或者用网络磁盘来描述更加合适:还有格式化,并不是传统意义上的磁盘清理,而是一些清除与准备工 ...
- NDK(14)Native的char*和Java的String相互转换
转自: http://www.cnblogs.com/canphp/archive/2012/11/13/2768937.html 首先确保C/C++源文件的字符编码是UTF-8与JAVA的class ...
- Linux系统下统计目录及其子目录文件个数
(1)查看某目录下文件的个数: ls -l |grep "^-"|wc -l 或 find ./company -type f | wc -l (2)查看某目录下文件的个数,包括子 ...
- git文件未改动pull的时候提示冲突
今天在mac下使用git工具,出现一个很奇怪的问题. 先声明当前工作目录是干净的,运行 git status 没有任何文件改动,且没有任何需要push的文件. 我执行 git pull 命令,直接提示 ...
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...