您的位置:首页 > 其它

奇葩的各种算法

2013-09-05 15:47 288 查看
J***A大数速成

std::ios::sync_with_stdio(false);

加了这句cin cout就能和scanf差不多速度,但是加了这句就不能用scanf和printf

#pragma comment(linker, "/STACK:1024000000,1024000000")


汇编开栈:

int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p));


C fread读写挂

//////////////////////////
	const int MAXBUF = 10000;
	char buf[MAXBUF], *ps = buf, *pe = buf + 1;
	inline void rnext()
	{
		if (++ps == pe)
			pe = (ps = buf) + fread(buf, sizeof(char), sizeof(buf) / sizeof(char), stdin);
	}

	template <class T>
	inline bool rd(T &ans)
	{
		ans = 0;
		T f = 1;
		if (ps == pe) return false;//EOF
		do {
			rnext();
			if ('-' == *ps) f = -1;
		} while (!isdigit(*ps) && ps != pe);
		if (ps == pe) return false;//EOF
		do
		{
			ans = (ans << 1) + (ans << 3) + *ps - 48;
			rnext();
		} while (isdigit(*ps) && ps != pe);
		ans *= f;
		return true;
	}
	const int  MAXOUT = 100000;
	char bufout[MAXOUT], outtmp[50], *pout = bufout, *pend = bufout + MAXOUT;
	inline void write()
	{
		fwrite(bufout, sizeof(char), pout - bufout, stdout);
		pout = bufout;
	}
	inline void out_char(char c) { *(pout++) = c;if (pout == pend) write(); }
	inline void out_str(char *s) {
		while (*s) {
			*(pout++) = *(s++);
			if (pout == pend) write();
		}
	}
	template <class T>
	inline void pt(T x) {
		if (!x) {
			out_char('0');
			return;
		}
		if (x < 0) x = -x, out_char('-');
		int len = 0;
		while (x) {
			outtmp[len++] = x % 10 + 48;
			x /= 10;
		}
		outtmp[len] = 0;
		for (int i = 0, j = len - 1; i < j; i++, j--) swap(outtmp[i], outtmp[j]);
		out_str(outtmp);
	}
//////////////////////////在int main(){ write();return 0;}return 0;前一句一定要写write();
/*demo: 一旦使用pt要注意的三件事 1:要加write 2:不能使用别的任何输出语句 如puts putchar cout printf等 3:输出的文件大小不能超过MAXOUT
int main() {
	int n; rd(n); pt(n);
	write(); return 0;
}*/


C读写外挂:

template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if(c=getchar(),c==EOF) return 0;
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	sgn=(c=='-')?-1:1;
	ret=(c=='-')?0:(c-'0');
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
	ret*=sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}


计时黑科技:

clock_t lim = clock() + 1.9*CLOCKS_PER_SEC;//2s 所以设置上限是1.9
	if (clock() >= lim)break;


java读写外挂:点击打开链接

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;

public class Scanner {
    BufferedReader br;
    StringTokenizer st;

    public Scanner(InputStream in) {
        br = new BufferedReader(new InputStreamReader(in));
        eat("");
    }

    private void eat(String s) {
        st = new StringTokenizer(s);
    }

    public String nextLine() {
        try {
            return br.readLine();
        } catch (IOException e) {
            return null;
        }
    }

    public boolean hasNext() {
        while (!st.hasMoreTokens()) {
            String s = nextLine();
            if (s == null)
                return false;
            eat(s);
        }
        return true;
    }

