终于又回到熟悉的Round了

数学 A - Pouring Rain

设个未知数,解方程,还好没有hack点

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
const double PI = acos (-1.0); int main() {
double d, h, v, e; scanf ("%lf%lf%lf%lf", &d, &h, &v, &e);
double V = PI * (d / 2.0) * (d / 2.0) * h;
double addv = PI * (d / 2.0) * (d / 2.0) * e;
if (addv > v) {
puts ("NO");
} else {
double t = -V / (addv - v);
puts ("YES");
printf ("%.8f\n", t);
}
return 0;
}

数学 B - Coat of Anticubism

题意:求增加最小长度的一根木棍,使得构成一个多边形。

分析:那么构成三角形,原来n条木棍分成A,B两边,A和B接近(A<=B),那么另一条边满足A + C > B,即C = B +1 - A

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
int a[N]; int main() {
int n; scanf ("%d", &n);
ll sum = 0;
for (int i=0; i<n; ++i) {
scanf ("%d", a+i);
sum += a[i];
}
std::sort (a, a+n);
ll del = 1000001000;
ll A, B, presum = 0;
for (int i=0; i<n; ++i) {
presum += a[i];
ll res = sum - presum;
ll tmp = presum - res;
if (abs (tmp) < del) {
if (tmp < 0) {
del = -tmp;
A = presum;
B = res;
} else {
del = tmp;
A = res;
B = presum;
}
}
}
ll C = B + 1 - A;
printf ("%I64d\n", C); return 0;
}

DP C - Reberland Linguistics

题意:问后缀由若干个长度为2或3的子串构成,且相邻的子串不能相同。

分析:The only restriction — it is not allowed to append the same string twice in a row! 英语太渣不知道这是连续,相邻的意思。那么dp[i][0]表示i开始长度为2的子串能否可行,如果可行,那么dp[i-3][1]一定可行,因为长度不相等;还有如果长度相等的判断一下,即dp[i-2][0]。

#include <bits/stdc++.h>

const int N = 1e4 + 5;
bool dp[N][2]; int main() {
std::string str;
std::cin >> str;
int n = str.length ();
dp[n-2][0] = dp[n-3][1] = true;
std::set<std::string> ans;
for (int i=n-1; i>=5; --i) {
if (dp[i][0]) {
ans.insert (str.substr (i, 2));
dp[i-3][1] = true;
if (str.substr (i-2, 2) != str.substr (i, 2)) {
dp[i-2][0] = true;
}
}
if (dp[i][1]) {
ans.insert (str.substr (i, 3));
dp[i-2][0] = true;
if (str.substr (i-3, 3) != str.substr (i, 3)) {
dp[i-3][1] = true;
}
}
}
std::cout << ans.size () << '\n';
for (auto s: ans) {
std::cout << s << '\n';
}
return 0;
}

图论 D - World Tour

题意:找4个点按照顺序走,a->b->c->d,每次点到下一个点走的是最短路,问走的长度总和最大是多少。

分析:先计算dis(u, v),枚举b和c,对于b来说在反向图中找距离最远的点,因为a!=b a!=c,所以存最优的前3个点;对于c来说在原图中找距离最远的点,存最优的前4个点。

#include <bits/stdc++.h>

