紫书第五章

UVA 10474

lower_bound 应用

#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int qq = 10005;
int num[qq];
int main(){
	int n,m;
	int k = 1;
	while(scanf("%d%d", &n, &m)!=EOF){
		if(n==0&&m==0)	break;
		for(int i=0; i<n; ++i)
			scanf("%d", &num[i]);
		printf("CASE# %d:\n", k++);
		sort(num, num+n);
		for(int i=0; i<m; ++i){
			int x;scanf("%d", &x);
			int id = lower_bound(num, num+n, x)-num;
			//printf("%d %d\n", x, num[id].x);
			if(num[id]!=x)	printf("%d not found\n", x);
			else	printf("%d found at %d\n", x, id+1);
		}
	}
	return 0;
}

UVA 101

vector 应用

#include <string>
#include <vector>
#include <iostream>
using namespace std;
const int maxn = 30;
vector <int> pile[maxn];
int n;

void find_block(int a, int& p, int& h){
	for(p = 0; p < n; ++p){
		for(h = 0; h < pile[p].size(); ++h){
			if(pile[p][h] == a)	
				return;
		}
	}
}
void clear_above(int p, int h){
	for(int i = h+1; i<pile[p].size(); ++i){
		int b = pile[p][i];
		pile[b].push_back(b);
	}
	pile[p].resize(h + 1);
}
void pile_onto(int pa, int ha, int pb){
	for(int i=ha; i<pile[pa].size(); ++i)
		pile[pb].push_back(pile[pa][i]);
	pile[pa].resize(ha);
}

int main(){
	cin >> n;
	for(int i=0; i<n; ++i)
		pile[i].push_back(i);
	string x, y;
	int a, b;
	while(cin >> x){
		if(x=="quit")	break;
		cin >> a >> y >> b;
		int pa, pb, ha, hb;
		find_block(a, pa, ha);
		find_block(b, pb, hb);
		if(pa == pb)	continue;
		if(x == "move")	clear_above(pa, ha);
		if(y == "onto")	clear_above(pb, hb);
		pile_onto(pa, ha, pb);
	}
	for(int i = 0; i < n; ++i){
		cout << i << ":";
		for(int j = 0; j < pile[i].size(); ++j)
			cout << " " << pile[i][j];
		cout << endl;
	}
	return 0;
}

UVA 10815

set应用

#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<set>
#include<string>
#include<iostream>
#include<ctype.h>
using namespace std;
set<string>s;
set<string>::iterator it;
int main(){
	string str;
	while(getline(cin, str)){
		for(int i=0; i<str.size(); ++i){
			if(isalpha(str[i])){
				string x;
				int c = i;
				while(isalpha(str[c])&&c<str.size()){
					if(isupper(str[c]))	x+=(str[c]+'a'-'A');
					else	x+=str[c];
					c++;
				}
				if(s.find(x)==s.end())	s.insert(x);
				i = c;
			}
		}
	}
	for(it=s.begin(); it!=s.end(); ++it)
			cout << (*it) << endl;
	return 0;
}

UVA 156

map 应用

#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
#include <ctype.h>
using namespace std;
map<string, string> a;
map<string, int> b;
int main(){
    string x;
    while(getline(cin, x)){
        if(x[0]=='#')   break;
        int len = x.size();
        for(int i=0; i<len; ++i)
            if(isalpha(x[i])) {
                string y;
                int c = i;
                while (isalpha(x[c])) y += x[c], c++;
                string z;
                for (int j = 0; j < y.size(); ++j)
                    if (isupper(y[j])) z += y[j] - 'A' + 'a';
                    else z += y[j];
                sort(z.begin(), z.end());
                if (a[z] == "") {
                    a[z] = y;
                } else {
                    b[z] = 1;
                }
                i = c;
            }
    }
    set<string>s;
    for(map<string, string>::iterator it=a.begin(); it!=a.end(); ++it){
        if(b[it->first]==0)     s.insert(it->second);
    }
    for(set<string>::iterator it=s.begin(); it!=s.end(); ++it){
        cout << (*it) << endl;
    }
    return 0;
}


UVA 12096

set_union   集合并集

set_intersection   集合交集

应用

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

typedef set<int> Set;
map<Set, int> IDcache;
vector<Set> Setcache;
int ID(Set x){
	if(IDcache.count(x))	return IDcache[x];
	Setcache.push_back(x);	//tian jia xin ji he
	return IDcache[x] = Setcache.size() - 1;
}
#define ALL(x)	x.begin(), x.end()
#define INS(x)	inserter(x, x.begin())
int main(){
	int t; cin >> t;
	while(t--){
		stack<int> s;
		int n;cin >> n;
		for(int i = 0; i < n; ++i){
			string  op;
			cin >> op;
			if(op[0] == 'P')	s.push(ID(Set()));
			else if(op[0] == 'D')	s.push(s.top());
			else{
				Set x1 = Setcache[s.top()]; s.pop();
				Set x2 = Setcache[s.top()]; s.pop();
				Set x;
				if(op[0] == 'U')	set_union (ALL(x1), ALL(x2), INS(x) );
				if(op[0] == 'I')	set_intersection (ALL(x1), ALL(x2), INS(x));
				if(op[0] == 'A')	{ x = x2; x.insert(ID(x1)); };
				s.push(ID(x));
			}
			cout << Setcache[s.top()].size() << endl;
		}
		cout << "***" << endl; 
	}
}


UVA 540

简单队列应用

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <map>
#include <string>
#include <queue>
#include <iostream>
using namespace std;

const int qq = 1e6+10;
map<int, int> ma;
queue<int>q[qq];
vector<int>v;
int c[qq];
int main(){
	 int cas=1;
	 int n;
	 while(scanf("%d", &n)!=EOF){
	 	if(n==0)	break;
	 	for(int i=1; i<=n; ++i){
	 		int m;
			 scanf("%d", &m);
	 		for(int j=0; j<m; ++j){
	 			int x;
	 			scanf("%d", &x);
	 			ma[x]=i;
	 		//	printf("%d\n", ma[x]);
	 		}
	 	}
	 	printf("Scenario #%d\n", cas++);
	 	char str[25];
	 	int l, r;
	 	l = r = 1;
	 	while(1){
	 		scanf("%s", str);
	 		if(str[0]=='S')	break;
	 		if(str[0]=='E'){
	 			int a;
				scanf("%d", &a);
	 			if(q[ma[a]].size()==0){
	 				q[ma[a]].push(a);
	 				c[r++] = ma[a];
	 			}else{
	 				q[ma[a]].push(a);
	 			}
	 			//printf("%d\n", q[ma[a]].size());
	 		}else{
	 		//	printf("%d\n", q[c[l]].size());
	 			printf("%d\n", q[c[l]].front());
	 			q[c[l]].pop();
	 			if(q[c[l]].size()==0)	l++;
	 		}
	 	}
	 	printf("\n");
	 	for(int i=0; i<qq; ++i)
	 		while(!q[i].empty())	q[i].pop();
	 }
	 return 0;
}


UVA 136

暴力过了

#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <ctype.h>
#include <map>
using namespace std;

#define ll long long
const int qq = 1e5+10;
vector<ll>v;
map<ll, int>Q;
ll fun(ll a, ll b){
	ll ans = 1;
	while(b){
		if(b&1)	ans = ans*a;
		a = a*a;
		b>>=1;
	}
	return ans;
}
int get(ll a){
	int k = 0;
	while(a){
		a/=10;
		k++;
	}
	return k;
}
int main(){
	for(ll i=0; i<31; ++i){
		ll a = fun(2,i);
		if(!Q[a])	Q[a] = 1, v.push_back(a);
		for(ll j=0; j<23; ++j){
			ll b = fun(3, j);
			if(!Q[b])	Q[b] = 1, v.push_back(b);
			if(get(a)+get(b)>18)	break;
			ll c = a*b;
			for(ll k=0; k<17; ++k){
				ll d = fun(5, k);
				if(!Q[d])	Q[d] = 1, v.push_back(d);
				if(get(c)+get(d)>18)	break;
				if(!Q[c*d])	Q[c*d] = 1, v.push_back(c*d);
			}
		}
	}
	sort(v.begin(), v.end());
	printf("The 1500'th ugly number is %lld.\n", v[1499]);
	return 0;
}

注意输出什么, 什么被替代 !!!!


UVA 400

细节处理

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

const int maxcol = 60;
const int MAXN = 100 + 5;
string filename[MAXN];
void print(const string& s, int len, char extra){
	cout << s ;
	for(int i = 0; i < len - s.length(); ++i)
		cout << extra;
}
int n;
int main(){
	while(cin >> n){
		int maxlen = 0;
		for(int i = 0; i < n; ++i){
			cin >> filename[i];
			maxlen = max(maxlen, (int)filename[i].length());
		}
		sort(filename, filename + n);
		int cols = (maxcol - maxlen)/(maxlen + 2) + 1;
		int row = (n + cols - 1)/cols;
		print("", 60, '-');
		cout << endl;
		for(int i = 0; i < row; ++i){
			for(int j = 0 ; j < cols ; ++j){
				int idx = j * row + i;
				if(idx < n)	print(filename[idx], j == cols-1? maxlen : maxlen+2, ' ');
			}
			cout << endl;
		}
	}
	return 0;
}


UVA 1592