    public String next() {
        hasNext();
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public BigInteger nextBigInteger() {
        return new BigInteger(next());
    }

    public int[] nextIntArray(int n) {
        int[] is = new int
;
        for (int i = 0; i < n; i++) {
            is[i] = nextInt();
        }
        return is;
    }

    public long[] nextLongArray(int n) {
        long[] ls = new long
;
        for (int i = 0; i < n; i++) {
            ls[i] = nextLong();
        }
        return ls;
    }

    public double[] nextDoubleArray(int n) {
        double[] ds = new double
;
        for (int i = 0; i < n; i++) {
            ds[i] = nextDouble();
        }
        return ds;
    }

    public BigInteger[] nextBigIntegerArray(int n) {
        BigInteger[] bs = new BigInteger
;
        for (int i = 0; i < n; i++) {
            bs[i] = nextBigInteger();
        }
        return bs;
    }

    public int[][] nextIntMatrix(int row, int col) {
        int[][] mat = new int[row][];
        for (int i = 0; i < row; i++) {
            mat[i] = nextIntArray(col);
        }
        return mat;
    }

    public long[][] nextLongMatrix(int row, int col) {
        long[][] mat = new long[row][];
        for (int i = 0; i < row; i++) {
            mat[i] = nextLongArray(col);
        }
        return mat;
    }

    public double[][] nextDoubleMatrix(int row, int col) {
        double[][] mat = new double[row][];
        for (int i = 0; i < row; i++) {
            mat[i] = nextDoubleArray(col);
        }
        return mat;
    }

    public BigInteger[][] nextBigIntegerMatrix(int row, int col) {
        BigInteger[][] mat = new BigInteger[row][];
        for (int i = 0; i < row; i++) {
            mat[i] = nextBigIntegerArray(col);
        }
        return mat;
    }
}

Java模板:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Queue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main {

	void work() throws Exception{
	}

    public static void main(String[] args) throws Exception{
        Main wo = new Main();
    	in = new BufferedReader(new InputStreamReader(System.in));
    	out = new PrintWriter(System.out);
  //  	in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
  //  	out = new PrintWriter(new File("output.txt"));
        wo.work();
        out.close();
    }

	static int N = 3*100050;
	static int M = N*N * 10;
	DecimalFormat df=new DecimalFormat("0.0000");
	static long inf = 1000000000000L;
	static long inf64 = (long) 1e18*2;
	static double eps = 1e-8;
	static double Pi = Math.PI;
	static int mod = 1000000009 ;
	
	private String Next() throws Exception{
    	while (str == null || !str.hasMoreElements())
    	    str = new StringTokenizer(in.readLine());
    	return str.nextToken();
    }
    private int Int() throws Exception{
    	return Integer.parseInt(Next());
    }
    private long Long() throws Exception{
    	return Long.parseLong(Next());
    }
    StringTokenizer str;
    static BufferedReader in;
    static PrintWriter out;
    /*
	class Edge{
		int from, to, nex;
		Edge(){}
		Edge(int from, int to, int nex){
			this.from = from;
			this.to = to;
			this.nex = nex;
		}
	}
	Edge[] edge = new Edge[M<<1];
	int[] head = new int
;
	int edgenum;
	void init_edge(){for(int i = 0; i < N; i++)head[i] = -1; edgenum = 0;}
	void add(int u, int v){
		edge[edgenum] = new Edge(u, v, head[u]);
		head[u] = edgenum++;
	}/**/
	int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;
		int pos = r;
		r--;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (A[mid] <= val) {
				l = mid + 1;
			} else {
				pos = mid;
				r = mid - 1;
			}
		}
		return pos;
	}

	int Pow(int x, int y) {
		int ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	double Pow(double x, int y) {
		double ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	int Pow_Mod(int x, int y, int mod) {
		int ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			ans %= mod;
			y >>= 1;
			x = x * x;
			x %= mod;
		}
		return ans;
	}
	long Pow(long x, long y) {
		long ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	long Pow_Mod(long x, long y, long mod) {
		long ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			ans %= mod;
			y >>= 1;
			x = x * x;
			x %= mod;
		}
		return ans;
	}

	int Gcd(int x, int y){
		if(x>y){int tmp = x; x = y; y = tmp;}
		while(x>0){
			y %= x;
			int tmp = x; x = y; y = tmp;
		}
		return y;
	}
	long Gcd(long x, long y){
		if(x>y){long tmp = x; x = y; y = tmp;}
		while(x>0){
			y %= x;
			long tmp = x; x = y; y = tmp;
		}
		return y;
	}
	int Lcm(int x, int y){
		return x/Gcd(x, y)*y;
	}
	long Lcm(long x, long y){
		return x/Gcd(x, y)*y;
	}
	int max(int x, int y) {
		return x > y ? x : y;
	}

	int min(int x, int y) {
		return x < y ? x : y;
	}

	double max(double x, double y) {
		return x > y ? x : y;
	}

	double min(double x, double y) {
		return x < y ? x : y;
	}

	long max(long x, long y) {
		return x > y ? x : y;
	}

	long min(long x, long y) {
		return x < y ? x : y;
	}

	int abs(int x) {
		return x > 0 ? x : -x;
	}

	double abs(double x) {
		return x > 0 ? x : -x;
	}

	long abs(long x) {
		return x > 0 ? x : -x;
	}

	boolean zero(double x) {
		return abs(x) < eps;
	}
	double sin(double x){return Math.sin(x);}
	double cos(double x){return Math.cos(x);}
	double tan(double x){return Math.tan(x);}
	double sqrt(double x){return Math.sqrt(x);}
}