const int N = 3e3 + 5;
const int M = 5e3 + 5;
const int INF = 0x3f3f3f3f;
int dis[N][N];
std::vector<int> G[N], rG[N];
std::vector<std::pair<int, int> > bin[N], bout[N];
bool vis[N];
int n, m; void BFS() {
memset (dis, INF, sizeof (dis));
for (int i=1; i<=n; ++i) {
std::queue<int> que;
dis[i][i] = 0;
que.push (i);
while (!que.empty ()) {
int u = que.front (); que.pop ();
for (auto v: G[u]) {
if (dis[i][v] > dis[i][u] + 1) {
dis[i][v] = dis[i][u] + 1;
que.push (v);
}
}
}
}
} void sort_out() {
for (int i=1; i<=n; ++i) {
memset (vis, false, sizeof (vis));
vis[i] = true;
bout[i].push_back (std::make_pair (0, i));
std::queue<std::pair<int, int> > que;
que.push (std::make_pair (0, i));
while (!que.empty ()) {
std::pair<int, int> pu = que.front (); que.pop ();
for (auto v: G[pu.second]) {
if (!vis[v]) {
vis[v] = true;
bout[i].push_back (std::make_pair (pu.first + 1, v));
que.push (std::make_pair (pu.first + 1, v));
}
}
}
std::sort (bout[i].begin (), bout[i].end (), std::greater<std::pair<int, int> > ()); //dis[i][v], v
if (bout[i].size () > 4) {
bout[i].resize (4);
}
}
} void sort_in() {
for (int i=1; i<=n; ++i) {
memset (vis, false, sizeof (vis));
vis[i] = true;
bin[i].push_back (std::make_pair (0, i));
std::queue<std::pair<int, int> > que;
que.push (std::make_pair (0, i));
while (!que.empty ()) {
std::pair<int, int> pu = que.front (); que.pop ();
for (auto v: rG[pu.second]) {
if (!vis[v]) {
vis[v] = true;
bin[i].push_back (std::make_pair (pu.first + 1, v));
que.push (std::make_pair (pu.first + 1, v));
}
}
}
std::sort (bin[i].begin (), bin[i].end (), std::greater<std::pair<int, int> > ()); //dis[v][i], v
if (bin[i].size () > 3) {
bin[i].resize (3);
}
}
} int main() {
scanf ("%d%d", &n, &m);
for (int u, v, i=0; i<m; ++i) {
scanf ("%d%d", &u, &v);
G[u].push_back (v);
rG[v].push_back (u);
}
BFS ();
sort_in ();
sort_out ();
int a, b, c, d;
int best = 0;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
if (i == j || dis[i][j] == INF) {
continue;
}
int k, l;
for (int ii=bin[i].size ()-1; ii>=0; --ii) {
int tot = dis[i][j];
std::pair<int, int> &x = bin[i][ii];
if (x.second != i && x.second != j) {
k = x.second;
tot += x.first;
for (int jj=bout[j].size ()-1; jj>=0; --jj) {
std::pair<int, int> &y = bout[j][jj];
if (y.second != i && y.second != j && y.second != k) {
l = y.second;
tot += y.first;
if (best < tot) {
best = tot;
a = k; b = i; c = j; d = l;
}
tot -= y.first;
}
}
}
}
}
}
printf ("%d %d %d %d\n", a, b, c, d); return 0;
}

组合数学 C - Codeword(div 1)

题意:给一个串,长度为 l,问它扩展成长度n的串有多少个(其中长度l的原串相当于变成长度n的子序列)

分析:对于长度为 l 的子序列,n 的答案是

因为 的总和不超过 10^5​​,所以 不同的取值只有最多 2 sqrt(10^5)​​​​​ 种。对于每个 l,前缀和预处理所有 n 的答案。

