hdu 4996 1~n排列LIS值为k个数】的更多相关文章

http://acm.hdu.edu.cn/showproblem.php?pid=4996 直接贴bc题解 按数字1-N的顺序依次枚举添加的数字,用2N的状态保存在那个min数组中的数字,每次新添加数字可以根据位置计算出新的min数组. 怎么快速计算呢?这里如果枚举N的位置是不可行的,这样2n的state记录的信息不够.很巧妙的思路是枚举放在当前位置的数字,比如说1-N-1的排列状态下,枚举第N位为K,那么1-N-1位的>=k的数字全加1,就得到了一个1-N的排列. 当然这个算法是有问题的,但…
Necklace Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2423    Accepted Submission(s): 766 Problem Description SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang e…
http://acm.hdu.edu.cn/showproblem.php?pid=3664 求1~n的排列个数,使得逆序数(ai>i ) 为给定的k. dp[i][j]表示前1~i的排列中,有j个数是逆序数的个数. #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <string> #include <queue>…
这种题为什么要用到主席树啊..8说了,直接上代码 /* 1-n的排列,给定所有前缀的逆序对数量,要求恢复排列 首先能确定最后一个数是什么,然后倒序确定即可 开线段树找空位:如果Ai-Ai-1=k,说明pi前面有k个数比它要大,,即有i-k-1个数比它小, 那么pi排在第i-k位,线段树查询第i-k个空位,然后把这个空位填上即可 */ #include<bits/stdc++.h> using namespace std; #define maxn 50005 int n,a[maxn],ans…
Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1959    Accepted Submission(s): 1006 Problem Description There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i…
这是在面试常遇到的topk问题. 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 解题思路: 思路一:用快排对数组做一次排序,输出前k个.这种方法会改变原数组排列,复杂度为O(nlogn). 思路二:基于partition思想,进行一次快速排序用哨兵数分割数组中的数据.由于partition每次将数组分成两段,左半部分小于关键字,右半部分大于关键字.可以递归做partition,关键字位置,若关键字位置等于k…
题目一(输出国际象棋棋盘)  分析: 用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格.   主要代码: for i in range(8): for j in range(8): if (i+j)%2!=0: print(chr(219)*2,end='') else: print(' ',end='') print('') 题目二(排列组合问题)   有1.2.3.4个数字,能组成多少个互不相同且无重复数字的四位数?都是多少?   分析:  我们可以先预测一下,共有2…
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / \ 1 5 / \ \ 5 5 5 return 4. 这道题让我们求相同值子树的个数,就是所有节点值都相同的子树的个数,之前有道求最大BST子树的题Largest…
继续学习,这一篇主要是通过scala来吐槽java的,同样是jvm上的语言,差距咋就这么大呢? 作为一个有.NET开发经验的程序员,当初刚接触java时,相信很多人对java语言有以下不爽(只列了极小一部分): 1. 一堆的setter/getter方法,没有c#中的property属性概念 2. 方法的参数值,不能设置缺省值 3. 不定个数参数的写法太单一 ... 然后java的拥护者讲出一堆大道理,说这样设计是如何如何有道理,各种洗脑,时间长了,也就被迫习惯了.要不是遇到scala,我还真就…
1. 问题描写叙述 给定一个单链表,删除当中值为k的全部节点.比如:1→2→6→3→4→5→61 \to 2 \to 6 \to 3 \to 4 \to 5 \to 6,删除当中值为6的节点,返回:1→2→3→4→51 \to 2 \to 3 \to 4 \to 5 . 2. 方法与思路 这是一个简单的链表操作题.删除是要考虑的节点所在的位置(头部,中间和尾部),分开处理一下好了.主要步骤例如以下描写叙述: I. while head−>val≠targetwhile head->val \n…
No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2000    Accepted Submission(s): 851 Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yours…
CA Loves GCD  Accepts: 64  Submissions: 535  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 262144/262144 K (Java/Others) 问题描述 CA喜欢是一个热爱党和人民的优秀同♂志,所以他也非常喜欢GCD(请在输入法中输入GCD得到CA喜欢GCD的原因). 现在他有N个不同的数,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去. 为了使自己不会无聊,CA…
给定一个数列a1,a2,a3,...,an和m个三元组表示的查询,对于每个查询(i,j,k),输出ai,ai+1,...,aj的升序排列中第k个数. #include <iostream> using namespace std; #define SIZE 20 #define M 3 typedef struct Elem { int i, j; int k; } ELEM; /* 表示一个元素的三元组结构 */ int getMedian(int* arr, int low, int hi…
E. Beautiful Subarrays   One day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an. A subarray of the array a is a sequence al,  al  +  1,  ...,  ar for some integers (l,  r) such that 1  ≤  l  ≤  r  ≤  n. ZS the Coder…
统计numpy数组中某一个值或某几个值出现的个数:sum(data==4) # 统计出现了几个cluster include0Cluster = sum(res == 0) include1Cluster = sum(res == 1) include2Cluster = sum(res == 2) include3Cluster = sum(res == 3) include4Cluster = sum(res == 4) include5Cluster = sum(res == 5) inc…
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of the subtree have the same value. For example:Given binary tree, 5 / \ 1 5 / \ \ 5 5 5 return 4. 给一个二叉树,求唯一值子树的个数.唯一值子树的所有节点具有相同值. 解法:递归 Java: /** * De…
/*链表返回倒数第k个数的位置的值*/ /* 算法思想:先取得链表的长度len,之后获取len-k+1的位置元素的值 */ Elemtype Getelem_rear(LinkList L, int k) { ) ; int len = getLength(L); ; LNode *p = L; ) { p = p->next; i--; } cout << "倒数第"<<k<<"个数的位置的值是:" << p-…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4996 题意:求1到n的全排列中,有多少个排列的最长上升子列长度为K? 思路:对于当前的最长上升子列,我们记录最后一个值得最小值即可.因此我们用2^n的状态表示当前最长上升子列中使用了哪些数字,且字典序最小.前n-1个数字之后,我们枚举最后一个位置的数字为[1,n]中每个数字,设为k,那么我们只要将前面[1,n-1]组成的数列中所有大于等于k的数字加一即可. int n,k; i64 f[22][22…
XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2422    Accepted Submission(s): 990 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful…
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往根的方向走,可以得到他的祖先序列,现在需要从v1点的祖先序列中挑选出一定数量的点,组成数列v1,v2,v3……vm,要求vi是vi-1的祖先,求dp[v1]=max(dp[vi]+(w[v1] opt w[vi])),opt是一种运算,在题目中可为xor,or或者and,最后求出ans=sum_{i…
题目链接:https://vjudge.net/contest/228455#problem/A 题目大意: 有2n个城市,其中有n个富有的城市,n个贫穷的城市,其中富有的城市只在一种资源富有,且富有的城市之间富有的资源都不相同,贫穷的城市只有一种资源贫穷,且各不相同,现在给出一部分贫穷城市的需求,每个需求都是一个贫穷的向一个富有的城市要资源,且每个富有的城市都想向贫穷的城市输入自己富有的那部分资源,现在为了运输要建设多条路,但是路与路之间不允许有交叉,求满足贫穷城市的各种要求最多可以建设多少条…
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1853 Cyclic Tour Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others) Total Submission(s): 1904    Accepted Submission(s): 951 Problem Description There are N cities in our c…
题目链接 Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. Define that the domain of function f is the set of integers from 0 to n−1, and the range of it is the set of integers from 0 to m−1. Please calcul…
简易版之最短距离 点我跳转到HDOJ Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16700 Accepted Submission(s): 7481 Problem Description 寒假的时候,ACBOY要去拜访很多朋友,恰巧他所有朋友的家都处在坐标平面的X轴上.ACBOY可以任意选择一个朋友的家开始访问,但是每次访问后他都必须…
最近在线段树的世界里遨游,什么都能用线段树做,这不又一道权值线段树了么. ZYB's Premutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1029    Accepted Submission(s): 528 Problem Description ZYB has a premutation P ,but he o…
http://acm.hdu.edu.cn/showproblem.php?pid=1160 同样是先按它的体重由小到大排,相同就按speed排就行. 这样做的好处是,能用O(n^2)枚举,因为前面的肯定不能和后面的搭配了. 然后就是LIS的题了, #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #defin…
方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7717    Accepted Submission(s): 2911 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数…
题意: 给一个数字序列,要求找到LIS,输出其长度. 思路: 扫一遍+二分,复杂度O(nlogn),空间复杂度O(n). 具体方法:增加一个数组,用d[i]表示长度为 i 的递增子序列的最后一个元素,且该元素总是保持当前最小.初始化d[1]=A[i],当前LIS的长度len=1.从 2 to n,若A[i]>d[len],则d[++len]=A[i],否则,在数组d中找到A[i]应该插入的位置,代替掉那个第一个比它大的数字,比如d[k]<A[i]<=d[k+1],直接将A[i]代替掉d[…
Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3299    Accepted Submission(s): 1674 Problem Description On a grid map there are n little men and n houses. In each unit time, every l…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; ; int G[maxn][maxn]; int dp[maxn]; //dp[i]表示前i个人,最大有多少个人没撒谎. int main() { //freopen(…