归并排序(AC),货郎担问题(AC),黑白染色(已解),最小树形图http://blog.csdn.net/wsniyufang/article/details/6747392(有向图的最小生成树)(AC) , 模拟退火,差分约束,旋转卡壳,种类并查集/article/1969953.html 计算几何(已解)
RMQ(AC),扫描线(AC)

http://cpp.zjut.edu.cn/ShowProblem.aspx?ShowID=1872 面积并http://blog.csdn.net/jasonzhu8/article/details/6010980 http://www.cnblogs.com/ch3656468/archive/2011/03/28/1997443.html

网络流典题http://acm.sgu.ru/problem.php?contest=0&problem=438(AC)

上下界网络流http://hi.baidu.com/newfarking/item/b9780317201c9651f0090e64?qq-pf-to=pcqq.c2c(AC)

可持续化treapgithub.com/9974/DataStructures/blob/master/treap/%E5%8F%AF%E6%8C%81%E4%B9%85%E5%8C%96treap.cpp

最大权闭合图(AC)

自适应simpson 求积分 && 变步长积分法
树重心:找到一个节点u作为root,使得root的所有孩子的节点数的最大值最小。那么这个点就是重心 http://www.docin.com/p-589701802.html#documentinfo 例题:POJ1655(AC)
扩展KMPwww.isnowfy.com/kmp-and-extend-kmp/

费马点

伸展树http://www.cnblogs.com/kernel_hcy/archive/2010/03/17/1688360.html (AC)
博弈树
后缀数组
在线ac自动机http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1007&cid=487(AC)
哈夫曼树
最小支配集问题

中国邮递员问题:一般图中国邮递员问题算法是 (用状压DP+floyd,AC) 求一般图最小权匹配, 完全图直接公式
最小权匹配算法(KM,AC)
一般图匹配 :带花树http://www.cnblogs.com/kuangbin/archive/2013/08/23/3278621.html?ADUIN=654889339&ADSESSION=1385702093&ADTAG=CLIENT.QQ.5239_.0&ADPUBNO=26248
上下界的最大流 (AC)
最近点对
莫队算法:点击打开链接
网络流:带下界的可行流,最大可行流,最小可行流,带下界的费用流:点击打开链接

treap(树堆) (AC)

vector<ll>G;
sort(G.begin(),G.end());
G.resize(unique(G.begin(),G.end())-G.begin);


斐波那契数列 前n项和 Sum(n) = f(n+2)-1 (AC)

二次剩余
环状树形dp
双调队列(AC)
------------------------
(以下来自BIT神牛所授。。
把以1为根的树:
5
1 2
1 3
3 4
3 5
dfs得到欧拉序列是 1 2 1 3 4 3 5 3 1
那么以1为根,欧拉序列就是 1 2 1 3 4 3 5 3 1
复制一下自己 ,得到:
1 2 1 3 4 3 5 3 1 2 1 3 4 3 5 3 1

此时。如果将根变换为2,则欧拉序列就是
1 2 1 3 4 3 5 3 1 21 3 4 3 5 3 1

即,如果是以i点为根,则欧拉序列就是i点第一次出现的位置到后面9个。则用树状数组维护一下这个序列就能解决HDU4836了

-------------------------

#include<functional>



#include<queue>

using namespace std;



priority_queue<int, vector<int>, greater<int> > q;


各种Hash
博客搬家
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: