科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
比赛网址传送门
目录
- A 点对最大值
- B 减成一
- C 面积
- D 扔硬币
- E 赛马
- F 三角形
- G 养花
- H 直线
- I 字典序
- J 最大值
A 点对最大值
求这个树的直径,已经见过三次了,还是没打出来,注意个别地方稍微修改一下。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
struct Edge
{int before, to, w;
} e[maxn << 1];
int val[maxn], dp[maxn];
int n, k, head[maxn];
int ans = 0;
void add(int u, int v, int w)
{e[k].before = head[u];e[k].to = v;e[k].w = w;head[u] = k++;
}
void DFS(int u, int fa)
{dp[u] = val[u];for (int i = head[u]; i != -1; i = e[i].before){int v = e[i].to;if (v == fa)continue;DFS(v, u);ans = max(ans, dp[u] + dp[v] + e[i].w);dp[u] = max(dp[u], dp[v] + e[i].w);}
}
int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endifIO;int T;scanf("%d", &T);while (T--){k = 0;memset(head, -1, sizeof head);scanf("%d", &n);int x, y;for (int i = 2; i <= n; i++){scanf("%d %d", &x, &y);add(x, i, y);add(i, x, y);}for (int i = 1; i <= n; i++)scanf("%d", &val[i]);ans = -inf;DFS(1, 0);cout << ans << "\n";}return 0;
}
B 减成一
容易发现答案为每一个递增序列的递增差之和,可以把数组当做很多个递增序列(样例为3个递增序列为别递增 (2+2)+5+0=9),但是可能会出现第一个序列即为递减的情况,这时我们可以再在a[0] 位置设置一个1。这样就不需要特判了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
LL a[maxn];
int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endif// IO;int T;int n;scanf("%d", &T);while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%lld", &a[i]);a[0] = 1;LL ans = 0;for (int i = 1; i <= n; i++){if (a[i] >= a[i - 1])ans += 1LL * a[i] - a[i - 1];}cout << ans << endl;}return 0;
}
C 面积
水
D 扔硬币
这是一个基于二项分布情况下的简单的条件概率,把相对应的概率算出来就好。对于逆元要用线性递推进行预处理,否则T飞。哦对了,组合数也用了递推式,并没有逐个计算。
但是对于这道题目,只需要处理组合数就行,后面硬币概率算了就多此一举了,可惜我当时还是稀里糊涂得计算了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e6 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
LL c[maxn];
LL inv[maxn];
LL qpow(LL a, LL b, LL c)
{LL ans = 1;while (b){if (b & 1)ans = ans * a % c;a = a * a % c;b = b >> 1;}return ans;
}
LL Inv(LL a, LL p)
{return qpow(a, p - 2, p);
}
int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endif// IO;int T;inv[1] = 1;for (int i = 2; i <= maxn; i++){inv[i] = mod - mod / i * inv[mod % i] % mod;}cin >> T;while (T--){LL n, m, k;cin >> n >> m >> k;if (k > n - m){cout << 0 << endl;continue;}m = n - m;LL t1 = qpow(Inv(2, mod), n, mod); // 2 的 n次方LL t2 = Inv(k, mod); // k 的 逆元c[0] = 1;LL ans2 = c[0] * t1 % mod;for (int i = 1; i <= m; i++){c[i] = ((c[i - 1] % mod * (n - i + 1) % mod) * inv[i] % mod) % mod;ans2 = (ans2 % mod + c[i] * t1 % mod) % mod;}LL ans1 = c[k] * t1 % mod;cout << ans1 * Inv(ans2, mod) % mod << endl;}return 0;
}
E 赛马
O(n^)暴力即可,不要忘了标记。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 998244353;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
bool vis[maxn];
int a[maxn], b[maxn];
int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endif// IO;int T;int n;cin >> T;while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++){vis[i] = false;scanf("%d", &a[i]);}for (int i = 1; i <= n; i++)scanf("%d", &b[i]); // 对手sort(a + 1, a + n + 1);int ans = 0;for (int i = 1; i <= n; i++){int t = b[i];int flag = false;for (int j = 1; j <= n; j++){if (a[j] > t && vis[j] == false){flag = true;vis[j] = true;break;}}if (flag)ans++;}cout << ans << endl;}return 0;
}
F 三角形
这题当时懵了,这么多人做出来,就该再仔细思考一下的。
斐波那契数列对于这题是最完美的,假如给定的长度不满足斐波那契前n项和,那么直接加到最后一条边上,具体看代码。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e6 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
ULL f[200];int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endif// IO;f[1] = 1;f[2] = 1;for (int i = 3; i <= 200; i++){f[i] = f[i - 1] + f[i - 2];if (f[i] < f[i - 1])break;}int T;ULL a;cin >> T;while (T--){cin >> a;for (int i = 1; i <= 93; i++){if (a > f[i])a -= f[i];else if (a == f[i]){cout << i << endl;break;}else if (a < f[i]){cout << i - 1 << endl;break;}}}return 0;
}
G 养花
最大流,建图很多地方不明白,待定。
H 直线
由公式可得答案为 n*(n-1)/2 python水过。。
n = int(input())
for i in range(n):m = int(input())print(m * (m-1) // 2)
I 字典序
待定。
J 最大值
可以把输入的s当做模式串,s去掉第一个字符做为文本串,进行kmp匹配,维护一个匹配的最大长度,这样就满足题意了,题解说直接求next数组??我还是tcl!!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO \ios::sync_with_stdio(false); \// cin.tie(0); \// cout.tie(0);
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
string s, p;
int nextp[maxn];
void Get_nextp(int n)
{for (int i = 1; i < n; i++){int j = i;while (j > 0){j = nextp[j];if (p[i] == p[j]){nextp[i + 1] = j + 1;break;}}}
}
int Kmp(int n)
{int i, j;int ans = -1;for (i = 0, j = 0; i < n; i++){if (j < n && s[i] == p[j])j++, ans = max(ans, j);else{while (j > 0){j = nextp[j];if (s[i] == p[j]){j++;ans = max(ans, j);break;}}}}return ans;
}
int main()
{#ifdef ONLINE_JUDGE
#elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);
#endifIO;int T, n;cin >> T;while (T--){cin >> s;n = s.size();p = s.substr(1, n - 1);swap(s, p);// cout << s << endl;// cout << p << endl;Get_nextp(n);cout << Kmp(n - 1) << endl;}return 0;
}
科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)相关推荐
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) 题解
"科林明伦杯"哈尔滨理工大学第十届程序设计竞赛(同步赛) 题解 萌新又来写题解啦 原题链接 B 减成一 题意:存在n个数,每次操作可以任选一个区间使得区间内的所有数字减一.问最少多 ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)---全题目+题解
文章目录 A.点对最大值 B.减成一 C.面积 D.扔硬币 E.赛马 F.三角形 G.养花 H.直线 I.字典序 J.最大值 A.点对最大值 链接:https://ac.nowcoder.com/ac ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) F
F 三角形 链接:https://ac.nowcoder.com/acm/contest/5758/F 来源:牛客网 小明有一根长度为a的木棒,现在小明想将木棒分为多段(每段木棒长度必须为整数), 使 ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) 点对最大值 dp
链接:https://ac.nowcoder.com/acm/contest/5758/A 来源:牛客网 题目描述 这里有一棵树,每个点和每条边都存在一个价值.对于树上点对的价值,包括点对的起点和终点 ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛) E
E 赛马 一天小明与他同学准备赛马,他们每人有n匹马,每匹马有一个固定的战力值,战力值高的马会战胜战力值低的马并赢得比赛.每匹马只能出场比赛一次.小明偷看到了他对手每匹马的出场顺序,小明在更改自己马出 ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
A.点对最大值 这里有一棵树,每个点和每条边都存在一个价值.对于树上点对的价值,包括点对的起点和终点以及路径上边权值之和,不包括路径上其他点值. 求这颗树上最大的点对价值为多少.点对至少需要两个点. ...
- 科林明伦杯”哈尔滨理工大学第十届程序设计竞赛B(减成1)
科林明伦杯"哈尔滨理工大学第十届程序设计竞赛 存在n个数,每次操作可以任选一个区间使得区间内的所有数字减一.问最少多少次操作,可以让所有数都变成1. 数据保证一定有解. 输入描述: 输入t, ...
- “科林明伦杯”哈尔滨理工大学第十届程序设计竞赛 E 赛马 python
"科林明伦杯"哈尔滨理工大学第十届程序设计竞赛 E 赛马 python E 好家伙 田忌赛马真就 匹配就不解释了 思路,主要咱不止一匹马 所以就最好的比 对方比这个数小的即可 所以 ...
- 科林明伦杯 哈尔滨理工大学第十届程序设计竞赛 (补)
B减成一 利用差分数组,把前后差为正的数都加起来,这里a[0]要设置为1. #include <iostream> #define ll long longusing namespace ...
最新文章
- Swift3实现的绘制股票K线库, FastImageCache提升图片的加载和渲染速度,Chameleon颜色框架
- 扩展卡尔曼滤波EKF与多传感器融合
- spring、springMvc、springBoot和springCloud的联系与区别
- Remote Data Access
- css 样式尾部带感叹号是什么意思_CSS书写规范
- 蓝桥杯第八届省赛JAVA真题----字母组串
- Adobe illustrator 多个对象进行环形布局 - 连载22
- C|C++中的静态全局变量,静态局部变量,全局变量,局部变量的区别
- 开发环境与工具部署服务_开发与部署之间的区别
- 生产环境频繁内存溢出,原来就是因为这个“String类”
- 河南科技大学计算机专业专升本,河南科技大学成人高考计算机应用技术专业的课程有哪些...
- keepalived详解(一)——keepalived理论基础
- Apache无法加载PHP模块的解决方案
- Linux系统用户账号的管理
- 数学建模:Leslie离散人口发展模型
- H264格式详细说明
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- Eclipse中导入项目前有红叉提示但是项目内容不报错解决办法
- 使用kaminari实现分页
- ros自己写避障算法_基于ROS的机器人室内环境探索、避障和目标跟踪方法与流程...
热门文章
- C#在一个form中改变另一个form中控件的内容、C#做登录界面并且密码显示为*
- svn版本库的拆分及迁移子目录等
- Exponial欧拉降幂
- 【YOLOv1原文+翻译】You Only Look Once Unified, Real-Time Object Detection
- 启动gazebo失败报错[gazebo-1] process has died [pid 10999, exit code 255
- 三维电子无人机倾斜摄影数字沙盘开发第38课 实现简单的粒子效果
- Generating Summaries with Topic Templates and Structured Convolutional Decoders笔记
- python 使用twilio免费发送短信
- 修改电脑开机徽标教程
- Android给Activity取消title标题