水 A - Two Bases

水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了。被hack掉了。用long long能存的下

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int b1[44], b2[44];
int n, m; int main(void) {
scanf ("%d%d", &n, &m);
double ans1 = 0, ans2 = 0;
for (int i=1; i<=n; ++i) {
scanf ("%d", &b1[i]);
}
for (int i=n; i>=1; --i) {
ans1 += pow ((double) m, n-i) * b1[i];
}
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", &b2[i]);
}
for (int i=n; i>=1; --i) {
ans2 += pow ((double) m, n-i) * b2[i];
}
if (ans1 < ans2) puts ("<");
else if (ans1 > ans2) puts (">");
else puts ("="); return 0;
}
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int b1[44], b2[44];
int n, m; ll _pow(int m, int x) {
ll ret = 1;
for (int i=1; i<=x; ++i) {
ret *= m;
}
return ret;
} int main(void) {
cout << (ll) pow (39, 9) << endl;
cout << _pow (39, 9) << endl;
scanf ("%d%d", &n, &m);
ll ans1 = 0, ans2 = 0;
for (int i=1; i<=n; ++i) {
scanf ("%d", &b1[i]);
}
for (int i=n; i>=1; --i) {
ans1 += _pow (m, n-i) * b1[i];
}
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", &b2[i]);
}
for (int i=n; i>=1; --i) {
ans2 += _pow (m, n-i) * b2[i];
}
if (ans1 < ans2) puts ("<");
else if (ans1 > ans2) puts (">");
else puts ("="); return 0;
} /*
9 39
10 20 16 36 30 29 28 9 8
9 38
12 36 10 22 6 3 19 12 34
*/

尺取法 B - Approximating a Constant Range

简单说就是维护[i, j]区间的最大值和最小值以及它们的最后的位置。

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[N]; int main(void) {
int n; scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
}
int ans = 1, mn = a[1], mx = a[1], i = 1, j = 2;
int p1 = 1, p2 = 1;
while (j <= n) {
if (a[j] <= mn) {
mn = a[j]; p1 = j;
}
else if (a[j] >= mx) {
mx = a[j]; p2 = j;
} if (mx - mn <= 1) {
ans = max (ans, j - i + 1);
}
else {
while (mx - mn > 1) {
if (p1 < p2 && p1 + 1 <= j) {
mn = a[p1+1]; i = p1 + 1; p1++;
}
else if (p1 >= p2 && p2 + 1 <= j) {
mx = a[p2+1]; i = p2 + 1; p2++;
}
else break;
}
ans = max (ans, j - i + 1);
}
j++;
} printf ("%d\n", ans); return 0;
}

BFS C - The Two Routes

两点之间要不是地铁要不就是汽车,那么1到n也一样,只要一次BFS就行了,水水的。

我刚做了一道双向BFS,想套一个试试,写麻烦了,但是还是A掉了,想想还是有问题,vis数组有问题,数据水了。。因为有一个一定会在一步到达,和另一个不冲突

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 4e2 + 10;
const int M = N * N;
const int INF = 0x3f3f3f3f;
struct Edge {
int v, w, nex;
Edge() {}
Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {}
}edge[M];
int head[N];
bool lk[N][N];
bool vis[N][N][2];
bool vis2[N][2];
bool ok[2];
int n, m, e;
queue<int> que[2]; void init(void) {
memset (head, -1, sizeof (head));
e = 0;
} void add_edge(int u, int v, int w) {
edge[e].v = v; edge[e].w = w; edge[e].nex = head[u];
head[u] = e++;
} bool BFS(int typ, int tim) {
int sz = que[typ].size ();
while (sz--) {
int u = que[typ].front (); que[typ].pop ();
for (int i=head[u]; ~i; i=edge[i].nex) {
int v = edge[i].v, w = edge[i].w;
if (w != typ) continue;
if (v == n) {
ok[typ] = true;
if (ok[typ^1]) return true;
}
if (vis2[v][typ]) continue;
vis2[v][typ] = true;
if (vis[v][tim][typ^1]) continue;
vis[v][tim][typ] = true;
que[typ].push (v);
}
}
return false;
} int run(void) {
que[0].push (1); que[1].push (1);
int step = 0;
while (!que[0].empty () || !que[1].empty ()) {
step++;
if (step > 800) break;
if (!ok[0]) {
if (BFS (0, step)) return step;
}
if (!ok[1]) {
if (BFS (1, step)) return step;
}
}
return -1;
} int main(void) {
init ();
scanf ("%d%d", &n, &m);
for (int u, v, i=1; i<=m; ++i) {
scanf ("%d%d", &u, &v);
add_edge (u, v, 1);
add_edge (v, u, 1);
lk[u][v] = lk[v][u] = true;
}
int cnt = 0;
for (int i=1; i<=n; ++i) {
for (int j=i+1; j<=n; ++j) {
if (i == j || lk[i][j]) continue;
cnt++;
add_edge (i, j, 0);
add_edge (j, i, 0);
}
}
if (cnt == 0) {
puts ("-1"); return 0;
}
printf ("%d\n", run ()); return 0;
}

找规律+区间端点 D - Lipshitz Sequence

题意:q次询问,询问[l, r]的所有子区间 sum (max (abs (a[i] - a[j]) / j - i)) (1 <= i < j <= n)

