实验室通宵刷题模式

2013-7-26  19:00

一百道!!!!


pre:python 写的EOJ比较题的工具

import urllib2
import sgmllib
import sys
import string

class LinksParser(sgmllib.SGMLParser):
	urls = []

	def do_a(self, attrs):
		for name, value in attrs:
			if value.startswith('problem.php?problemid='):
				s = value[22:]
				t = string.atoi(s)
				self.urls.append(t)
				#print s,t
			'''
			if name == 'div' and value not in self.urls:
				if value.startswith('http'):
					self.urls.append(value)
					#print value
			else:
				continue
			return
			'''
		#return urls

if __name__ == "__main__":
	# str = ""
	# if str.strip() is '':
		# print "str is None"
	# else:
		# print "str is no None"

	myid = 'rgtjf'
	otherid =  '10072130237'
	url = 'http://acm.cs.ecnu.edu.cn/userinfo.php?username='
	problemurl = 'http://acm.cs.ecnu.edu.cn/problem.php?problemid='
	mysets = []
	othersets = []
	notsolved = []

	filename = 'file'
        
        #输入id
	'''
	myid = raw_input('myid')
	otherid = raw_input('otherid')
	'''

	#shuchu chong dingxiang
	temp0 = sys.stdout
	sys.stdout = open(filename,'w')
	temp1 = sys.stdout
        #temp0为console
        #temp1为file
        sys.stdout = temp0


	p =  LinksParser()
	try:
		f = urllib2.urlopen(url+myid)
	except URLError,e:
		print e.reason()
	value = f.read()
	p.feed(value)
	mysets = p.urls[:]

	try:
		f = urllib2.urlopen(url+otherid)
	except URLError,e:
		print e.reason()
	value = f.read()
	p.feed(value)
	othersets = p.urls[:]

	#print mysets
	#print othersets
	
	c = 0
	for i in othersets:
		#print i
		if i not in mysets:
			print problemurl+str(i)
			c = c + 1
	print 'All:',
	print c
	f.close()
	p.close()


version 0.1

直接打开第一个不同的链接

import urllib2
import sgmllib
import sys
import string
import webbrowser

class LinksParser(sgmllib.SGMLParser):
	urls = []

	def do_a(self, attrs):
		for name, value in attrs:
			if value.startswith('problem.php?problemid='):
				s = value[22:]
				t = string.atoi(s)
				self.urls.append(t)
				#print s,t
			'''
			if name == 'div' and value not in self.urls:
				if value.startswith('http'):
					self.urls.append(value)
					#print value
			else:
				continue
			return
			'''
		#return urls

if __name__ == "__main__":
	# str = ""
	# if str.strip() is '':
		# print "str is None"
	# else:
		# print "str is no None"

	myid = 'rgtjf'
	otherid =  '10122130152'
	url = 'http://acm.cs.ecnu.edu.cn/userinfo.php?username='
	problemurl = 'http://acm.cs.ecnu.edu.cn/problem.php?problemid='
	mysets = []
	othersets = []
	notsolved = []

	filename = 'file'

	'''
	myid = raw_input('myid')
	otherid = raw_input('otherid')
	'''

	#shuchu chong dingxiang
	temp0 = sys.stdout
	sys.stdout = open(filename,'w')
	temp1 = sys.stdout
	sys.stdout = temp0


	p =  LinksParser()
	f = urllib2.urlopen(url+myid)
	value = f.read()
	p.feed(value)
	mysets = p.urls[:]


	f = urllib2.urlopen(url+otherid)
	value = f.read()
	p.feed(value)
	othersets = p.urls[:]

	#print mysets
	#print othersets
	saveid = 0;
	c = 0
	for i in othersets:
		#print i
		if i not in mysets:
			if saveid == 0:
				saveid = i
			print problemurl+str(i)
			c = c + 1
	print 'All:',
	print c

	
	if saveid != 0:
		webbrowser.open_new_tab(problemurl+str(saveid))
	'''
	if saveid != 0:
		f = urllib2.urlopen(problemurl+str(saveid))
		value = f.read()
		print value
	'''
	f.close()
	p.close()




1:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1011

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const int maxn = 100005;
int visited[maxn];
int z,i,m,l;

int main(){
	int cas = 1;
	while(scanf("%d%d%d%d",&z,&i,&m,&l),z||i||m||l){
		memset(visited,0,sizeof(visited));
		int c = 1;
		visited[l] = c++;
		printf("Case %d: ",cas++);
		while(1){
			int nl = (z*l+i)%m;
			//printf("%d\n",nl);
			if(visited[nl]!=0){
				printf("%d\n",c-visited[nl]);
				break;
			}
			else {
				visited[nl] = c++;
			}
			l = nl;
		}
	}
}

2:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1014

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const int maxn = 10;
int x[maxn];
int y[maxn];
int a[maxn];
int b[maxn];
bool solve()
{
	bool visited[10];
	memset(visited,false,sizeof(visited));
	int top = 0;
	for(int i=0;i<6;i++){
		if(visited[i] == true)continue;
		for(int j=i+1;j<6;j++){
			if(visited[j] == true)continue;
			if(x[i] == x[j] && y[i] == y[j]){
				visited[j] = true;
				a[top] = x[i];
				b[top] = y[i];
				top ++;
				break;
			}
		}
	}
	if(top != 3)return false;
	int ta,tb;
	if(a[0] == a[1]){
		if(b[0] == a[2]){
			if(b[1] == b[2])return true;
		}
		else if(b[0] == b[2]){
			if(b[1] == a[2])return true;
		}
	}
	else if(a[0] == b[1]){
		if(b[0] == a[2]){
			if(a[1] == b[2])return true;
		}
		else if(b[0] == b[2]){
			if(a[1] == a[2])return true;
		}
	}


	if(a[0] == a[2]){
		if(b[0] == a[1]){
			if(b[1] == b[2])return true;
		}
		else if(b[0] == b[1]){
			if(b[2] == a[1])return true;
		}
	}
	else if(a[0] == b[2]){
		if(b[0] == a[1]){
			if(a[2] == b[1])return true;
		}
		else if(b[0] == b[1]){
			if(a[1] == a[2])return true;
		}
	}
	return false;
}
int main()
{
	int t1,t2;
	while(scanf("%d%d",&t1,&t2)!=-1){
		if(t1 < t2){
			x[0] = t1;
			y[0] = t2;
		}
		else{
			x[0] = t2;
			y[0] = t1;
		}
		for(int i=1;i<6;i++){
			scanf("%d%d",&t1,&t2);
			if(t1 < t2){
				x[i] = t1;
				y[i] = t2;
			}
			else{
				x[i] = t2;
				y[i] = t1;
			}
		}
		if(solve())printf("POSSIBLE\n");
		else printf("IMPOSSIBLE\n");
	}
	return 0;
}


3.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1018

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const int maxn = 257;
int dp[maxn];
int main(){
	int T;
	scanf("%d",&T);
	while(T --){
		string str;
		cin >> str;
		int m;
		cin >> m;
		vector<int> v[maxn];
		memset(dp,0x3f,sizeof(dp));
		while(m --){
			string patten;
			cin >> patten;
			size_t found = str.find(patten);
			int length = patten.length();
			while(found!= string::npos){
    			//cout << found << '\n';
    			v[found+length-1].push_back(found-1);
    			//printf("%d -> %d\n",found+length-1,found);
    			found = str.find(patten,found+1);
    			//int f = (int)found;	
			}

		}
		int length = str.length();
		for(int i=0;i<length;i++){
			for(int j=0;j<v[i].size();j++){
				int t = v[i][j];
				//printf("%d -> %d\n",i,t);
				if(t == -1){
					dp[i] = 1;
					continue;
				}
				if(dp[t] + 1 < dp[i]){
					dp[i] = dp[t] + 1;
				}
			}
			//printf("%d %d\n",i, dp[i]);
		}
		printf("%d\n",dp[length-1]);
	}
}

4:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1034

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const int maxn = 1000005;
const double eps = 1e-8;
double f[maxn];
int d[maxn];
void init(){
	f[1] = 0;
	for(int i=1;i<maxn;i++){
		f[i] = f[i-1] + log2(1.0*i);
		d[i] = ceil(f[i]+eps);
		//printf("%.2lf\n",f[i]);
	}
}

int bs(double x){
	int l = 1;
	int r = maxn;
	int ans;
	while(l <= r){
		int m = (l+r) >> 1;
		if(f[m] < x){
			ans = m;
			l = m + 1;
		}
		else{
			r = m - 1;
		}
	}
	return ans;
}

int pow2(int n){
	int ans = 1;
	for(int i=0;i<n;i++){
		ans *= 2;
	}
	return ans;
}


int main(){
	//int t = ceil(10+eps);
	//printf("%d\n",t);
	init();
	int n;
	while(cin >> n, n){
		n = (n - 1960) / 10;
		n = pow2(n+2);
		cout << bs(1.0*n) << endl;
	}
	return 0;
}



5:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1038

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const double eps = 1e-8;

const int maxn = 105;

int main(){
	int n,m;
	while(scanf("%d\n",&n),n){
		scanf("%d",&m);
		set<int> s;
		for(int i=0;i<n;i++){
			int t;
			scanf("%d",&t);
			s.insert(t);
		}
		bool ans = true;
		while(m --){
			int c;
			int r;
			scanf("%d%d",&c,&r);
			int cr = 0;
			for(int i=0;i<c;i++){
				int t;
				scanf("%d",&t);
				if(s.find(t) != s.end()){
					cr ++;
				}
			}
			if(cr >= r);
			else ans = false;
		}
		if(ans)printf("yes\n");
		else printf("no\n");
	}
	return 0;
}


 


6:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1035

#include <cstdio>
#include <deque>
#include <queue>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
 
using namespace std;
typedef long long LL;
const double eps = 1e-8;

const int maxn = 1005;
bool v[maxn];
bool visited[maxn];
int n,z,m;
bool solve(int k){
	memset(visited,false,sizeof(visited));
	int t = 0;
	//visited[t] = true;
	while(1){
		if(t == z)return true;
		if(visited[t] == true)return false;
		if(v[t] == true)return false;
		visited[t] = true;
		t = (t+k)%n;
	}	
}
int main(){

	while(scanf("%d%d%d",&n,&z,&m)!=-1){
		memset(v,false,sizeof(v));
		z --;
		while(m --){
			int t;
			scanf("%d",&t);
			t --;
			v[t] = true;
		}
		int k=1;
		while(1){
			if(solve(k)){
				printf("%d\n",k);
				break;
			}
			k++;
		}
	}
	return 0;
}





7.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1044

#include <cstdio>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

