Codeforces Round #138 (Div. 2) ACBDE
A.Parallelepiped
题意:给一个六面体三面的面积,求12条边的长度和。
题解:因为都是整数,设边长分别为a,b,c,面积为xyz,那么可设x=a*b,y=b*c,z=c*a,简单解方程就可以了。
#include <iostream>
#include <math.h>
using namespace std; int main()
{
int a, b, c;
int x, y, z;
while (cin >> x >> y >> z) {
a = sqrt(x * z / y);
b = x / a;
c = z / a;
cout << * (a + b + c) << endl;
}
return ;
}
B.Array
题意:给一个长度为N的数列,求一个连续子序列,包含K个不同的数。要求这个子序列不能有满足条件的子序列,但不要求长度是最小。
题解:从头开始,每得到一个数就放入set,并记录每一个数字出现的最后的位置。当set的大小等于k时,就是子序列结束的位置,因为要开始位置尽可能靠后(否则就存在求得序列的子序列也满足条件),其次每个数字至少出现一次,那么求last最小值即可。
#include <stdio.h>
#include <math.h>
#include <set>
#include <string.h>
using namespace std; int a[];
int last[]; int main()
{
int n, k;
while (~scanf("%d%d",&n, &k)) {
for (int i = ; i < n; ++i) scanf("%d", a + i);
memset(last, -, sizeof last);
set<int>s;
int l = n;
int r = -;
for (int i = ; i < n; ++i) {
s.insert(a[i]);
last[ a[i] ] = i;
if (s.size() == k) {
r = i;
break;
}
}
if (r == -) {
printf("-1 -1\n");
continue;
}
for (int i = ; i <= r; ++i) {
if (last[ a[i] ] != - && last[ a[i] ] < l) l = last[ a[i] ];
}
printf("%d %d\n", l + , r + );
}
return ;
}
C.Bracket Sequence
题意:给一个只含有()[]的字符串,求一个括号匹配且没有相互嵌套的连续子序列,要求'['的个数最多。
题解:dp。对于每一个字符,都希望向前找到最长的子串,那么最长子串含有'['一定也是最多的。设一个数组f[n],f[i]表示以字符i为最后一个字符的子序列能向前匹配到不能匹配的位置。f[i]=-1表示能匹配到第一个字符。f[i]=i表示子序列长度为0。当a[i]=a[f[i-1]],f[i]=f[ f[i-1]-1 ] 。ans[i]表示包含第i个字符的最长子序列含有多少个'['。

#include <stdio.h>
#include <string.h>
#include <stack>
#include <utility>
using namespace std;
char a[];
int f[];
int ans[]; int main()
{
while (~scanf("%s", a)) {
int len = strlen(a);
f[] = ans[] = ;
for (int i = ; i < len; ++i) {
if (f[i-] == -) {
f[i] = i;
ans[i] = ;
} else if (a[ f[i-] ] == '(' && a[i] == ')') {
f[i] = f[i - ] - ; ans[i] = ans[i - ];
if (f[i] != -) {
ans[i] += ans[ f[i] ];
f[i] = f[ f[i] ];
}
} else if (a[ f[i-] ] == '[' && a[i] == ']') {
f[i] = f[i - ] - ; ans[i] = ans[i - ] + ;
if (f[i] != -) {
ans[i] += ans[ f[i] ];
f[i] = f[ f[i] ];
}
} else {
f[i] = i;
ans[i] = ;
}
}
int maxn = ;
int pos = ;
for (int i = ; i < len; ++i) {
if (maxn < ans[i]) {
maxn = ans[i];
pos = i;
}
}
printf("%d\n", maxn);
for (int i = f[pos] + ; i <= pos; ++i) printf("%c", a[i]);
printf("\n");
}
return ;
}
D.Two Strings
题意:给2个字符串S和T,求S中所有和T相等的子串(不要求连续),能不能使用S中所有的字符至少一次。
题解:从前向后扫一遍求每个字符能匹配到最靠右的位置l,从后向前扫一遍求每个字符能匹配到最靠左的位置r,如果前面的能l>=r,证明该字符可以被用到。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std; const int N = ; char s[N], t[N];
int l[N], r[N];
int ok[];
int main()
{
while (scanf("%s%s", s, t) == ){
memset(ok, -, sizeof (ok));
int slen = strlen(s);
int tlen = strlen(t);
int pos = ;
bool flag = true;
for (int i = ; i < slen; ++i) {
if (pos < tlen && s[i] == t[pos]) {
ok[ s[i] ] = pos;
l[i] = pos++;
} else {
if (ok[ s[i] ] == -) {
flag = false;
}
l[i] = ok[ s[i] ];
}
}
if (!flag) {
puts("No");
continue;
}
memset(ok, -, sizeof (ok));
pos = tlen - ;
for (int i = slen - ; i >= ; --i) {
if (pos>= && s[i] == t[pos]) {
ok[ s[i] ] = pos;
r[i] = pos--;
} else {
if (ok[ s[i] ] == -) {
flag = false;
}
r[i] = ok[ s[i] ];
}
}
if (!flag) {
puts("No");
continue;
}
for (int i = ; i < slen; ++i) {
if (l[i] < r[i]) {
flag = false;
break;
}
}
if (!flag) puts("No");else puts("Yes");
}
return ;
}
E.Partial Sums
给一个长度为N的数组,做n次操作后求该数组。公式如下。
很容易想到通过矩阵快速幂解题,设转移矩阵F,通过A*(F^K)求的结果。然而(1 ≤ n ≤ 2000, 0 ≤ k ≤ 109),复杂度为n^3*logk,妥妥的超时了。
这个题的矩阵是特殊的矩阵,上三角矩阵(当然也可以是下三角),而且每个值都是1。
对于如下图的矩阵——上三角矩阵,且为带状矩阵。每个对角线值都相等。

