2017 ZSTU寒假排位赛 #4
题目链接:https://vjudge.net/contest/148543#overview。
A题:n个罪犯,每个人有一个犯罪值,现在要从里面选出连续的c个人,每个人的犯罪值都不能超过t,问选法的种类数。O(n)xjbg一下即可:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int N = + ;
typedef long long ll;
typedef pair<int,int> pii; int a[N]; int main()
{
int n,t,c;
cin >> n >> t >> c;
ll ans = ;
int cnt = ;
for(int i=;i<=n;i++)
{
int temp;scanf("%d",&temp);
if(temp <= t) cnt++;
else
{
if(cnt >= c) ans += cnt - c + ;
cnt = ;
}
}
if(cnt >= c) ans += cnt - c + ;
cout << ans << endl;
return ;
}
A
B题:n个数字,从中选出不重叠的k段数字,每段的长度都为m,问最大的和。因为n是5000,所以很明显的开个二维数组n方dp一下即可:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int N = + ;
typedef long long ll;
typedef pair<int,int> pii; ll pre[N];
ll dp[N][N]; int main()
{
int n,m,k;
cin >> n >> m >> k;
for(int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
pre[i] = pre[i-] + x;
}
for(int i=m;i<=n;i++)
{
for(int j=;j<=k;j++)
{
dp[i][j] = max(dp[i-][j], dp[i-m][j-] + pre[i] - pre[i-m]);
}
}
cout << dp[n][k] << endl;
return ;
}
B
C题:给一个距离矩阵,问是否可能构成一棵树。做法是克鲁斯卡尔以后dfs一遍看看距离矩阵是否相等即可。仔细想想还是挺妙的。代码如下(写的有点挫):
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int N = + ;
typedef long long ll;
typedef pair<int,int> pii; int root[N],cnt,vis[N],last[N];
int n,G[N][N],d[N][N];
vector<pii> graph[N];
void init() {for(int i=;i<=n;i++) root[i] = i;}
int find(int x) {return x == root[x] ? x : root[x] = find(root[x]);}
void connect(int x,int y,int w)
{
int rx = find(x);
int ry = find(y);
if(rx != ry)
{
root[rx] = ry;
//d[x][y] = d[y][x] = w;
graph[x].push_back(pii(y,w));
graph[y].push_back(pii(x,w));
cnt--;
}
}
struct edge
{
int u,v,w;
bool operator < (const edge & temp) const
{
return w > temp.w;
}
}; void dfs(int u,int fa)
{
for(int i=;i<graph[u].size();i++)
{
pii p = graph[u][i];
int v = p.first;
int w = p.second;
if(v == fa) continue;
last[v] = last[u] + w;
dfs(v,u);
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&G[i][j]);
if(i == j && G[i][j]) return *puts("NO");
}
}
for(int i=;i<=n;i++) for(int j=;j<=n;j++) if(G[i][j] != G[j][i] || i!=j && G[i][j] == ) return *puts("NO");
priority_queue<edge> Q;
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
Q.push((edge){i,j,G[i][j]});
}
}
cnt = n; init();
for(;cnt > ;)
{
edge e = Q.top();Q.pop();
connect(e.u, e.v, e.w);
}
//for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) printf("%d%c",d[i][j],j==n?'\n':' ');
for(int i=;i<=n;i++)
{
last[i] = ;
dfs(i,-);
for(int j=;j<=n;j++)
{
if(j == i) continue;
if(last[j] != G[i][j]) return *puts("NO");
}
}
puts("YES");
return ;
}
C
D题:给两个部分的字符串,分别重复n次和m次(最终长度相等),问有多少位置字符是不同的。只需要比较一个lcm里面的长度是肯定的,但暴力做肯定T。一个lcm中,考虑到只有pos%gcd这些位置需要比较。那么就可以线性的做出来了。最后扩大相应倍数即可。代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
const int N = + ;
typedef long long ll;
typedef pair<int,int> pii; ll n,m;
char a[N],b[N];
int vis[N][]; int main()
{
cin >> n >> m;
scanf("%s%s",a+,b+);
int lena = strlen(a+);
int lenb = strlen(b+);
int g = __gcd(lena,lenb);
ll lcm = (ll)lena / g * lenb;
ll ans = ;
for(int i=;i<=lena;i++) vis[i%g][a[i]-'a']++;
for(int i=;i<=lenb;i++) ans += vis[i%g][b[i]-'a'];
cout << 1LL*lena * n / lcm * (lcm - ans) << endl;
return ;
}
D
E题,看了一会没怎么明白题意,暂时放过吧。
2017 ZSTU寒假排位赛 #4的更多相关文章
- 2017 ZSTU寒假排位赛 #7
题目链接:https://vjudge.net/contest/149498#overview. A题,水题,直接按照题意模拟一下即可. B题,我用的是线段树.大力用的差分标记(上次听zy说过,下次再 ...
- 2017 ZSTU寒假排位赛 #1
题目链接:https://vjudge.net/contest/147102#overview. A题:给出一堆的点,要找出两条垂直的直线,一条与x轴呈45度.-->使得所有的点到任意一条直线的 ...
- 2017 ZSTU寒假排位赛 #2
题目链接:https://vjudge.net/contest/147632#overview. A题,状态压缩一下然后暴力即可. B题,水题,略过. C题,有负数,前缀和不是单调的,因此不能用尺取法 ...
- 2017 ZSTU寒假排位赛 #8
题目链接:https://vjudge.net/contest/149845#overview. A题,水题. B题,给出 p个 第一个人的区间 和 q个第二个人的区间,问[l,r]中有多少个整数满足 ...
- 2017 ZSTU寒假排位赛 #6
题目链接:https://vjudge.net/contest/149212#overview. A题,水题,略过. B题,水题,读清题意即可. C题,数学题,如果把x表示成x=nb+m,则k=n/m ...
- 2017 ZSTU寒假排位赛 #5
题目链接:https://vjudge.net/contest/148901#overview. A题,排序以后xjbg即可. B题,弄个数组记录当前列是不是删除以及当前行是不是已经大于下一行然后乱搞 ...
- 2017 ZSTU寒假排位赛 #3
题目链接:https://vjudge.net/contest/147974#overview. A题,费用流,不会..跳过了. B题,给一个图,问至少添加几条边能成为强连通图.显然缩点,要使得成为一 ...
- Codeforces Round #341 (Div. 2)
在家都变的懒惰了,好久没写题解了,补补CF 模拟 A - Wet Shark and Odd and Even #include <bits/stdc++.h> typedef long ...
- 2017杭电ACM集训队单人排位赛 - 6
2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...
随机推荐
- Nodejs:单线程为什么能支持高并发?
1.Nodejs是一个平台,构建在chrome的V8上(js语言解释器),采用事件驱动.非阻塞模型( c++库:libuv). 参考官方: Node.js is a platform built ...
- Node.js Express项目搭建
讲干货,不啰嗦,Express 是一个简洁而灵活的 node.js Web应用框架,使用 Express 可以快速地搭建一个完整功能的网站.本教程介绍如何从零开始搭建Express项目. 开发环境:w ...
- selenium 12306模拟登陆
代码应用场景 :基于第三方打码网站模拟登陆12306 验证码识别 基于第三方平台超级鹰识别 超级鹰官网:http://www.chaojiying.com/user/ 超级鹰使用流程: 注册 登陆(用 ...
- json树迭代
getArray(data){ for (var i in data) { if(data[i].disabled){ data[i].disabled = false } if(data[i].ch ...
- Spring Data JPA 大纲归纳
第一天: springdatajpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查 ...
- IDM下载百度资源出现403的解决方法
测试发现是受cookie的影响,百度为了防止用外部下载工具突破限速加入了cookie验证,因为一般的下载工具请求下载的时候不会附加cookie信息. IDM就是这样,它请求下载文件时只知道文件的下载地 ...
- java oop 基础
1.如果将一个类打包,使用该类的时候,必须使用该类的全名,java编译器才能找到. 2.也可以使用import 导入这个包. 3.可以不需要import语句 直接使用 java.lang包中的类. 4 ...
- php的三个常用判断函数
<?phperror_reporting(E_ERROR);$a;$b = false;$c = '';$d = 0;$e = null;$f = array(); echo 'empty', ...
- kafka启动失败错误:: replica.fetch.max.bytes should be equal or greater than message.max.bytes
1 详细异常 2019-10-14 14:38:21,260 FATAL kafka.Kafka$: java.lang.IllegalArgumentException: requirement f ...
- 封装Server类和Client类
服务器端: EasyTcpServer.hpp #ifndef _EasyTcpServer_hpp_ #define _EasyTcpServer_hpp_ #ifdef _WIN32 #defin ...