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的更多相关文章

  1. Codeforces Round #138 (Div. 2)

    A. Parallelepiped 枚举其中一边,计算其他两条边. B. Array 模拟. C. Bracket Sequence 栈. D. Two Strings \(pre[i]\)表示第i个 ...

  2. Codeforces Round #138 (Div. 2) A. Parallelepiped

    A. Parallelepiped time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  3. Codeforces Round #138 (Div. 1)

    A 记得以前做过 当时好像没做对 就是找个子串 满足括号的匹配 []最多的 开两个栈模拟 标记下就行 #include <iostream> #include<cstring> ...

  4. 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 ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. 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 ...

随机推荐

  1. 一个HexToInt的C/C++函数

    int  BetterVenca25(char* hex){   int res=0;   for(;*hex;hex++)   {  int d=toupper(*hex);      if(d & ...

  2. hadoop No FileSystem for scheme: hdfs

    http://stackoverflow.com/questions/17265002/hadoop-no-filesystem-for-scheme-file This is a typical c ...

  3. ArcGIS学习记录-Excel和Txt中XY点数据生成点Shape文件方法

    (一)Excel中XY点数据生成点Shape文件方法 1.Excel表如下:   2.点击ArcGIS中的"+"号按钮,添加数据.选择第一步中制作好的Excel文件,点击Add按钮 ...

  4. PHP-CGI, FastCGI, PHP-FPM的关系和区别

    Web server(apache, nginx) 接受到一个php请求后要解析php文件, 怎么解析呢, web server是C语言写的, 所以需要一个协议, 一个php解释器, 也就是CGI. ...

  5. visio2010去除直线交叉处的跨线

    设计(最上方)->连接线(最右侧)->显示跨线(取消打钩)

  6. netty 实现socket服务端编写

    import java.net.InetSocketAddress; import io.netty.bootstrap.ServerBootstrap; import io.netty.channe ...

  7. GPS(1)核心API及3种列出LocationProvider的方法

    GPS的常用API Android SDK为GPS提供了很多API,其中LocationManager类是这些API的核心.所有与GPS相关的操作都由LocationManager对象及其派生的对象完 ...

  8. Oracle中Blob和Clob类型的区别与操作

    Oracle中Blob和Clob类型 1.Oracle中Blob和Clob类型的区别 BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的 ...

  9. 【HDOJ】4579 Random Walk

    1. 题目描述一个人沿着一条长度为n个链行走,给出了每秒钟由i到j的概率($i,j \in [1,n]$).求从1开始走到n个时间的期望. 2. 基本思路显然是个DP.公式推导也相当容易.不妨设$dp ...

  10. sencha touch tabsidebar 源码扩展

    先上图看效果 没错,这是一个sencha touch 项目,而这里的右边推出效果(下文叫做tabsiderbar),使用插件tabsiderbar来扩展的. 插件js下载地址:http://www.m ...