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 ...
随机推荐
- Spring与Web框架(例如Spring MVC)漫谈——关于Spring对于多个Web框架的支持
在看Spring MVC的官方文档时,最后一章是关于Spring对于其它Web框架的支持(如JSF,Apache Struts 2.x,Tapestry 5.x),当然Spring自己的MVC框架Sp ...
- 图解Java继承内存分配
图解Java继承内存分配 继承的基本概念: (1)Java不支持多继承,也就是说子类至多只能有一个父类. (2)子类继承了其父类中不是私有的成员变量和成员方法,作为自己的成员变量和方法. (3)子 ...
- 作业1:java虚拟机内存模型图示
看了很多篇文章,整理成一幅图,但仍然有许多不解的地方,以后再接着完善,哪位大神看到不正确的地方,请指出,谢谢.
- NVIDIA双显卡
NVIDIA双显卡 第一步:代码:sudo update-pciids #更新显卡信息非常重要,否则可能识别出错lspci -v | grep -i vga #察看显卡 我的显卡信息如下:代码:00: ...
- luogu2568GCD题解--欧拉函数
题目链接 https://www.luogu.org/problemnew/show/P2568 分析 题目即求\(\sum_{i=1}^N \sum_{j=1}^N [gcd(i,j)\) \(is ...
- SVG学习之stroke-dasharray 和 stroke-dashoffset 详解
本文适合对SVG已经有所了解,但是对stoke-dasharray和stroke-dashoffset用法有疑问的童鞋 第一:概念解释 1. stroke意思是:画短线于,在...上划线 2. str ...
- pytorch转onnx问题
Fail to export the model in PyTorch https://github.com/onnx/tutorials/blob/master/tutorials/PytorchA ...
- os.path:平台独立的文件名管理
介绍 利用os.path模块中包含的函数,很容易编写代码来处理多个平台上的文件 解析路径 import os.path ''' os.path中的第一组函数可以用来将表示文件名的字符串解析为文件名的各 ...
- c++ 二叉树的遍历(迭代,递归)
#include<iostream> #include <algorithm> #include <vector> #include <set> #in ...
- [转]makefile学习
原文: http://blog.fatedier.com/2014/09/08/learn-to-write-makefile-01/ -------------------------------- ...