typedef long long LL;
const int maxn = 100005;
struct node{
	int v;
	int next;
	int val;
}e[maxn*100];
int head[maxn];
int tot;
bool vis[maxn];
int d[maxn];
void init(){
	tot = 0;
	memset(head,-1,sizeof(head));
}
void add(int u,int v,int val){
	e[tot].v = v;
	e[tot].val = val;
	e[tot].next = head[u];
	head[u] = tot ++;
}
void spfa(int st){
	memset(vis,false,sizeof(vis));
	memset(d,0x3f,sizeof(d));
	queue<int> q;
	q.push(st);
	vis[st] = true;
	d[st] = 0;
	while(!q.empty()){
		int u = q.front();
		q.pop();
		vis[u] = false;
		for(int i=head[u];i!=-1;i=e[i].next){
			int v = e[i].v;
			if(d[u]+e[i].val < d[v]){
				d[v] = d[u] + e[i].val;
				if(!vis[v]){
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
}

int main(){
	int n;
	while(~scanf("%d",&n)){
		init();
		for(int i=0;i<n;i++){
			int s,e,l,c;
			l = 1;
			scanf("%d%d",&s,&c);
			while(c --){
				scanf("%d",&e);
				add(s,e,l);
				add(e,s,l);
			}
		}
		int st,ed;
		scanf("%d%d",&st,&ed);
		spfa(st);
		printf("%d %d %d\n",st,ed,d[ed]-1);
	}
	return 0;
}
EOJ 内存还能开这么大啊,不容易!




8:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1047

#include <cstdio>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

typedef long long LL;



/* **************************************************************************
//二分图匹配(匈牙利算法的DFS实现)
//初始化:g[][]两边顶点的划分情况
//建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配
//g没有边相连则初始化为0
//uN是匹配左边的顶点数,vN是匹配右边的顶点数
//调用:res=hungary();输出最大匹配数
//优点:适用于稠密图,DFS找增广路,实现简洁易于理解
//时间复杂度:O(VE)
//***************************************************************************/


//顶点编号从0开始的

const int MAXN=510;
int uN,vN;//u,v数目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//从左边开始找增广路径
{

    int v;
    for(v=0;v<vN;v++)//这个顶点编号从0开始,若要从1开始需要修改
	if(g[u][v]&&!used[v])
	{
	    used[v]=true;
	    if(linker[v]==-1 || dfs(linker[v]))
	    {//找增广路,反向
	        linker[v]=u;
	        return true;
	    }
	}
    return false;//这个不要忘了,经常忘记这句
}

int hungary()
{
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}

struct node{
	int h;
	char sex[4];
	char style[105];
	char sport[105];
}e[MAXN];

int main()
{
	int T;
	scanf("%d",&T);
	while(T --)
	{
		int n;
		scanf("%d",&n);
		uN = vN = n;
		for(int i=0;i<n;i++)
		{
			cin >> e[i].h >> e[i].sex >> e[i].style >> e[i].sport;
		}
		memset(g,0,sizeof(g));
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				if(i == j)continue;
				if(abs(e[i].h-e[j].h)>40          \
					|| strcmp(e[i].sex,e[j].sex) == 0 \
					|| strcmp(e[i].style,e[j].style) != 0  \
					|| strcmp(e[i].sport,e[j].sport) == 0)
				{
					;

				}
				else
				{
					g[i][j] = 1;
					g[j][i] = 1;
				}				
  			}
		}
		int ans = hungary();
		printf("%d\n",uN-ans/2);
	}
	return 0;
}

果然要建双向边啊,单向边果然有问题!



9:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1050

#include<stdio.h>
#define L 110
#define LL 10
int N,n,i,j,k,a[L][L],b[L][L],c[L][L];
int a11[LL][L][L],a12[LL][L][L],a21[LL][L][L],a22[LL][L][L],b11[LL][L][L],b12[LL][L][L],b21[LL][L][L],b22[LL][L][L],c11[LL][L][L],c12[LL][L][L],c21[LL][L][L],c22[LL][L][L];
int d1[LL][L][L],d2[LL][L][L],d3[LL][L][L],d4[LL][L][L],d5[LL][L][L],d6[LL][L][L],d7[LL][L][L],t1[LL][L][L],t2[LL][L][L];
void fen(int a[L][L],int a11[L][L],int a12[L][L],int a21[L][L],int a22[L][L],int n) 
{
    for(i=1;i<=n;++i) 
	{
		for(j=1;j<=n;++j) 
		{
			a11[i][j]=a[i][j];
            a12[i][j]=a[i][j+n];
            a21[i][j]=a[i+n][j];
            a22[i][j]=a[i+n][j+n];
        }
    }
}
 
void he(int a[L][L],int a11[L][L],int a12[L][L],int a21[L][L],int a22[L][L],int n) 
{
    int i,j;
    for (i=1;i<=n;++i) 
	{
        for (j=1;j<=n;++j) 
		{
			a[i][j]=a11[i][j];
            a[i][j+n]=a12[i][j];
            a[i+n][j]=a21[i][j];
            a[i+n][j+n]=a22[i][j];
        }
    }
}
 
void add(int c[L][L],int a[L][L],int b[L][L],int n) 
{
    int i, j;
    for (i=1;i<=n;++i) 
        for (j=1;j<=n;++j) 
			c[i][j]=a[i][j]+b[i][j];
}
 
void sub(int c[L][L],int a[L][L],int b[L][L],int n) 
{
    int i,j;
    for(i=1;i<=n;++i) 
        for (j=1;j<=n;++j)
			c[i][j]=a[i][j]-b[i][j];
}
void mul(int c[L][L],int a[L][L],int b[L][L],int n, int dep) 
{       
    if (n==1) 
    {
        c[1][1]=a[1][1]*b[1][1];
    
    }
    else if(n>1&&n<=100)
    {
		fen(a,a11[dep],a12[dep],a21[dep],a22[dep],n/2);
		fen(b,b11[dep],b12[dep],b21[dep],b22[dep],n/2);
		
		sub(t1[dep],b12[dep],b22[dep],n/2);
		mul(d1[dep],a11[dep],t1[dep],n/2,dep+1);
		
		add(t2[dep],a11[dep],a12[dep],n/2);
		mul(d2[dep],t2[dep],b22[dep],n/2,dep+1);
 
		add(t1[dep],a21[dep],a22[dep],n/2);
		mul(d3[dep],t1[dep],b11[dep],n/2,dep+1);
 
		sub(t2[dep],b21[dep],b11[dep],n/2);
		mul(d4[dep],a22[dep],t2[dep],n/2,dep+1);
			
		add(t1[dep],a11[dep],a22[dep],n/2);
		add(t2[dep],b11[dep],b22[dep],n/2);
		mul(d5[dep],t1[dep],t2[dep],n/2,dep+1);			
 
		sub(t1[dep],a12[dep],a22[dep],n/2);
		add(t2[dep],b21[dep],b22[dep],n/2);
		mul(d6[dep],t1[dep],t2[dep],n/2,dep+1);
			
		sub(t1[dep],a11[dep],a21[dep],n/2);
		add(t2[dep],b11[dep],b12[dep],n/2);
		mul(d7[dep],t1[dep],t2[dep],n/2,dep+1);
 
	    add(t1[dep],d4[dep],d5[dep],n/2);
		sub(t2[dep],d2[dep],d6[dep],n/2);
 
		sub(c11[dep],t1[dep],t2[dep],n/2);
 
		add(c12[dep],d1[dep],d2[dep],n/2);
 
		add(c21[dep],d3[dep],d4[dep],n/2);
 
		add(t1[dep],d1[dep],d5[dep],n/2);
		add(t2[dep],d3[dep],d7[dep],n/2);
		sub(c22[dep],t1[dep],t2[dep],n/2);
		
		he(c,c11[dep],c12[dep],c21[dep],c22[dep],n/2);
	}
}
 
void main() 
{
        scanf("%d",&N);
        for(k=1;k<=N;k++) 
        {                
            scanf("%d",&n);
            for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
					scanf("%d",&a[i][j]);
			for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
					scanf("%d",&b[i][j]);
            mul(c,a,b,n,1);
            for(i=1;i<=n;i++)
			{
				for(j=1;j<n;j++)
					printf("%d ",c[i][j]);
				printf("%d",c[i][j]);
				printf("\n");
			}
        }
        
}

 


分治算法,具体见:http://www.cnblogs.com/rollenholt/archive/2011/08/27/2155854.html



10.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1062

//============================================================================
// Name        : 基本函数模板.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;

const double eps = 1e-8;
const double PI = acos(-1.0);
int sgn(double x)
{
	if(fabs(x) < eps)return 0;
	if(x < 0)return -1;
	else return 1;
}
struct Point
{
	double x,y;
	Point(){}
	Point(double _x,double _y)
	{
		x = _x;y = _y;
	}
	Point operator -(const Point &b)const
	{
		return Point(x - b.x,y - b.y);
	}
	//叉积
	double operator ^(const Point &b)const
	{
		return x*b.y - y*b.x;
	}
	//点积
	double operator *(const Point &b)const
	{
		return x*b.x + y*b.y;
	}
	//绕原点旋转角度B(弧度值),后x,y的变化
	void transXY(double B)
	{
		double tx = x,ty = y;
		x = tx*cos(B) - ty*sin(B);
		y = tx*sin(B) + ty*cos(B);
	}
};
struct Line
{
	Point s,e;
	Line(){}
	Line(Point _s,Point _e)
	{
		s = _s;e = _e;
	}
	//两直线相交求交点
	//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
	//只有第一个值为2时,交点才有意义
    pair<int,Point> operator &(const Line &b)const
    {
        Point res = s;
        if(sgn((s-e)^(b.s-b.e)) == 0)
        {
            if(sgn((s-b.e)^(b.s-b.e)) == 0)
                return make_pair(0,res);//重合
            else return make_pair(1,res);//平行
        }
        double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        res.x += (e.x-s.x)*t;
        res.y += (e.y-s.y)*t;
        return make_pair(2,res);
    }
};
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
//*判断线段相交
bool inter(Line l1,Line l2)
{
	return
    max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
//判断直线和线段相交
bool Seg_inter_line(Line l1,Line l2) //判断直线l1和线段l2是否相交
{
	return sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0;
}
//点到直线距离
//返回为result,是点到直线最近的点
Point PointToLine(Point P,Line L)
{
    Point result;
    double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
    result.x = L.s.x + (L.e.x-L.s.x)*t;
    result.y = L.s.y + (L.e.y-L.s.y)*t;
    return result;
}
//点到线段的距离
//返回点到线段最近的点
Point NearestPointToLineSeg(Point P,Line L)
{
    Point result;
    double t = ((P-L.s)*(L.e-L.s))/((L.e-L.s)*(L.e-L.s));
    if(t >= 0 && t <= 1)
    {
        result.x = L.s.x + (L.e.x - L.s.x)*t;
        result.y = L.s.y + (L.e.y - L.s.y)*t;
    }
    else
    {
        if(dist(P,L.s) < dist(P,L.e))
            result = L.s;
        else result = L.e;
    }
    return result;
}
//计算多边形面积
//点的编号从0~n-1
double CalcArea(Point p[],int n)
{
    double res = 0;
    for(int i = 0;i < n;i++)
        res += (p[i]^p[(i+1)%n])/2;
    return fabs(res);
}
//*判断点在线段上
bool OnSeg(Point P,Line L)
{
    return
    sgn((L.s-P)^(L.e-P)) == 0 &&
    sgn((P.x - L.s.x) * (P.x - L.e.x)) <= 0 &&
    sgn((P.y - L.s.y) * (P.y - L.e.y)) <= 0;
}
//*判断点在凸多边形内
//点形成一个凸包,而且按逆时针排序(如果是顺时针把里面的<0改为>0)
//点的编号:0~n-1
//返回值:
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inConvexPoly(Point a,Point p[],int n)
{
	for(int i = 0;i < n;i++)
	{
		if(sgn((p[i]-a)^(p[(i+1)%n]-a)) < 0)return -1;
		else if(OnSeg(a,Line(p[i],p[(i+1)%n])))return 0;
	}
	return 1;
}
//*判断点在任意多边形内
//射线法,poly[]的顶点数要大于等于3,点的编号0~n-1
//返回值
//-1:点在凸多边形外
//0:点在凸多边形边界上
//1:点在凸多边形内
int inPoly(Point p,Point poly[],int n)
{
	int cnt;
	Line ray,side;
	cnt = 0;
	ray.s = p;
	ray.e.y = p.y;
	ray.e.x = -100000000000.0;//-INF,注意取值防止越界

	for(int i = 0;i < n;i++)
	{
		side.s = poly[i];
		side.e = poly[(i+1)%n];

		if(OnSeg(p,side))return 0;

		//如果平行轴则不考虑
		if(sgn(side.s.y - side.e.y) == 0)
			continue;

		if(OnSeg(side.s,ray))
		{
			if(sgn(side.s.y - side.e.y) > 0)cnt++;
		}
		else if(OnSeg(side.e,ray))
		{
			if(sgn(side.e.y - side.s.y) > 0)cnt++;
		}
		else if(inter(ray,side))
			cnt++;
	}
	if(cnt % 2 == 1)return 1;
	else return -1;
}
//判断凸多边形
//允许共线边
//点可以是顺时针给出也可以是逆时针给出
//点的编号0~n-1
bool isconvex(Point poly[],int n)
{
	bool s[3];
	memset(s,false,sizeof(s));
	for(int i = 0;i < n;i++)
	{
		s[sgn( (poly[(i+1)%n]-poly[i])^(poly[(i+2)%n]-poly[i]) )+1] = true;
		if(s[0] && s[2])return false;
	}
	return true;
}


int main()
{
	Line l1,l2;
	while(cin >> l1.s.x >> l1.s.y \
		      >> l1.e.x >> l1.e.y \
		      >> l2.s.x >> l2.s.y \
		      >> l2.e.x >> l2.e.y){
		if(inter(l1,l2))printf("Yes\n");
		else printf("No\n");
	}
	return 0;
}

模板,不解释!


11.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1051

    #include<iostream>
    #include<vector>
    using namespace std;
    typedef long long LL;
    inline LL min(LL a, LL b){
            return a > b ? b : a;
    }

    int main(){
            LL t, i, j, r, a[101][101], b[101];
            LL T;
            cin >> T; 
            while(T --){
                    cin >> t;
                    for(i = 0; i < t; ++i){
                            cin >> b[i] >> b[i + 1];
                    }
                    for(i = 0; i <= 100; ++i){
                            for(j = 0; j <= 100; ++j){
                                    a[i][j] = 0;    //全部归零
                            }
                    }
                    for(r = 2; r <= t; ++r){        //求相邻r个矩阵的最优值
                            for(i = 1; i + r - 1 <= t; ++i){        //从第i个矩阵开始的r个矩阵
                                    LL num = 0xffffffffffffff;     //定义一个较大的值
                                    for(j = i; j < i + r - 1; ++j){ //从第i个矩阵开始的r个矩阵拆分为i到j和j+1到i+r-1两个部分
                                            num = min(num, a[i][j] + a[j + 1][i + r - 1] + b[i - 1] * b[j] * b[i + r - 1]);
                                    }
                                    a[i][i + r - 1] = num;
                                    //cout << i << ":" << i + r - 1 << " " << a[i][i + r - 1] << " ";
                            }
                            //cout <<  endl;
                    }
                    cout << a[1][t] << endl;
            }
            return 0;
    }

动态规划:

具体见:http://www.clanfei.com/2012/05/1159.html

最后的求最优解的方法值得看看!



12.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1053

#include <stdio.h>
#include <iostream>
using namespace std;

int count,n,x;
int s[105];
void search(int i,int sum)
{
    if(i>=n)
    {
        if(sum==x)
        {
            count++;
        }
    }
    else
    {
        if(sum+s[i]<=x)
        {      
            search(i+1,sum+s[i]);
        }
        search(i+1,sum);
    }
}

int main()
{
    int T;
    cin >> T;
    while(T --)
    {
        cin >> n >> x;
        for(int i=0;i<n;i++)
        {
            cin >> s[i];
        }
        count = 0;
        search(0,0);
        cout << count << endl;
    }
}

NP,T^T,没看懂


13.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1054

#include<stdio.h>
int i,j,C,n,tw,tc,bestw;
int w[52][3],c[52][3];
void backtrack(int i)
{
	int j;
	if(i>=n)
	{
		if(tw <bestw||bestw==-1)
		{
			bestw=tw; 
		}
		return;
	}
	for(j=0;j<3;j++)
	{
		if(tc+c[i][j]<C)
		{
			tc+=c[i][j];
			tw+=w[i][j];
			backtrack(i+1);
			tc-=c[i][j];
			tw-=w[i][j];
		}
	}
}
 
int main()
{
	while(scanf("%d%d",&n,&C)!=EOF)
	{
		tw=tc=0;
		bestw=-1;
		for(i=0;i<n;i++)
			for(j=0;j<3;j++)
			scanf("%d%d",&w[i][j],&c[i][j]);
		backtrack(0);
		printf("%d\n",bestw);
	}
	
}

也就EOJ能这么做了



14.http://acm.cs.ecnu.edu.cn/problem.php?problemid=1111

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#define MAX_ARRAY_SIZE 105
int data[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];//存放最初的数据
int cost[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];//存放各个子问题的最优解
int mark[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE];//存放输出最优解方案标志
int main(int argc,char *argv[])
{   int T;
    scanf("%d",&T);
    while(T --){
        //初始化data
        int n;
        scanf("%d",&n);
        int i,j;
        for(i=0;i<n;i++){
            for(j=0;j<=i;j++){
                scanf("%d",&data[i][j]);
            }
        }
        //dp初始化
        for(i=0;i<n;i++){
            cost[n-1][i]=data[n-1][i];
        }
        //dp运行
        for(i=n-2;i>=0;i--){
            for(j=0;j<=i;j++){
                if(cost[i+1][j]<cost[i+1][j+1]){
                    cost[i][j]=data[i][j]+cost[i+1][j];
                    mark[i][j]=0;
                }else{
                    cost[i][j]=data[i][j]+cost[i+1][j+1];
                    mark[i][j]=1;
                }
            }
        }
        printf("%d\n",cost[0][0]);
    }
    return 0;
}

这题一点都不想写,直接copy的人家的代码


15:http://acm.cs.ecnu.edu.cn/problem.php?problemid=1114

#include <cstdio>
#include <deque>
#include <set>
#include <string>
#include <map>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
 
typedef long long LL;
const int maxn = 40;
bool isprime[maxn];
bool visited[22];
void init(){
    memset(isprime,true,sizeof(isprime));
    isprime[0] = false;
    isprime[1] = false;
    for(int i=2;i<maxn;i++){
        if(isprime[i]){
            for(int j=2;j<=maxn/i;j++){
                isprime[i*j] = false;
            }
        }
    }
}

int n;
int a[22];
void dfs(int i){
    if(i > n){
        if(isprime[a[i-1]+a[1]] == true){
            for(int j=1;j<=n;j++){
                if(j != 1)printf(" ");
                printf("%d",a[j]);
            }
            printf("\n");
            return ;
        }
    }
    for(int j=1;j<=n;j++){
        if(visited[j] == true)continue;
        if(i!=1 && isprime[a[i-1]+j] == false)continue;
        a[i] = j;
        visited[j] = true;
        dfs(i+1);
        visited[j] = false;
    }
}

int main(){
    init();
    while(cin >> n){
        memset(visited,false,sizeof(visited));
        a[1] = 1;
        visited[1] = true;
        dfs(2);
    }
    return 0;
}

贴了那么多代码,还是自己写的看的清楚啊,高端霸气上档次!


7-28 :15:30

先不刷水题了,没意思,还是看看怎么搞点没接触过的。

先从高斯消元开始吧


16:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值