对于这个类型的矩阵,只要知道了第一行的元素就可以推出整个矩阵的样子。那么存该矩阵的时候也可以只存一行。
两个这样的矩阵相乘,还为这样的矩阵。
于是可以考虑把矩阵压缩成一维,那么矩阵乘法的复杂度也从n^3降到n^2。
设A*B=C
可以推出
c[0][i]
=sigma(j=0..n-1)a[0][j]*b[j][i]
=sigma(j=0..i) a[0][j]*b[j][i](因为是上三角矩阵,下面的元素都为0)
=sigma(j=0..i) a[0][j]*b[0][i-j](因为是带状矩阵,b[j][i]==b[0][i-j],可以对照上面的图算一下)
对于此题,转移矩阵压缩为一维后,求得F^k。
然后对于A*F,可以通过F的性质求的F[i][j],即F[i][j]=F[0][j-i]。
(一份糟糕的代码)
#include <cstdio>
#include <cmath>
#include <vector>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat; const int M = ;
int n, k;
// A*B
mat mul(mat &A, mat &B)
{
mat C(, vec(n));
for (int i = ; i < n; ++i) {
for (int k = ; k <= i; ++k) {
C[][i] = (C[][i] + A[][k] * B[][i - k]) % M;
}
}
return C;
} // A^k
mat pow(mat A, int k)
{
mat B(, vec(n));
B[][] = ;
while (k > ) {
if (k & ) B = mul(B, A);
A = mul(A, A);
k >>= ;
}
return B;
} void solve()
{
mat f(, vec(n));
for (int i = ; i < n; ++i) {
f[][i] = ;
}
f = pow(f, k);
mat A(, vec(n));
for (int i = ; i < n; ++i) cin >> A[][i];
cout << A[][];
for (int i = ; i < n; ++i) {
ll tmp = ;
for (int j = ; j <= i; ++j) {
tmp = (tmp + A[][j] * f[][i - j]) % M;
}
cout << " " << tmp;
}
} int main()
{
scanf("%d%d", &n, &k);
solve();
return ;
}
Codeforces Round #138 (Div. 2) ACBDE的更多相关文章
- Codeforces Round #138 (Div. 2)
A. Parallelepiped 枚举其中一边,计算其他两条边. B. Array 模拟. C. Bracket Sequence 栈. D. Two Strings \(pre[i]\)表示第i个 ...
- Codeforces Round #138 (Div. 2) A. Parallelepiped
A. Parallelepiped time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #138 (Div. 1)
A 记得以前做过 当时好像没做对 就是找个子串 满足括号的匹配 []最多的 开两个栈模拟 标记下就行 #include <iostream> #include<cstring> ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- jstack和线程dump分析
转自:http://jameswxx.iteye.com/blog/1041173 一:jstack jstack命令的语法格式: jstack <pid>.可以用jps查看java进程 ...
- Android ExpandableListView 带有Checkbox的简单应用
expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...
- cocos2dx addchild坐标问题
a.addchild(b); 会把a->getBoundingBox矩形的左下角坐标点和b的锚点贴合在一起. ----- 其他引擎默认不是这样的,所以再跨平台导数据的时候,要注意这些细微的差别 ...
- 数据库存储安全之(MD5+盐)加密
一般系统数据库密码加密方式: MD5后存入数据库 SHA1 Hash后存入数据库 缺点:黑客可以通过密码暴力破解获取密码信息,具体做法是将常用密码进行Hash后做成一个字典, 破解的时候,只需要查字典 ...
- node.js 异步式I/O 与事件驱动
Node.js 最大的特点就是异步式 I/O(或者非阻塞 I/O)与事件紧密结合的编程模式.这种模式与传统的同步式 I/O 线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一 ...
- android 页面滑动 ViewFlipper,OnGestureListener,OnTouchListener
public class Main extends Activity implements OnGestureListener, OnTouchListener { // 一般不直接使用ViewAni ...
- Android开发之MediaPlayer类
官网关于MediaPlayer类的使用简介:
- Android开发之动画(转)
activity跳转的过渡效果,很漂亮,很全 注意,切换方法overridePendingTransition只能在startActivity和finish方法之后调用. 第一个参数为第一个Activ ...
- Spring AOP--返回通知,异常通知和环绕通知
在上篇文章中学习了Spring AOP,并学习了前置通知和后置通知.地址为:http://www.cnblogs.com/dreamfree/p/4095858.html 在本文中,将继续上篇的学习, ...
- 单点登录系统(SSO)的开发思路
单点登录并不是一个新鲜的玩意儿,比较官方的解释是企业业务整合的解决方案之一,通俗来讲SSO就是一个通用的用户中心,国内比较流行的UCenter就是一套单点登录解决方案.而近期以CSDN明文存储用户密码 ...