基本算法
zjy2015302395
ACM要AC
展开
-
字符串左对齐
#include int main(){ char s1[20],s3[20]; char s2[20],s4[20]; scanf("%s%s%s%s",s1,s2,s3,s4); printf("%-*10s", s1); printf("%-*10s", s2); printf("\n"); pri原创 2016-02-19 14:18:17 · 2566 阅读 · 0 评论 -
UVA1593 不定量不定长度单词,左对齐
找出每一列的最大长度。。一道题卡了一天,很水虽然,但是这个点想不到我真的做不了,因为不定量,不定长,所以只能用vector做才比较好 while(getline(cin,code)) { stringstream tran(code); //创建一个“字符串流”——tran,接下来只需要像读取cin那样读取tran即可! whi原创 2016-02-23 09:57:06 · 386 阅读 · 0 评论 -
c++输出对齐函数 setw()
int a=10; // 也可以是string a string b 字符串对齐的代码和int型其实是一样的,底下都不改,输出的格式也会右对齐,已经尝试过 int b=20; cout.setf(ios::right,ios::adjustfield); cout.fill('0'); cout cout setw() 属于在cout要求对齐时候比较方便的原创 2016-02-26 10:28:22 · 10441 阅读 · 0 评论 -
HDU2036三角形面积求法
typedef struct point{ int x,y;}point;point a[110];//n的范围限制double area(point p,point q){ return p.x*q.y-q.x*p.y;//叉乘计算面积的公式,简化的,是以(0,0)为起始点划分的}s = area / 2;原创 2016-02-26 17:08:15 · 428 阅读 · 0 评论 -
poj1511小灶第三次作业(dijstra)
历时三天,终于ac。。。第一次这么晚睡觉呵呵。。。http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109960#problem/F题意:直接翻译:有个出发点,然后有很多个公交站台,从一个站台到另一个站台需要花钱,求从初始点到所有站台,然后再回来 最少需要多少钱浓缩成题意:有n个点,m条边的有向图 ,求从初始原创 2016-03-25 01:23:07 · 796 阅读 · 4 评论 -
素数筛
#include #include const int MAXN=1e6+10;int prime[MAXN]={2,3,5};//素数筛void is_prime(){ int i,j; int flag=0; int gcd=2; int k=3; for(i = 7;i MAXN;i += gcd){ flag = 0原创 2016-02-19 14:52:52 · 347 阅读 · 0 评论 -
HDU1269 强连通Tarjan
http://acm.hdu.edu.cn/showproblem.php?pid=1269 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明可以通过这个通道由A房间到达B房间,但并不说明通过它可以由B房间到达A房间。Gardon需要请你写个程序确认一下是否任意两原创 2016-07-25 19:48:58 · 359 阅读 · 0 评论 -
区间dp
http://acm.hust.edu.cn/vjudge/contest/121485#problem/AThe multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of th原创 2016-07-25 20:03:04 · 550 阅读 · 0 评论 -
假期集训--线性dp
例题Ahttp://acm.hust.edu.cn/vjudge/contest/view.action?cid=121767#problem/AFigure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at th原创 2016-07-15 18:24:16 · 968 阅读 · 0 评论 -
#codeforces #369 div2
全场wa longlong 无颜见江东父老a题找两个连着的O变成+#include <cstdio>#include <cstring>#include <iostream>using namespace std;int n;char s[1010][5];void init(){ scanf("%d",&n); int flag = 0; for(int i =原创 2016-08-30 20:42:34 · 253 阅读 · 0 评论 -
陕西省集训(数位dp)
其实这篇文章写出来并不是陕西省集训这一系列该有的样子,只是呢,这种数位dp的写法确实是艾神所赐,我觉得不打上这个标头很对不起艾神,一直以来很怕数位dp,直到遇见这种递推的神奇dpA wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string “13”原创 2016-09-01 12:43:59 · 343 阅读 · 0 评论 -
陕西省集训 day4(搜索下)
A题:http://codeforces.com/problemset/problem/540/C题意:从起始点到终止点走,’ . ‘表示裂的冰,‘X’表示已经碎了不能走了(起点可以),’ . ‘走过变成’ X ‘,最后要求这个人的末位置冰碎掉。思路:就是说最后末尾位置如果是’。’,要绕出去再回来踩碎它。。。当时读题半个小时QAQ#include <iostream>#include <cstd原创 2016-08-24 01:51:31 · 375 阅读 · 0 评论 -
陕西省集训day3(搜索上)
A题:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175题意:简单版练练看,不能在外侧连,而且折线不能超过两次,中间当然也不能碰到别的东西,简化到每次只问一对,两次没有关系。思路:搜索的时候记得加上上一次的方向,加一个参数就行,如果变了方向就+1,不能超过2#include <iostream>#include <cstdio>#includ原创 2016-08-23 04:24:44 · 435 阅读 · 0 评论 -
陕西省集训day2(二分,贪心,枚举续)
I题:http://codeforces.com/problemset/problem/442/A题意:小明有25种牌,每张有花色和数值,现在他知道手里有哪些牌,但是不知道哪个是哪个。 旁边的人可以提示他花色,把某个花色的牌告诉他,或者数值,同理。求最少提示次数。思路:注释很清晰,用二进制代表花色或者数字是否已知#include<cstdio>#include <iostream>using n原创 2016-08-20 16:41:47 · 579 阅读 · 0 评论 -
陕西省集训之树形dp
doc老师给的树形dp的建议是一次dfs出来所有的解,而不是用记忆化搜索,每次都去dfs子解然后看是否算过了A题: http://poj.org/problem?id=2342There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has原创 2016-09-01 13:20:30 · 350 阅读 · 0 评论 -
陕西省集训(并查集)
并查集正常的思想就是每次输入的时候找到两个人的祖先,如果不是一个的话,把随便一个并给另一个,然后最后想知道有多少个不联营的区域就是相当于找多少个爸爸是自己的节点; int findroot(int i) { if(fa[i] == i) return i; return fa[i] = findroot(fa[i]);原创 2016-09-01 14:23:14 · 298 阅读 · 0 评论 -
陕西省集训(单调队列)
A题:题意:对于一个序列A[1…N],一共N个数,除去M个数使剩下的数组成的整数最小。 也就是说在A[1…N]中顺次选取N-M个数,使值最小。思路:单调队列,删除少于要求各节点的话比之前的数字小,就一定把之前的删除,让小的这个做第一个,这样就相当于维护一个单挑队列,先满足前面的尽量小,能删多少删多少#include <iostream>#include <cstdio>#include <cs原创 2016-09-01 15:56:33 · 317 阅读 · 0 评论 -
陕西省集训之树状数组
http://acm.hust.edu.cn/vjudge/contest/127376#overview 终于写到树状数组了啊,好开心 树状数组可以满足两种题型:1是单点询问区间修改,另一个是区间询问单点修改int lowbit(int x){ return x&(-x);}void add(int pos,int num){ for(int i = pos ; i <原创 2016-09-01 18:43:17 · 623 阅读 · 0 评论 -
2-sat
dfs模板ural 1382int n;int a[maxn], b[maxn], c[maxn];struct TwoSAT{ int n; vector <int> G[maxn*2]; bool mark[maxn*2]; int S[maxn*2], c; bool dfs(int x) { if(mark[x^1]) re原创 2016-08-24 11:01:30 · 296 阅读 · 0 评论 -
简单dp
http://www.lydsy.com/JudgeOnline/problem.php?id=1037 #include <cstdio>#include <iostream>#include <cstring>#define maxn 160using namespace std;long long n,m,k,dp[maxn][maxn][25][25],ans;//i个男的j个原创 2016-09-02 20:49:14 · 259 阅读 · 0 评论 -
组队赛之SPFA+记录路径?
以后要放弃bellman-ford了,tle哭到爆炸#include <cstdio>#include <iostream>#include <cstring>using namespace std;#define maxn 110#define maxm 500005int n,m,row,cl;int head[maxn];int ans ,ansdis;bool vis[ma原创 2016-08-24 20:35:36 · 315 阅读 · 0 评论 -
tarjan+缩点
之前有写过tarjan的模板,反正图论大大真是各种模板,比赛时候就不会,看出来也不会,看不出来更不会=。=,经常我们找到强连通分量之后不是数数个数就结束了,tarjan经常是帮别人一点而已,比如2-sat啊,桥啊,割点 啊什么的,这次只是简单的做了把连通块缩成一个点#include <cstdio>#include <cstring>#include <iostream>using names原创 2016-08-24 20:41:58 · 5412 阅读 · 0 评论 -
tarjan帮我来找桥
http://acm.hust.edu.cn/vjudge/contest/129458#problem/B 判断图中各个联通分量是不是只有中间一条桥,两边都是完全图#include <cstdio>#include <cstring>#include <iostream>#include <vector>#define maxn 110using namespace std;vec原创 2016-08-24 20:47:46 · 387 阅读 · 0 评论 -
hiho#1015 : KMP算法
http://hihocoder.com/problemset/problem/1015 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。这一天,他们遇到了一只河蟹,于是河蟹就向小Hi和小Ho提出了那个经典的问题:“小Hi和小Ho,你们能不能原创 2016-10-10 00:38:58 · 298 阅读 · 0 评论 -
vijos kmp next数组应用
https://vijos.org/p/1677背景 陶陶是一个智能机器人,他能像人一样思考问题,不过由于IQ问题,他给自己取了一个很长很长的名字。 描述 某一天,陶陶想把自己的名字涂在墙上。由于他的名字太长,为了省事,他从自己名字的开头截取了一段作为模板。我们不妨设这个模板的长度为l,陶陶的名字的长度为L,那么有1≤l≤L。然后陶陶会用这个模板进行若干次喷涂,喷出自己的名字(后一次喷涂会覆盖原创 2016-10-10 01:18:55 · 499 阅读 · 0 评论 -
最长回文子串(O(n) )Manacher算法
http://www.spoj.com/problems/MSUBSTR/ As we all know Utkarsh is very good at solving number based problems, this time Arpit thinks smartly and gives Utkarsh to solve a problem on Strings. Arpit gives原创 2016-10-10 01:59:48 · 463 阅读 · 0 评论 -
poj2391Ombrophobic Bovines
Floyd+二分+最大流(dinic)+拆点 貌似这样子显得很高端呢传送门大意:给定一个无向图,点 i 处有 Ai 头牛,点 i 处的牛棚能容纳 Bi 头牛,求一个最短时 间 T 使得在 T 时间内所有的牛都能进到某一牛棚里去。(1 <= N <= 200, 1 <= M <= 1500, 0 <= Ai <= 1000, 0 <= Bi <= 1000, 1 <= Dij <= 1,000,00原创 2016-09-28 02:42:24 · 347 阅读 · 0 评论 -
poj1149 pigs
http://poj.org/problem?id=1149大意:有 M 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依 次来了 N 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每 个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的 猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共 最多能卖出多少头猪。(1 <= N <= 1原创 2016-09-28 03:08:01 · 368 阅读 · 0 评论 -
#poj2699 The Maximum Number of Strong Kings
http://poj.org/problem?id=2699题意:有n(n<=10)个竞赛者,进行了n*(n-1)/2次的比赛。已知每一个竞赛者在比赛中胜利的次数a1、a2、a3……..an。(次序列是从小到大排序的),问你这n个人中最强的竞赛者最多有几个。(如果这个竞赛者胜利的次数最多或这个竞赛者打败了所有胜利次数比他多的竞赛者,那么这个竞赛者就是最强的。其中每两个人有且仅进行一场比赛。)思路:k原创 2016-09-29 00:54:26 · 286 阅读 · 0 评论 -
n个数中任意两个异或最大值
01字典树 把每一个数以二进制形式从高位到低位插入trie中,依次枚举每个数,在trie中贪心,即当前为0则向1走,为1则向0走。#include <stdio.h>#include <string.h>#define MAX(a,b) ((a)>(b)?(a):(b))#define NODE 3200010#define N 100010int n;int v[N];int no原创 2016-10-01 11:55:37 · 5488 阅读 · 0 评论 -
[kuangbin带你飞]专题四 最短路练习 A
Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of t原创 2016-11-29 23:18:31 · 343 阅读 · 0 评论 -
[kuangbin带你飞]专题四 最短路练习 B
真的不知道要败给读题到什么时候。。。http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to vis原创 2016-11-30 11:46:02 · 344 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索 A
Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。 Input 输入含有多组测试数据。 每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8原创 2016-11-18 16:09:09 · 336 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索B
Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit原创 2016-11-18 16:13:50 · 364 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索C
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0原创 2016-11-18 16:36:01 · 298 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索 D
Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15原创 2016-11-18 16:55:17 · 669 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索 E
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 2原创 2016-11-18 17:13:59 · 559 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索 F
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices. — It is a matter of se原创 2016-11-18 17:33:04 · 365 阅读 · 0 评论 -
[kuangbin带你飞]专题一 简单搜索 G
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing原创 2016-11-18 17:43:54 · 530 阅读 · 0 评论 -
kuangbin带你飞]专题一 简单搜索 H
http://poj.org/problem?id=3414 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: 1 FILL(i) fill the pot i (1 ≤ i ≤ 2) from th原创 2016-11-18 17:49:00 · 347 阅读 · 0 评论