,则

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
const int MOD = 1e9 + 7;
int fac[N], inv_fac[N];
char s[N];
int ans[N];
std::vector<std::pair<int, int> > query[N]; //len, n int pow_mod(int x, int n) {
int ret = 1;
while (n) {
if (n & 1) {
ret = (ll) ret * x % MOD;
}
x = (ll) x * x % MOD;
n >>= 1;
}
return ret;
} int binom(int n, int m) {
if (m > n) {
return 0;
} else {
return (ll) fac[n] * inv_fac[m] % MOD * inv_fac[n-m] % MOD;
}
} void prepare(int len, int maxn) {
int base = 1;
//g(n)
for (int i=0; i<=maxn; ++i) {
ans[i] = i < len ? 0 : (ll) binom (i - 1, len - 1) * base % MOD;
if (i >= len) {
base = (ll) base * 25 % MOD;
}
}
//f(n+1) = g(n+1) + f(n+1) * 26
for (int i=0; i<maxn; ++i) {
if (ans[i] >= MOD) {
ans[i] -= MOD;
}
ans[i+1] += (ll) ans[i] * 26 % MOD;
}
if (ans[maxn] >= MOD) {
ans[maxn] -= MOD;
}
} int main() {
fac[0] = 1;
for (int i=1; i<N; ++i) {
fac[i] = (ll) fac[i-1] * i % MOD;
}
inv_fac[N-1] = pow_mod (fac[N-1], MOD-2);
for (int i=N-2; i>=0; --i) {
inv_fac[i] = (ll) inv_fac[i+1] * (i + 1) % MOD;
}
std::vector<int> query;
int m; scanf ("%d", &m);
scanf ("%s", s);
int len = strlen (s);
int maxn = -1;
for (int i=0; i<m; ++i) {
int op; scanf ("%d", &op);
if (op == 1) {
prepare (len, maxn);
for (int j=0; j<query.size (); ++j) {
printf ("%d\n", ans[query[j]]);
}
query.clear ();
maxn = -1;
scanf ("%s", s);
len = strlen (s);
} else {
int n; scanf ("%d", &n);
if (maxn < n) {
maxn = n;
}
query.push_back (n);
}
}
prepare (len, maxn);
for (int j=0; j<query.size (); ++j) {
printf ("%d\n", ans[query[j]]);
}
return 0;
}

  

Codeforces Round #349的更多相关文章

  1. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  2. Codeforces Round #349 (Div. 2) D. World Tour (最短路)

    题目链接:http://codeforces.com/contest/667/problem/D 给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] ...

  3. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  4. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  5. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  6. Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)

    C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. Codeforces Round #349 (Div. 1)E. Forensic Examination

    题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...

  8. Codeforces Round #349 (Div. 2)

    第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...

  9. Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour   A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...

随机推荐

  1. Java_内存管理String and Array

    题目1.指出下列程序运行的结果 ()public class Example { String str = new String("good"); char[] ch = { 'a ...

  2. October 3rd 2016 Week 41st Monday

    Better to light one candle than to curse the darkness. 与其诅咒黑暗,不如燃起蜡烛. Sitting in the darkness and wa ...

  3. 苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推

    最近使用Hbuilder 进行了HTML5开发,因为 HTML5 可以放在android 机器上,也可以放到 IOS机器上,所以很感兴趣,于是开发了一个小应用, 不过问题接着来了: 图1 如图所示:当 ...

  4. Tomcat部署的时候 unpackWARs="true"

    在部署项目到Tomcat的时候发现当Tomcat启动的时候,项目并没有解压出来,导致系统传照片的时候找不到路径,因为,系统没有解压,所以找不到路径, 进入地址==>C:\Users\ceshi\ ...

  5. Lua程序设计入门

    在Lua中,一切都是变量,除了关键字.TTMD强大了. 1.注释 -- 表示注释一行 --[[ ]]表示注释一段代码,相当于C语言的/*....*/ 注意:[[ ... ]]表示一段字符串 2.lua ...

  6. iOS中的两种主要架构及其优缺点浅析

    凡是程序的开发者,应该对程序的架构都不陌生.一个程序的架构的好坏对这个程序有着非常重要的作用.今天我们来看一下iOS开发中用要的两种主流的程序架构.这个过程中我们主要以例子的形式展开. 我们来看第一种 ...

  7. iOS,Objective-C,相册功能的实现。

    #import "ViewController.h" #define kuan [UIScreen mainScreen].bounds.size.width #define ga ...

  8. 数据库支持emoji表情

    从MySQL5.5.3开始,MySQL 支持一种utf8mb4的字符集,这个字符集能够支持4字节的UTF8编码的字符.utf8mb4字符集能够完美地兼容utf8字符串.在数据存储方面,当一个普通中文字 ...

  9. DOM - 5.事件冒泡 + 6.事件中的this

    5.事件冒泡 如果元素A嵌套在元素B中,那么A被点击不仅A的onclick事件会被触发,B的onclick也会被触发.触发的顺序是"由内而外" .验证:在页面上添加一个table. ...

  10. 【JAVA常用类演示】

     一.java.lang.System类. public final class Systemextends Object 该类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的 ...