分析:首先要知道最大值是abs (a[i] - a[i-1]),然后要弄出多少个子区间包含了这个值,用到KMP思想,left[i]/right[i]能记录i最左边和最右边不大于该值的位置,左闭右开防止重复

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[N];
int lef[N], righ[N];
int n, m; ll run(int l, int r) {
if (l == r) return 0;
for (int i=l+1; i<=r; ++i) {
int j = i - 1;
while (j > l && abs (a[j] - a[j-1]) <= abs (a[i] - a[i-1])) j = lef[j];
lef[i] = j;
}
for (int i=r; i>l; --i) {
int j = i + 1;
while (j <= r && abs (a[j] - a[j-1]) < abs (a[i] - a[i-1])) j = righ[j];
righ[i] = j;
}
ll ret = 0;
for (int i=l+1; i<=r; ++i) {
ret += 1ll * (i - lef[i]) * (righ[i] - 1 - i + 1) * abs (a[i] - a[i-1]);
}
return ret;
} int main(void) {
scanf ("%d%d", &n, &m);
for (int i=1; i<=n; ++i) {
scanf ("%d", &a[i]);
}
for (int l, r, i=1; i<=m; ++i) {
scanf ("%d%d", &l, &r);
printf ("%I64d\n", run (l, r));
} return 0;
}

概率DP E - Kleofáš and the n-thlon

题意:n次比赛,m个人,现在给出n次比赛一个人的排名,问他最后的排名的期望

分析:转换成其余人得分少于他的概率 * 总人数 +1,dp[i][j] 表示前i次比赛,得分为j时的期望。dp[i][j] = sum + dp[i-1][j-1] - dp[i-1][j-m-1] - dp[i-1][j-a[i]];状态转移用前缀和优化复杂度

#include <bits/stdc++.h>
using namespace std; double dp[105][105*1005];
int a[105]; int main(void) {
int n, m; scanf ("%d%d", &n, &m);
int sum = 0;
for (int i=0; i<n; ++i) {
scanf ("%d", &a[i]); sum += a[i];
}
if (m == 1) {
printf ("%.10f\n", 1.0); return 0;
}
dp[0][0] = m-1;
for (int i=1; i<=n; ++i) {
double sum = 0;
for (int j=i; j<=i*m; ++j) {
sum += dp[i-1][j-1] / (m-1);
if (j - 1 - m >= 0) sum -= dp[i-1][j-m-1] / (m-1);
dp[i][j] = sum;
if (j - a[i-1] >= 0) dp[i][j] -= dp[i-1][j-a[i-1]] / (m-1);
}
}
double ans = 1;
for (int i = 0; i < sum; i++) ans += dp[n][i];
printf("%.10f\n", ans); return 0;
}

Codeforces Round #333 (Div. 2)的更多相关文章

  1. Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp

    C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...

  2. Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分

    B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...

  3. Codeforces Round #333 (Div. 2) C. The Two Routes flyod

    C. The Two Routes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/pro ...

  4. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  5. Codeforces Round #333 (Div. 2) A. Two Bases 水题

    A. Two Bases Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/602/problem/ ...

  6. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  7. Codeforces Round #333 (Div. 1) D. Acyclic Organic Compounds trie树合并

    D. Acyclic Organic Compounds   You are given a tree T with n vertices (numbered 1 through n) and a l ...

  8. Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈

    题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...

  9. Codeforces Round #333 (Div. 2) B

    B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...

随机推荐

  1. struts2域值操作

    1.通过servletActionContext类 /*** * 获得方式一:通过ServletActionContext类 * 提供的静态方法获得原始的web对象,直接和servlet的API耦合 ...

  2. October 10th 2016 Week 42nd Monday

    What makes life dreary is the want of motive. 没有了目标,生活便黯然无光. Motive and goal, are absolutely indispe ...

  3. SQL Server之存储过程基础知

    什么是存储过程呢?存储过程就是作为可执行对象存放在数据库中的一个或多个SQL命令. 通俗来讲:存储过程其实就是能完成一定操作的一组SQL语句. 那为什么要用存储过程呢?1.存储过程只在创造时进行编译, ...

  4. Gif图片制作

    gif图片是博客中展示项目效果的一种很好的方式,为我们的app制作一张gif图片并不复杂,录制屏幕采用系统自带的QuickTime Player,制作gif采用PicGIF软件.licecap软件更是 ...

  5. 清空mysql的历史记录

    # vi ~/.mysql_history show tables; show databases; 清空里面的内容,并不用退出当前shell,就可以清除历史命令!!

  6. ASP.NET Web API 数据验证

    第一种方式单独为每一个Action做验证 // POST api/values public HttpResponseMessage Post([FromBody]UserInfo userInfo) ...

  7. 关于strcpy_s

    #include"stdafx.h" #include<iostream> #include<cstring> int main() { using nam ...

  8. <转>JDBC获取DB元数据

    原文链接:http://jiauwu.iteye.com/blog/1307617 package com.util.jdbc; import java.sql.Connection; import ...

  9. 简单的redis 性能测试

    C:\Users\luhan.qian\Desktop\Tools\redis C:\Users\luhan.qian\Desktop\Tools\redis $ redis-benchmark.ex ...

  10. 在IIS上部署SSL

    背景: 在处理DropboxAPI开发时,其重定向的URL地址必须是https的[除了localhost],不得已在自己网站上加了ssl,下面简单介绍下添加自签名证书,毕竟只是临时使用. 1.打开II ...