这题真是好题

pair + map

还有暴力技巧

#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <utility>
#include <iostream>
using namespace std;

typedef pair<int , int> pill;
map<string, int> cache;
map<pill , int> goal;
int num[10005][12];
int n, m;
int diff;
void Init(string &x, int row){
	string y;
	x += ',';
	int len = x.size();
	int cols = 1;
	for(int i = 0; i < len; ++i){
		if(x[i] != ',')	y+=x[i];
		else{
			if(!cache.count(y))	cache[y] = diff++;
			num[row][cols++] = cache[y];
			y = "";
		}
	}
}
int main(){
	while(scanf("%d%d", &n, &m)!=EOF){
	    getchar();
        //cout << n << " " << m << endl;
		diff = 1;
		string x;
			for(int i = 1; i <= n ; ++i){
				getline(cin, x);
				//cout << x << endl;
				Init(x, i);
				x = "";
			}
		int flag = 0;
		for(int j,i = 1; i <= m; ++i){
			for(j = i+1; j <= m; ++j){
				for(int k = 1; k <= n; ++k){
					if(goal.count(make_pair(num[k][i], num[k][j]))){
						flag = 1;
						printf("NO\n");
						printf("%d %d\n", goal[make_pair(num[k][i], num[k][j])], k);
						printf("%d %d\n", i, j);
					}
                    if(flag)    break;
					goal[make_pair(num[k][i], num[k][j])] = k;
				}
				goal.clear();
				if(flag)	break;
			}
			if(flag)    break;
		}
		if(!flag)	cout << "YES" << endl;
		cache.clear();
	}
	return 0;
}

UVA 211

三维可见性 ....

貌似还有点不懂

#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <utility>
#include <sstream>
#include <set>
using namespace std;
const int qq = 100 + 5;
double x[qq*2];
int n;

struct Rectan{
	double x, y, w, d, h;
	int id;
	bool operator < (const Rectan &a)const{
		return x < a.x || (x == a.x && y < a.y);
	}
}b[qq];

bool cover(int k, double mx){
	if(b[k].x <= mx && b[k].x + b[k].w >= mx)	return true;
	return false;
}

bool visible(int k, double mx){
	if(!cover(k, mx))	return false;
	for(int i = 0; i < n; ++i)
		if(b[k].y > b[i].y && b[k].h <= b[i].h && cover(i, mx))	return false;
	return true;
}

int main(){
	int kase = 0;
	while(scanf("%d", &n) == 1 && n){
		for(int i = 0; i < n; ++i){
			scanf("%lf%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].w, &b[i].d, &b[i].h);
			x[i*2] = b[i].x, x[i*2 + 1] = b[i].x + b[i].w;
			b[i].id = i + 1;
		}
		sort(b, b + n);
		sort(x, x + 2*n);
		int m = unique(x, x + 2*n) - x;
		if(kase++) printf("\n");
		printf("For map #%d, the visible buildings are numbered as follows:\n%d" , kase, b[0].id);
		for(int i = 1; i < n; ++i){
			bool vis = false;
			for(int j = 0; j < m - 1; ++j)
				if(visible(i, (x[j] + x[j+1])/2)){ vis = true ; break ; }
			if(vis)	printf(" %d", b[i].id);
		}
		puts("");
	}
	return 0;
}


UVA 1593

细节处理

#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <utility>
#include <map>
#include <vector>
#include <ctype.h>
#include <iostream>
using namespace std;

const int qq = 1e5+10;
string s[1005 * 180];
int maxwid[1111]={0};
int lenrow[1111]={0};
int row;
int top;
void Init(string& x){
	int col = 0;
	x+=' ';
	int len = x.size();
	string y;
	for(int i = 0; i < len ; ++i){
		if(x[i]!=' '){
			int c = i;
			while(x[c]!=' ')	y+=x[c], c++;
			maxwid[col] = max(maxwid[col], c-i);
			col++;
			s[top++] = y;
			y = "";
			i = c;
		}
	}
	lenrow[row] = col;
	row++;
}
void print(string& x, int len, char extre){
	cout << x;
	for(int i = 0; i < len - (int)x.size(); ++i)
			cout << extre;
}
int main(){
    row = top = 0;
	string x;
	memset(maxwid, 0, sizeof(maxwid));
	memset(lenrow, 0, sizeof(lenrow));
	while(getline(cin, x)){
		Init(x);
	}
	int count = 0 ;
	for(int j, i = 0; i < row; ++i){
        for(j = 0; j < lenrow[i]; ++j){
			print(s[count], j!=lenrow[i]-1?maxwid[j]+1:(int)s[count].size(), ' ');
			count++;
		}
		puts("");
	}
	return 0;
}

UVA 1594



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值