
UVa
文章平均质量分 68
i逆天耗子丶
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
UVa - 536 - Tree Recovery(给定中序和先序求后序,递归)
思路:由先序遍历可得到根节点。在中序遍历中找到根节点的位置,然后分别递归左子树和右子树,一般的思路到这里是递归建树,但是由于是要求后序遍历的序列。是左孩子+右孩子+根节点,而递归建树时是通过找到根节点,然后先递归左子树,然后递归右子树 ,所以直接输出的就是后序遍历的结果。 #include #include using namespace std; char s1[30],s2[30]; //先原创 2016-10-08 16:59:23 · 491 阅读 · 0 评论 -
UVa - 10369 - Arctic Network ( 最小生成树 Kruscal )
The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every o原创 2016-12-16 11:56:45 · 441 阅读 · 0 评论 -
UVa - 10608 Friends (并查集)
题目大意: 朋友的朋友也是朋友,t 组测试用例 ,每组给出 n 和 m 分别代表 n 个人 ,m 个关系 ,接下来 m 行,每行给出 2 个数字 ,代表这两个人是朋友。求朋友组成的最大集体。 思路: 并查集 。 #include #include #include #define N 30001 using namespace std; int p[N]; int num[N];原创 2016-12-09 20:46:37 · 954 阅读 · 0 评论 -
UVa - 11400 - Lighting System Design(线性动态规划)
题目意思是 小灯泡(低电压)可以换成大灯泡,但是需求的数目不变,一种灯泡只买一个电源就可以。 举个例子 灯泡a和b。 a 电压 1 电源 50元 单价 5 需要 5 b 电压 2 电源 20元 单价 6 需要 4 买小灯泡的话,需要50+5*5=75 加上大灯泡的20+4*6+75 = 119元 如果把小灯泡换成大灯泡,虽然单价大灯泡贵,但是就不用买小灯泡的电源,就需要20+(5原创 2016-12-05 10:07:38 · 764 阅读 · 0 评论 -
UVa - 11854 - Partitioning by Palindromes (线性动态规划)
给定一个字符串,问:最少能分割成几个子字符串,并且每个字符串都是回文串。 设:dp [ i ] 是前 i 个字符能分割的最少个数。 动态转移方程: dp [ i ] = max ( dp [ i ] , dp[ j ] +1) ; 其中 j 是 0 ~ i 之间的数 ,执行转移方程的条件是 j~i 的字符串是回文串 #include #include #include #def原创 2016-12-05 18:36:02 · 441 阅读 · 0 评论 -
HDU - 1757 - A Simple Math Problem ( 矩阵快速幂 )
Problem Description Lele now is thinking about a simple function f(x). If x If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); And ai(0 Now, I will give a0 ~ a9原创 2016-12-13 11:08:17 · 412 阅读 · 0 评论 -
UVa - 11292 - The Dragon of Loowater ( 贪心 )
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into a major problem. The shores of Rellau Creek in central Loowater had always been a prime b原创 2016-05-14 10:16:35 · 514 阅读 · 0 评论 -
UVa - 10870 - Recurrences ( 矩阵快速幂 )
题目大意: f(n) = a1f(n − 1) + a2f(n − 2) + a3f(n − 3) + . . . + adf(n − d), for n > d, 最关键的就是题目中的这句话,当n>d的时候,递推关系如上 如果n 思路:写出递推矩阵:根据递推矩阵写方程即可。 #include #include #define LL long原创 2016-12-12 22:15:05 · 586 阅读 · 0 评论 -
UVa - 10158 - War ( 并查集 )
A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country’s espionage by attending the peace-talks taking place these days (incognito, of course). Th原创 2016-12-11 20:53:10 · 522 阅读 · 0 评论 -
UVa - 10227 - Forests ( 并查集 )
楼主英文不好,题目理解是个梗。。。。 题目不难,数据处理比较麻烦。 题目大意是:有 p 个人 , t 棵树 ,在森林中有书倒下,每个人听到自己感觉是某棵树倒下。 现在给出这些人的猜想。比如说编号为1 的人听到 2 ,3号树倒下,编号为2的人听到2号树倒下,编号为3的人听到2,3号树倒下,那么这三个人就有2种观点,一种观点是2,3号树倒下,一种是2号树倒下,题目就要求输出有几种观点。观原创 2016-12-10 22:52:11 · 560 阅读 · 0 评论 -
UVa - 539 - The Settlers of Catan ( DFS 回溯 )
Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities across its uncharted wilderness. You are employed by a sof原创 2016-12-14 14:25:16 · 668 阅读 · 0 评论 -
UVa - 10048 - Audiophobia ( Floyd 变形 )
题目大意:从a城市到b城市的路径中,尽可能让一路上的最大噪音最小。 题目思路:设d [ i ][ j ]表示 i 到 j 的最大噪音的最小值。 那么d [ i ][ j ] = min( d[ i ][ j ] ,max( d [ i ][ k ] , d [ k ][ j ]) ); #include #include #include #define N 105原创 2016-12-19 22:12:38 · 600 阅读 · 0 评论 -
UVa - 247 - Calling Circles ( Floyd 传递闭包 )
题目大意: 题目中给出 n 的人的名字,m组关系,表示前者给后者打电话 。如果两个人互相打过电话(直接或者间接),那么这两个人在一个集合。现在要求出所有集合中的人,输出格式看输出实例。 题目思路: 设d[ i ] [ j ] 表示 i 和 j 通话过,如果 d[ i ] [ j ] && d[ j ] [ i ] 那么说明 i 和 j 属于同一个集合。 d[ i ][ j ]原创 2016-12-19 21:01:19 · 647 阅读 · 0 评论 -
UVa - 11300 - Spreading the Wealth ( 数学推导 )
题目大意: n和村庄各自有自己的金币,相邻村庄可以互相给金币。为了实现共产主义,现在要实现每个村庄的金币数量都相同。求出最小的金币转移量。 题目思路: 1.设 a[ i ] 表示第 i 个村庄有的金币。 2.设 b[ i ] 表示第 i 个村庄给 i-1个村庄的金币。 3.设 M 表示最后每个村庄有的金币 则可以列出方程式 4.原创 2016-12-27 21:58:23 · 532 阅读 · 0 评论 -
UVa - 11729 - Commando War ( 排序+贪心 )
#include #include #define N 1001 using namespace std; struct Soldier{ int a,b; }s[N]; bool cmp(Soldier s1 ,Soldier s2){ return s1.b>s2.b; } int cnt = 0; int a,b,n; int main(){ while(scanf("%d",&n原创 2016-12-27 10:57:24 · 444 阅读 · 0 评论 -
UVa - 11292 - Dragon of Loowater ( 排序 + 贪心 )
#include #include #define N 20001 using namespace std; int a[N],b[N]; int n,m; int main(){ while(scanf("%d%d",&n,&m)==2&&n&&m){ for(int i=0 ;i<n ;i++) scanf("%d",&a[i]); for(int i=0 ;i<m ;i++)原创 2016-12-27 09:16:09 · 451 阅读 · 0 评论 -
UVa - 439 - Knight Moves(bfs求最短路)
#include #include #include #include using namespace std; int x1,y1,x2,y2,ans; string str1,str2; //用来标记是否已经访问过 int vis[10][10]; //八个方向 int dis[8][2] = {-1,2 ,1,2 ,2,1 ,2,-1 ,1,-2 ,-1,-2 ,-2,-1 ,-2,1}原创 2016-10-08 20:08:15 · 784 阅读 · 0 评论 -
Uva - 116 - Unidirectional TSP(多段图的最短路,动态规划)
Background Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing w原创 2016-12-01 22:35:11 · 733 阅读 · 0 评论 -
UVa - 12563 - Jin Ge Jin Qu hao(01背包,动态规划)
题目大意就是在KTV唱歌,剩下时间t秒,给定歌n首,选择尽可能多的歌曲能在规定时间内唱,但是要尽可能的晚离开KTV,所以至少要留1秒来点“劲歌金曲”,那么01背包问题就出来了,t-1秒内,选择尽可能多的歌曲,并且曲目数相同情况,尽可能时间长一点。 设 dp1[ i ] 是 i 秒内最多能唱的曲目数 状态转移方程 dp1[ i ] = max(dp1[ i ] , dp1[ i- v原创 2016-12-04 20:11:29 · 1181 阅读 · 0 评论 -
UVa - 1220 - Party at Hali-Bula ( 树形DP 求最大独立集 )
题目大意:有1个大boss,n-1个员工,每个员工只有一个直接上司,一个领导可以有多个员工,现在矩形一个聚会,有直接关系的人不会同时出现,问最多能请多少个人,情况是否唯一。 题目思路:树形动态规划,求最带独立集, 1.设dp[ i ][ 0 ] 表示不选择编号为 i 人能得到的最大独立集的人数 dp[ i ][ 0 ] = sum(dp[ j ][ 0 ] , dp[ j ][ 1 ]);原创 2016-12-14 23:37:39 · 505 阅读 · 0 评论 -
UVa - 12186 - Another Crisis ( 树形DP )
题目大意:在公司里面,有一个大boss,有员工,和领导,除了大boss一个人只有一个直接的领导 ,员工不是任何人的领导。现在要求老板加薪,当大boss的直接手下超过T%的人都反映加工资的时候,大boss才会同意,同理,每个领导也是一样的;问,如果要让领导同意加工资的话,至少需要多少员工反映加薪问题。 题目思路:设dp[ u ] 是 让 u 给上级发信,至少需要多少员工,那么,把 dp [ u原创 2016-12-15 22:36:50 · 526 阅读 · 0 评论 -
UVa - 1395 - Slim Span(Kruskal算法+并查集,最小生成树)
思路:题目要求求出边权值的最大值和最小值的差值,该差值是最小的。最小生成树,Kruskal算法和Prim算法其中Kruskal算法中的贪心策略,将边权从小到大排列,因此用Kruskal算法来求解。在生成最小生成树的时候,在范围[L,R]中,其中L是最小边权,将求权值的和改为求w[L]-w[R],然后一次更新L的值,遍历所有的情况,更新最小值即可。 注:其中给出的结点的编号是从1开始的,所以并查集原创 2016-10-28 21:22:36 · 892 阅读 · 0 评论 -
UVa - 548 - Tree(从中序和后序恢复二叉树)
输入有多组,每组包含两行,第一行是二叉树的中序遍历,第二行是后序遍历。每个数字代表结点的值,输出某个叶子结点的值,表示从根节点到该叶子节点经过的结点的和最小(包括根节点和叶子结点),如果有多个相同,那么输出叶子结点的值最小的那个结点的值。 思路:根据题目中给出的中序和后序递归建树。dfs遍历所有的情况和,记录最小的情况。 #include #include #include #incl原创 2016-09-28 15:44:50 · 454 阅读 · 0 评论 -
UVa - 10305 - Ordering Tasks(拓扑排序DFS)
拓扑排序DFS模版 bool dfs(int u){ vis[u]=-1;// 访问标志, //-1正在访问 1访问完成 0没有访问 for(int v=1 ;v<=n ;v++){ if(G[u][v]){ //存在环路 if(vis[v]<0)return false; if(!vis[v]&&!dfs(v))return false; } } vis原创 2016-10-03 16:18:33 · 477 阅读 · 0 评论 -
UVa - 839 - Not so Mobile(二叉树的DFS)
bool Right(int& w){ int wl,dl,wr,dr; bool b1 = true; bool b2 = true; cin>>wl>>dl>>wr>>dr; if(!wl) b1 = Right(wl); if(!wr) b2 = Right(wr); w = wl + wr; return b1&&b2&&(wl*dl==wr*dr); } 输原创 2016-09-29 19:47:43 · 400 阅读 · 0 评论 -
UVa - 1339 - Ancient Cipher(排序)
思路:题目的大概意思是对信息的加密。加密方法有两种。 第一种:Substitution cipher 将字母往后推1位或者更多,如A往后推一位是B。(可以理解为字母的映射),需要注意的是 不同的字母可以有不同的映射方式,如,A字母往后推一位,B字母往后推2位,那么A对应B, B对应D。 第二种:Permutation cipher 将字母进行重排列。 1.因为字母可以重排原创 2016-09-20 14:28:19 · 619 阅读 · 0 评论 -
UVa - 489 - Hangman Judge(刽子手游戏)
#include #include using namespace std; char s1[1000]; char s2[1000]; int n; int win,lose,chance,left; int main(){ void guess(char c); while(scanf("%d",&n)!=EOF){ if(n==-1){ break; }el原创 2016-09-19 14:02:39 · 477 阅读 · 0 评论 -
UVa - 133 - The Dole Queue(救济金发放)
Description In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be原创 2016-09-19 20:43:01 · 557 阅读 · 0 评论 -
UVa - 1587 - BOX
思路:由于出入的w和h不一定哪个大,给数据的处理带来麻烦,所以,先用结构题来存储w和h,并且数据存储要求为w>h,存储到结构体的数组中后,对现有的数据进行排序,排序的规则是,w不同时,按照w升序,否则按照h升序。 长方体的6个面的特点:两两面相同,ab,ab,ac,ac,bc,bc。 (其中 a=1,b=2,c=6) 。在编码中进行判断即可。 #include #include us原创 2016-09-19 11:23:54 · 3187 阅读 · 1 评论 -
UVa - 10340 - All in All(暴力遍历、子序列)
#include #include using namespace std; char s1[101000]; char s2[101000]; int main(){ // freopen("input.txt","r",stdin); while(scanf("%s %s",s1,s2)!=EOF){ int len1 = strlen(s1); int len2 = strl原创 2016-09-19 10:23:06 · 424 阅读 · 0 评论 -
UVa - 1586 - Molar mass(char s[n];scanf("%s",s);)
#include #include #include #include #include using namespace std; double weight[200]; int main(){ weight['C']=12.01; weight['H']=1.008; weight['O']=16.00; weight['N']=14.01; // freopen("input.tx原创 2016-09-17 12:02:59 · 358 阅读 · 0 评论 -
UVa - 227 - Puzzle(gets(),scanf("%c",&x))
思路:简单模拟,按照题目意思进行编码即可。 #include using namespace std; char m[5][5]; char cmd[100]; int main(){ // freopen("input.txt","r",stdin); int b_x ,b_y ,cases=0; while(gets(m[0])){ if(m[0][0]=='Z')原创 2016-09-18 20:46:55 · 460 阅读 · 0 评论 -
UVa - 1225 - Digit Counting(打表计算)
Description Trung is bored with his mathematics homeworks. He takes a piece of chalk and starts writing a sequence of consecutive integers starting with 1 to N (1 N . After that, he counts原创 2016-09-17 13:53:39 · 426 阅读 · 0 评论 -
UVa - 1585 - Score
Description There is an objective test result such as ``OOXXOXXOOO". An `O' means a correct answer of a problem and an `X' means a wrong answer. The score of each problem of this test is calcul原创 2016-09-13 22:29:33 · 623 阅读 · 0 评论 -
UVa - 156 - Ananagrams(STL - 映射map)
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters in different orders--for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have原创 2016-09-21 15:28:54 · 508 阅读 · 0 评论 -
UVa - 10815 - Andy's First Dictionary(STL - 集合set)
#include #include #include #include using namespace std; int main(){ //freopen("input.txt","r",stdin); set dict; string s,buf; while(cin>>s){ for(int i=0 ;i<s.length() ;i++){ if(isalp原创 2016-09-21 14:20:34 · 473 阅读 · 0 评论 -
UVa - 540 - Team Queue(STL - 队列queue)
#include #include #include using namespace std; int main(){ // freopen("input.txt","r",stdin); int t,k=0,n,temp; while(scanf("%d",&t)!=EOF){ mapteam; queue q,q1[1010]; if(t==0)break; k++;原创 2016-09-24 21:35:37 · 618 阅读 · 0 评论 -
UVa - 572 - Oil Deposits(dfs求连通块)
#include #include #include using namespace std; int m,n; int dir[8][2] ={1,-1,1,0,1,1,-1,-1,-1,0,-1,1,0,-1,0,1}; char map[105][105]; bool vis[105][105]; void dfs(int x,int y){ for(int i=0 ;i<8 ;i++)原创 2016-10-03 14:06:10 · 535 阅读 · 0 评论 -
UVa - 673 - Parentheses Balance(栈-stack)(Java和C++)
Java代码(没有用栈) import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); for(int i=0 ;i原创 2016-10-07 21:18:47 · 871 阅读 · 0 评论 -
UVa - 442 - Matrix Chain Multiplication(用栈实现简单的表达式解析)
#include #include #include #include #include using namespace std; struct Matrix{ int a,b; // Matrix(int a=0,int b=0):a(a),b(b){} // Matrix(int a,int b){ // this.a=a; // this.b=b; // } }m[26]; i原创 2016-09-25 16:39:12 · 504 阅读 · 0 评论