题目链接:http://lightoj.com/volume_showproblem.php?problem=1210 思路:首先是缩点染色,然后重建并且统计新图中的每个点的入度和出度,于是答案就是max(入度为0的点的个数, 出度为0的点的个数,这里有一个trick就是如果scc_count == 1,那么应该输出0. #include <iostream> #include <cstdio> #include <cstring> #include <algor…
Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10141   Accepted: 5031 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the r…
题目: 输入一个数字n  如果n为偶数则除以2,若为奇数则加1或者减1,直到n为1,求最少次数  写出一个函数 首先,这道题肯定可以用动态规划来解, n为整数时,n的解为 n/2 的解加1 n为奇数时,n的解为 (n+1)/2 和 (n-1)/2 的解中较小的解加2 通过这个思路,我们可以自底向上依次计算出n的解,代码如下 public static int getNum(int n) { if(n<1) { return 0; } int[] res = new int[n+1]; res[0…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到平均墙的高度(即最后墙的高度),遍历所有墙,如果小于平均墙,则用平均墙减去高度即是要移动的高度,统计所有需要"补"的高度即可.注意输出. AC Code: #include<bits/stdc++.h> using namespace std; int main() { ; w…
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not sincere non-interference. Like that show, now starvae also take part in a show, but it take place between city A and B. Starvae is in city A and girls a…
Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8637   Accepted: 3542 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt…
求一组N个数中的第k个最大者,设k=N/2. import java.util.Random; public class K_Max { /** * @param args */ //求第K大的数,保证K大于等于1,小于等于array.length/2哦 public static int TopK(int array[],int K) { int topk[] = new int [K]; for(int i = 0; i<topk.length;i++) topk[i] = array[i]…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=20的时候,不足四位,所以直接打表. 当n>20的时候,大于四位的时候,ans满足这个公式:ans=-0.5*log10(5.0)+num*1.0*log10((1+sqrt(5.0))/2.0); 这个公式是怎么来的呢?我们可以对an取10的对数,根据对数的性质. log10(ans)=log10(1/…
求1~n内全部数对(x,y),gcd(x,y)=质数,的对数. 思路:用f[n]求出,含n的对数.最后用sum[n]求和. 对于gcd(x,y)=a(设x<=y,a是质数),则必有gcd(x/a,y/a)=1;所以我仅仅要枚举i(设i=y/a),再枚举全部质数 他们乘积的f[i*a]值包含i的欧拉函数值. 时间复杂度(n*质数个数) #include<iostream> #include<cstring> using namespace std; const int maxx…
传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Exampl…