Noip2018 考前准备
未完成先看吧.大概9.1号完成.
然而现在都8102年了...
基础算法
二分
求最小值最大.
跳石头
#include <iostream>
#include <cstdio>
using namespace std;
const int maxN = 50000 + 7;
int a[maxN],n,m;
int L;
inline int read() {
register int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
bool calc(int k) {
int num = 0,path = 0;
for(int i = 1;i <= n;++ i) {
if(a[i] - a[path] < k) {
num ++;
}else path = i;
}
return num <= m;
}
int main() {
L = read();n = read();m = read();
for(int i = 1;i <= n;++ i) a[i] = read();
n ++;
a[n] = L;
int l = 0,r = L,ans;
while(l <= r) {
int mid = (l + r) >> 1;
if(calc(mid)) {
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
printf("%d",ans);
return 0;
}
模拟(未补)
高精(未学习)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;
struct bignum
{
int n;
int a[500];
bignum()
{
n = 0;
memset(a, 0, sizeof(a));
}
bignum(string s)
{
n = s.size();
memset(a, 0, sizeof(a));
for (int i = 0; i < n; i++)
a[i] = s[n - 1 -i] -'0';
}
bignum(int s)
{
memset(a, 0, sizeof(a));
n = 0;
while (s > 0)
{
a[n] = s % 10;
s /= 10;
n++;
}
}
void work()
{
for (int i = 0; i < n; i++)
{
if (a[i] < 0)
{
int tmp = (-a[i] - 1) / 10 + 1;
a[i] += 10 * tmp;
a[i + 1] -= tmp;
}
if (a[i] >= 10)
{
int tmp = a[i] / 10;
a[i] -= 10* tmp;
a[i + 1] += tmp;
if (i == n - 1 && a[i + 1] > 0) n++;
}
}
while (n > 0 && a[n - 1] == 0) n--;
}
void print()
{
for (int i = n - 1; i >= 0; i--)
cout << a[i];
cout << endl;
}
};
bignum operator + (const bignum &a, const bignum &b)
{
bignum c;
c.n = max(a.n, b.n);
for (int i = 0; i < c.n; i++)
c.a[i] = a.a[i] + b.a[i];
c.work();
return c;
}
bignum operator - (const bignum &a, const bignum &b)
{
bignum c;
c.n = max(a.n, b.n);
for (int i = 0; i < c.n; i++)
c.a[i] = a.a[i] - b.a[i];
c.work();
return c;
}
bignum operator * (const bignum &a, const bignum &b)
{
bignum c;
c.n = a.n + b.n - 1;
for (int i = 0; i < a.n; i++)
for (int j = 0; j < b.n; j++)
c.a[i + j] += a.a[i] * b.a[j];
c.work();
return c;
}
int main()
{
string s;
cin >> s;
int x;
cin >> x;
bignum a(s);
bignum b(x);
(a + b).print();
(a - b).print();
(a * b).print();
return 0;
}
搜索(未补)
排序
merge_sort(归并排序)
#include <iostream>
#include <cstdio>
const int maxN = 100000 + 7;
int z[maxN],a[maxN];
inline int read() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
void merge(int l,int r) {
if(l == r) return;
int mid = l + r >> 1;
merge(l,mid);
merge(mid + 1,r);
int z1 = l,z2 = mid + 1;
for(int i = l;i <= r;++ i) {
if(z1 <= mid)
if(z2 <= r)
if(a[z1] >= a[z2]) z[i] = a[z2 ++];
else z[i] = a[z1 ++];
else z[i] = a[z1 ++];
else z[i] = a[z2 ++];
}
for(int i = l;i <= r;++ i)
a[i] = z[i];
}
int main() {
int n = read();
for(int i = 1;i <= n;++ i)
a[i] = read();
merge(1,n);
for(int i = 1;i <= n;++ i)
printf("%d ",a[i]);
return 0;
}
图论
树的直径
POJ2631
#include <cstring>
#include <iostream>
#include <cstdio>
#include <vector>
const int maxN = 10000 + 7;
using namespace std;
int ans,id,n = 1;
int Rode[maxN];
struct Node{
int v,w;
};
vector<Node>Q[maxN];
void dfs1(int now,int fa) {
for(int i = 0;i < Q[now].size();++ i) {
int v = Q[now][i].v,w = Q[now][i].w;
if(v == fa) continue;
Rode[v] = Rode[now] + w;
dfs1(v,now);
}
return;
}
int max(int a,int b) {
return a > b ? a : b ;
}
int main() {
#ifdef WIN32
freopen("a.txt","r",stdin);
#endif
int u,v,w;
while(scanf("%d%d%d",&u,&v,&w) == 3) {
Q[u].push_back((Node) {v,w});
Q[v].push_back((Node) {u,w});
n ++;
}
dfs1(1,-1);
for(int i = 1;i <= n;++ i) {
if(ans < Rode[i]) {
ans = Rode[i];
id = i;
}
}
memset(Rode,0,sizeof(Rode));
dfs1(id,-1);
ans = 0;
for(int i = 1;i <= n;++ i)
ans = max(ans,Rode[i]);
printf("%d",ans);
return 0;
}
树的重心
POJ1655
#include <iostream>
#include <cstdio>
#include <vector>
const int maxN = 20000 + 7;
using namespace std;
vector<int> g[maxN];
int n;
int ans,id;
int max(int a,int b) {return a > b ? a : b;}
int min(int a,int b) {return a > b ? b : a;}
int dfs(int now,int fa) {
int son = 1,maxson = 0,kkk;
for(int i = 0;i < g[now].size();++ i) {
int v = g[now][i];
if(v == fa) continue;
int Q = dfs(v,now);
son += Q;
maxson = max(maxson,Q);
}
kkk = max(maxson,n - son);
if(kkk < ans) {
ans = kkk;
id = now;
}
return son;
}
int main() {
int T;
scanf("%d",&T);
while(T --) {
scanf("%d",&n);
for(int i = 1;i <= n;++ i)
g[i].clear();
for(int i = 1,u,v;i < n;++ i) {
scanf("%d%d",&u,&v);
g[u].push_back(v);
g[v].push_back(u);
}
ans = 0x7fffffff;
dfs(1,-1);
printf("%d %d\n",id,ans);
}
return 0;
}
最短路算法
Spfa
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
const int maxN = 100000 + 7;
const int maxM = 200000 + 7;
const int inf = 2147483647;
using namespace std;
int dis[maxN];
bool vis[maxN];
int max(int a,int b) {return a > b ? a : b;}
int min(int a,int b) {return a > b ? b : a;}
struct Node{
int v,w,nex;
}Map[maxM];
int head[maxN],num;
void add_Node(int u,int v,int w) {
Map[++ num].v = v;
Map[num].w = w;
Map[num].nex = head[u];
head[u] = num;
return;
}
void spfa(int now) {
queue<int>q;
q.push(now);
vis[now] = true;
dis[now] = 0;
while(!q.empty()) {
int u = q.front();q.pop();
vis[u] = 0;
for(int i = head[u];i != -1;i = Map[i].nex) {
int v = Map[i].v;
if(dis[v] > dis[u] + Map[i].w) {
dis[v] = dis[u] + Map[i].w;
if( !vis[v] ) {
vis[v] = true;
q.push(v);
}
}
}
}
return ;
}
int main() {
memset(dis,0x3f,sizeof(dis));
memset(head,-1,sizeof(head));
int n,m,s;
scanf("%d%d%d",&n,&m,&s);
for(int i = 1,u,v,w;i <= m;++ i) {
scanf("%d%d%d",&u,&v,&w);
add_Node(u,v,w);
}
spfa(s);
for(int i = 1;i <= n;++ i)
printf("%d ",dis[i] == 0x3f3f3f3f ? inf : dis[i]);
return 0;
}
Dijkstra
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x7fffffff;
const int maxN = 100000 + 7;
const int maxM = 200000 + 7;
struct Node{
int x,y;
inline bool operator < (const Node &a) const {
return y > a.y;
}
};
priority_queue<Node> q;
struct qode{
int v,nex,w;
}Map[maxM];
int s,dis[maxN],n,m,num,head[maxN];
void add_Node(int u,int v,int w) {
Map[++ num].v = v;
Map[num].w = w;
Map[num].nex = head[u];
head[u] = num;
}
void dij() {
for(int i = 1;i <= n;++ i) dis[i] = inf;
q.push((Node){s,0});
dis[s] = 0;
while(!q.empty()) {
Node x = q.top();q.pop();
if(x.y != dis[x.x]) continue;
for(int i = head[x.x];i;i = Map[i].nex) {
int v = Map[i].v;
if(dis[v] > dis[x.x] + Map[i].w) {
dis[v] = dis[x.x] + Map[i].w;
q.push((Node){v,dis[v]});
}
}
}
for(int i = 1;i <= n;++ i) {
printf("%d ",dis[i]);
}
}
int main() {
scanf("%d%d%d",&n,&m,&s);
for(int i = 1,u,v,w;i <= m;++ i) {
scanf("%d%d%d",&u,&v,&w);
add_Node(u,v,w);
}
dij();
return 0;
}
Floyd
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int a[101][101];
int main() {
int n,m,s;
scanf("%d%d%d",&n,&m,&s);
memset(a,0x3f,sizeof(a));
for(int i = 1,u,v,w;i <= m;++ i) {
scanf("%d%d%d",&u,&v,&w);
a[u][v]=min(a[u][v],w);
}
for(int k = 1;k <= n;++ k)
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= n;++ j)
a[i][j] = min(a[i][j],a[i][k] + a[k][j]);
a[s][s] = 0;
for(int i = 1;i <= n;++ i) {
printf("%d ",a[s][i] == 0x3f3f3f3f ? 2147483647 : a[s][i]);
}
}
最小生成树
kruskal
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxN = 5000 + 7;
const int maxM = 200000 + 7;
inline int read() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
struct Node {
int u,v,w;
}Q[maxM];
int fa[maxN];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void un(int a,int b) {
int faa = find(a),fab = find(b);
fa[faa] = fab;
return;
}
bool cmp(Node a,Node b) {
return a.w < b.w;
}
int num = 0;
void add_Node(int u,int v,int w) {
Q[++ num].u = u;
Q[num].v = v;
Q[num].w = w;
}
int main() {
int n,m;
n = read();m = read();
for(int i = 1;i <= n;++ i) fa[i] = i;
for(int i = 1,u,v,w;i <= m;++ i) {
u = read();v = read();w = read();
add_Node(u,v,w);
}
sort(Q + 1,Q + m + 1,cmp);
int sum = 0,k = 0;
for(int i = 1;i <= m;++ i) {
int fau = find(Q[i].u),fav = find( Q[i].v);
if(fau != fav) {
k ++ ;
sum += Q[i].w;
un(fau,fav);
if(k == n - 1) break;
}
}
if(k == n - 1) printf("%d",sum);
else printf("orz");
}
数论
线性筛
线性筛素数
void init1(int n) {
vis[1] = 1;
for(int i = 2;i <= n;++ i) {
if(!vis[i]) prime[++ num] = i;
for(int j = 1;prime[j] * i <= n;++ j) {
vis[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
}
线性筛phi
int phi[maxN];
void init2(int n) {
vis[1] = 1;
phi[1] = 1;
for(int i = 2;i <= n;++ i) {
if( !vis[i] ) {
prime[++ num] = i;
phi[i] = i - 1;
}
for(int j = 1;j <= num && prime[j] * i <= n;++ j) {
vis[i * prime[j]] = true;
if(i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
phi[i * prime[j]] = phi[i] *( prime[j] - 1);
}
}
}
线性筛莫比乌斯函数
int mu[maxN];
void init3(int n) {
vis[1] = 1;
mu[1] = 1;
for(int i = 2;i <= n;++ i) {
if(!vis[i]) {
prime[++ num] = i;
mu[i] = -1;
}
for(int j = 1;j <= num && prime[j] * i <= n;++ j) {
vis[i * prime[j]] = true;
if(i % prime[j] == 0) {
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
return;
}
gcd(最大公因数)
int gcd(int a,int b) {
while(b ^= a ^= b ^= a %= b);
return a;
}
lcm(最小公倍数)
int lcm(int a,int b) {
return a / gcd(a,b) * b;
}
快速幂
int fast_pow(int a,int b,int k) {
int ans = 1;
for(int now = a;b;b >>= 1,now = now * now) {
now %= k;
if(b & 1) ans *= now;
ans %= k;
}
return ans % k;
}
扩展欧几里得算法
void exgcd(int a,int b,int &x,int &y) {
if( !b ) {
x = 1;
y = 0;
return;
}
exgcd(b,a % b,x,y);
int tmp = x;
x = y;
y = tmp - (a / b) * y;
return;
}
逆元
nt inv(int n,int p) {
for(int i = 1,x,y;i <= n;++ i) {
exgcd(i,p,x,y);
printf("%d\n",(x % p + p) % p);
}
} //a * x ≡ q (mod p)
矩阵快速幂
ll n,m,k;
struct Marix{
ll s[107][107];
void clear() {
memset(s,0,sizeof(s));
return;
}
}ans,now,a;
Marix work(Marix a,Marix b) {
Marix c;
c.clear();
for(ll i = 1;i <= n;++ i)
for(ll j = 1;j <= n;++ j)
for(ll k = 1;k <= n;++ k)
c.s[i][j] = ( c.s[i][j] % mod+ ( a.s[i][k] * b.s[k][j] ) % mod ) % mod;
return c;
}
void fast_pow() {
ans.clear();
for(ll i = 1;i <= n;++ i) {
ans.s[i][i] = 1;
}
for(now = a;k;k >>= 1,now = work(now,now)) {
if(k & 1) ans = work(ans,now);
}
for(ll i = 1;i <= n;++ i) {
for(ll j = 1;j <= n;++ j) {
printf("%lld ",ans.s[i][j]);
}
puts("");
}
}
void Marix_fast_power() {
scanf("%lld%lld",&n,&k);
for(int i = 1;i <= n;++ i) {
for(int j = 1;j <= n;++ j) {
scanf("%lld",&a.s[i][j]);
}
}
fast_pow();
return ;
}
数据结构
ST表
#include <iostream>
#include <cstdio>
#define max(a,b) a > b ? a : b
#define min(a,b) a > b ? b : a
const int maxN = 1e5 + 7;
using namespace std;
int f[maxN][21];
int a[maxN];
int bin[maxN];
int n,m;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
void build_st() {
for(int i = 1;(1 << i) <= n;++ i)
bin[(1 << i)] = i;
for(int i = 3;i <= n;++ i)
if(!bin[i]) bin[i] = bin[i - 1];
for(int i = 1;i <= n;++ i)
f[i][0] = a[i];
for(int j = 1;(1 << j) <= n;++ j) {
for(int i = 1;i + (1 << j) - 1 <= n;++ i) {
f[i][j] = max(f[i + (1 << (j - 1))][j - 1],f[i][j - 1]);
}
}
}
int query(int l,int r) {
int x = bin[r - l + 1];
return max(f[l][x],f[r - (1 << x) + 1][x]);
}
int main() {
n = read();m = read();
for(int i = 1;i <= n;++ i)
a[i] = read();
build_st();
int l,r;
while(m --) {
l = read();r = read();
printf("%d\n",query(l,r));
}
return 0;
}
并查集
#include <iostream>
#include <cstdio>
using namespace std;
const int maxN = 10000 + 7;
int fa[maxN];
inline int read() {
int x = 0,f = 1;char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
return x * f;
}
int find(int x) {
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
void unit(int u,int v) {
int fau = find(u),fav = find(v);
fa[fau] = fav;
return;
}
int main() {
int n,m;
n = read();m = read();
for(int i = 1;i <= 10000;++ i)
fa[i] = i;
int opt,u,v;
while(m -- ) {
opt = read();u = read();v = read();
if(opt & 1) unit(u,v);
else {
int fau = find(u),fav = find(v);
if(fau == fav) printf("Y\n");
else printf("N\n");
}
}
}
Noip2018 考前准备的更多相关文章
- NOIP2018考前抱佛脚——图论基础复习
目录 存图方式 邻接矩阵存图 邻接表存图 链式前向星 最小生成树 例1 P1536 村村通 题目描述 输入输出格式 输入输出样例 标程 例2 P1546 最短网络 Agri-Net 题目背景 题目描述 ...
- NOIP2018考前抱佛脚——搜索复习
目录 搜索 DFS 例1 P1101 单词方阵 题目描述 输入输出格式 输入输出样例 标程 例2 P1605 迷宫 题目背景 输入输出格式 输入输出样例 标程 例3 P1019 单词接龙 题目描述 输 ...
- NOIP2018考前抱佛脚——数据结构基础及STL实现
目录 动态数组 栈 队列 优先队列 动态数组 srand(time(0)); std::vector<int> qwq; for(int i = 1;i <= 10;++i) qwq ...
- noip2018 考前提醒!
适应Noilinux 1.终端操作 打开终端 \(Ctrl+Alt+T\) 打开文件夹 \(cd\) +名称 新建文件夹 \(mkdir\) +名称 打开 \(vim\) 配置 \(vim ~/.vi ...
- NOIP2018 AFO记
仿佛"距NOIP2018还剩30天"的倒计时还在昨天,没想到这一天来的这么快. 考前在机房嘻嘻哈哈打打闹闹,开玩笑说道"我要AFO了",心里暗暗发誓做好自己,不 ...
- NOIP2018旅游记
2018.12.4更新: GD分数线出了,1=分数线310,1=了 好歹能和cyp交代了吧) 2018.11.28更新: 不好意思,太懒了,加上我也不记得后来发生什么了,总之就这样太监了. noip2 ...
- 初征——NOIP2018游记
前言 从最初接触oi到今年noip到来,也已经将近有一年了.从对于程序一窍不懂到现在开始学习算法,只是短短的不到一年的时间罢了.这次noip,不仅仅是我oi生涯的第一次noip,更是相当于是对我这一年 ...
- NOIP2018 Day0 回首向来萧瑟处,也无风雨也无晴
回首向来萧瑟处,也无风雨也无晴 NOIP2018 Day0 感想 by HGOI ljc20020730 Back ground: /* HGOI 陈功杰让我们写初赛总结?! (考这么烂还要写总结? ...
- 租酥雨的NOIP2018赛前日记
租酥雨的NOIP2018赛前日记 离\(\mbox{NOIP2018}\)只剩下不到一个月的时间辣! 想想自己再过一个月就要退役了,觉得有必要把这段时间的一些计划与安排记录下来. 就从国庆收假开始吧. ...
随机推荐
- Babel 处理 webpack 无法处理的 ES 语法(Babel 6.x 和 Babel 7.x 有区别)(二)
//这是main.js是我们项目的js入口文件 //1:a导入jQuery,引入jQuery包用$变量接收,此方法与node中 const $ = require('jquery') 同 //此为ES ...
- Python标准库time
原文:http://www.cnblogs.com/qq78292959/archive/2013/03/22/2975786.html Python官方文档 在程序中,免不了和时间打交道,要学习ti ...
- scrapy框架中Item Pipeline用法
scrapy框架中item pipeline用法 当Item 在Spider中被收集之后,就会被传递到Item Pipeline中进行处理 每个item pipeline组件是实现了简单的方法的pyt ...
- django-form and fields validation
参考资料 清除数据与表单验证 清除数据时会进行表单验证. 在表格处理时有三种clean方法可调用,通常是在对表单调用is_valid()时执行. clean响应:一般有两种结果,如果处理的数据有问题, ...
- js实现屏幕自适应局部
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- asp也玩三层架构(有源代码)
实体类 <% Class UserInfo Private mintId Public Property Let UserId(intUserId) mintId = intUserId End ...
- Java编程基础-方法
1.方法(函数)概要 (1).含义:方法(函数)就是定义在类中的具有特定功能的一段独立小程序. (2).方法定义的语法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参 ...
- jQuery1.6.1源码分析系列(作者:nuysoft/高云)
作者:nuysoft/高云 QQ:47214707 Email:nuysoft@gmail.com jQuery源码分析(版本1.6.1) 00 前言开光 01 总体架构 02 正则表达式-RegEx ...
- 【学习笔记】CSS优先级规则
CSS的优先级规则很多地方的说法都是错误的,常见错误说法是inline css>内部样式>外部样式,其实并没有这种规定.真正的CSS优先级确定是通过特性值大小确定的,在特性值大小相同的情况 ...
- 内存泄露,C++
内存泄露,C++通常是指new出来的内存没有delete掉.在你的代码里边,new了一块内存,然后马上就delete,自然就没有内存泄露了(呃……其实我也不确定啦,因为你用的是delete,而不是de ...