第五章 C++与STL入门(例题篇:综合部分)--算法竞赛入门经典

第五章 C++与STL入门(例题篇:综合部分)

知识点一:大整数类 P124
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
//#include<cstdio>
using namespace std;
struct BigInteger {
	static const int BASE = 100000000;
	static const int WIDTH = 8;
	vector<int> s;
	BigInteger(long long num = 0) { *this = num; }//构造函数
	BigInteger operator=(long long num)//赋值运算符
	{
		s.clear();
		do
		{
			s.push_back(num%BASE);
			num /= BASE;
		} while (num > 0);
		return *this;
	}
	BigInteger operator=(const string &str)//赋值运算符
	{
		s.clear();
		int x, len = (str.length() - 1) / WIDTH + 1;
		for (int i = 0;i < len;i++)
		{
			int end = str.length() - i*WIDTH;
			int start = max(0, end - WIDTH);
			sscanf(str.substr(start, end - start).c_str(), "%d", &x);
			s.push_back(x);
		}
		return *this;
	}

	//加法
	BigInteger operator+(const BigInteger&b)const {
		BigInteger c;
		c.s.clear();
		for (int i = 0, g = 0;;i++)
		{
			if (g == 0 && i >= s.size() && i >= b.s.size())
				break;
			int x = g;
			if (i < s.size()) x += s[i];
			if (i < b.s.size())x += b.s[i];
			c.s.push_back(x%BASE);
			g = x / BASE;
		}
		return c;
	}

	//加法
	BigInteger operator+=(const BigInteger &b)
	{
		*this = *this + b;
		return *this;
	}

	bool operator <(const BigInteger& b)const
	{
		if (s.size() != b.s.size())
			return s.size() < b.s.size();
		for (int i = s.size() - 1;i >= 0;i--)
		{
			if (s[i] != b.s[i])
				return s[i] < b.s[i];
		}
		return false;
	}

	bool operator >(const BigInteger&b)const
	{
		return b < *this;
	}
	bool operator <=(const BigInteger&b)const
	{
		return !(b < *this);
	}
	bool operator >=(const BigInteger&b)const
	{
		return !(*this < b);
	}
	bool operator !=(const BigInteger&b)const
	{
		return b < *this || *this < b;
	}
	bool operator ==(const BigInteger&b)const
	{
		return !(b < *this) && !(*this < b);
	}
};

//“<<”运算符
ostream& operator<<(ostream &out, const BigInteger& x)
{
	out << x.s.back();
	for (int i = x.s.size() - 2;i >= 0;i--)
	{
		char buf[20];
		sprintf(buf, "%08d", x.s[i]);
		for (int j = 0;j < strlen(buf);j++)
		{
			out << buf[j];
		}
	}
	return out;
}

//“>>”运算符
istream& operator>>(istream &in, BigInteger& x)
{
	string s;
	if (!(in >> s))return in;
	x = s;
	return in;
}
int main()
{
	/*ios::sync_with_stdio(false);*/
	//freopen("input.txt", "r", stdin);
	BigInteger y;
	BigInteger x = y;
	BigInteger z = 123;

	BigInteger a, b;
	cin >> a >> b;
	cout << a + b << endl;
	cout << BigInteger::BASE << endl;
	return 0;
}
例题5-9:数据库(Database,UVa1592)

//输入案例
3 3
How to compete in ACM ICPC,Peter,peter@neerc.ifmo.ru
How to win ACM ICPC,Michael,michael@neerc.ifmo.ru
Notes from ACM ICPC champion,Michael,michael@neerc.ifmo.ru
2 3
1,Peter,peter@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru
//输出
NO
2 3
2 3
YES

#include<string>
#include<map>
#include<iostream>
#include<sstream>
using namespace std;
typedef pair<int, int>PII;//pair容器
const int maxr = 10000 + 5;
const int maxc = 10 + 5;
int m, n, db[maxr][maxc], cnt;
map<string, int> id;
int ID(const string&s)
{
	if (!id.count(s))//count
	{
		id[s] = ++cnt;
	}
	return id[s];
}
void find()
{
	for (int c1 = 0;c1 < m;c1++)
	{
		for (int c2 = c1 + 1;c2 < m;c2++)
		{
			map<PII, int>d;
			for (int i = 0;i < n;i++)
			{
				PII p = make_pair(db[i][c1], db[i][c2]);//make_pair
				if (d.count(p))
				{
					printf("NO\n");
					printf("%d %d\n", d[p] + 1, i + 1);
					printf("%d %d\n", c1 + 1, c2 + 1);
					return;
				}
				d[p] = i;
			}
		}
	}
	printf("YES\n");
}
int main()
{
	freopen("input.txt", "r", stdin);
	string s;
	while (getline(cin, s))//可以获取有空格的整行
	{
		stringstream ss(s);//stringstream
		if (!(ss >> n >> m))
			break;
		cnt = 0;
		id.clear();
		for (int i = 0;i < n;i++)
		{
			getline(cin, s);
			int lastpos = -1;
			for (int j = 0;j < m;j++)
			{
				int p = s.find(',', lastpos + 1);//find
				if (p == string::npos)//find没找到会返回string::npos
					p = s.length();
				db[i][j] = ID(s.substr(lastpos + 1, p - lastpos - 1));//substr
				lastpos = p;
			}
		}
		find();
	}
	return 0;
}
例题5-10:PGA巡回赛的奖金(PGA Tour Prize Money,UVa207)
#include<string>
#include<cassert>
#include<algorithm>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)//宏定义一个for循环
#define gets(s) fgets(s,40,stdin)//宏定义一个gets(s);
const int maxn = 144;
const int n_cut = 70;
struct Player
{
	char name[25];
	int amateur;//是否是业余选手
	int sc[4];
	int sc36, sc72;
	int dq;
	int rnds;//轮数
}player[maxn];

int n;
double purse;//总奖金
double p[n_cut];//1-70名可以拿的奖金比例

bool cmp1(const Player&p1, const Player&p2)
{
	if (p1.sc36 < 0 && p2.sc36)
		return false;//equal
	if (p1.sc36 < 0)
		return false;//p2 smaller
	if (p2.sc36 < 0)
		return true;//p1 smaller
	return p1.sc36 < p2.sc36;
}

bool cmp2(const Player&p1, const Player&p2)
{
	if (p1.dq&&p2.dq)
	{
		if (p1.rnds != p2.rnds)//安轮数排序
			return p2.rnds < p1.rnds;
		if (p1.sc72 != p2.sc72)//如果轮数相同安总得分排序
			return p1.sc72 < p2.sc72;
		return strcmp(p1.name, p2.name) < 0;//总得分也相同则安名字排序
	}
	if (p1.dq)
		return false;
	if (p2.dq)
		return true;
	if (p1.sc72 != p2.sc72)//安总得分排序
		return p1.sc72 < p2.sc72;
	return strcmp(p1.name, p2.name) < 0;//总得分相同则安名字排序
}

void print_result()
{
	printf("Player Name          Place     RD1  RD2");
	printf("  RD3  RD4  TOTAL     Money Won\n");
	printf("---------------------------------------");
	printf("--------------------------------\n");

	int i = 0, pos = 0;
	while (i < n)
	{
		if (player[i].dq)
		{
			printf("%s           ", player[i].name);
			REP(j, player[i].rnds) printf("%-5d", player[i].sc[j]);//位宽为5左对齐
			REP(j, 4 - player[i].rnds) printf("     ");
			printf("DQ\n");
			i++;
			continue;
		}
		int j = i;
		int m = 0;//number of tied players
		bool have_money = false;
		double tot = 0.0;//total pooled money
		while (j < n&&player[i].sc72 == player[j].sc72)
		{
			if (!player[j].amateur)
			{
				m++;
				if (pos < n_cut)
				{
					have_money = true;
					tot += p[pos++];
				}
			}
			j++;
		}

		//print player [i,j) together because they have the same rank
		int rank = i + 1;//rank of all these m players
		double amount = purse*tot / m;
		while (i < j)
		{
			printf("%s ", player[i].name);
			char t[5];
			sprintf(t, "%d%c", rank, m>1 && have_money&&!player[i].amateur ? 'T' : ' ');
			printf("%-10s", t);
			REP(e, 4) printf("%-5d", player[i].sc[e]);
			if (!player[i].amateur&&have_money)
			{
				printf("%-10d", player[i].sc72);
				printf("$%-9.2lf\n", amount / 100.0);//位宽为9保留2位小数左对齐
			}
			else
			{
				printf("%d\n", player[i].sc72);
			}
			i++;
		}
	}

}
int main()
{
	/*ios::sync_with_stdio(false);*/
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	int T;
	char s[40];
	gets(s);
	sscanf(s, "%d", &T);
	while (T--)
	{
		gets(s);//empty line

				//prize
		gets(s);
		sscanf(s, "%lf", &purse);

		REP(i, n_cut)
		{
			gets(s);
			sscanf(s, "%lf", &p[i]);
		}

		//players
		gets(s);
		sscanf(s, "%d", &n);
		assert(n <= 144);
		REP(k, n)
		{
			gets(s);

			strncpy(player[k].name, s, 20);//strncpy复制字符串
			player[k].name[20] = 0;
			player[k].amateur = 0;
			if (strchr(player[k].name, '*'))
			{
				player[k].amateur = 1;
			}

			//scores
			player[k].sc36 = player[k].sc72 = player[k].dq = 0;
			memset(player[k].sc, -1, sizeof(player[k].sc));
			REP(i, 4)
			{
				char t[5];
				REP(j, 3)
				{
					t[j] = s[20 + i * 3 + j];
				}
				t[3] = '\0';
				if (!sscanf(t, "%d", &player[k].sc[i]))
				{
					//DQ!
					player[k].rnds = i;
					player[k].dq = -1;
					if (i < 2)
						player[k].sc36 = -1;
					break;
				}
				else
				{
					player[k].sc72 += player[k].sc[i];
					if (i < 2)
					{
						player[k].sc36 += player[k].sc[i];
					}
				}
			}
		}

		//round 1
		sort(player, player + n, cmp1);
		assert(player[n_cut - 1].sc36 >= 0);//保证70位选手晋级
		for (int i = n_cut - 1;i < n;i++)
		{
			if (i == n - 1 || player[i].sc36 != player[i + 1].sc36)
			{
				n = i + 1;//找到所有第一轮晋级的选手
				break;
			}
		}

		//round 2
		sort(player, player + n, cmp2);

		print_result();
		if (T)
			printf("\n");

	}
	return 0;
}

输入样例:
2

1000000.00
10.0
6.4
4.4
2.4
1.8
1.7
1.6
1.5
1.49
1.48
1.47
1.46
1.45
1.44
1.43
1.42
1.41
1.4
1.39
1.38
1.37
1.36
1.35
1.34
1.33
1.32
1.31
1.3
1.29
1.28
1.27
1.26
1.25
1.24
1.23
1.22
1.21
1.2
1.19
1.18
1.17
1.16
1.15
1.14
1.13
1.12
1.11
1.1
1.09
1.08
1.07
1.06
1.05
1.04
1.03
1.02
1.01
0.99
0.98
0.97
0.96
0.95
0.94
0.93
0.92
0.91
0.20
0.12
0.07
0.01
140
abcWALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
abbWALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
aabWALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
aaaWALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE* 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ
SID SHANKER* 90 99 62 61
SID SHANKER* 90 99 62 62
JIMMY ABLE 69 73 80 DQ
WALLY WEDGE 70 70 70 71
WALLY WEDGE 70 70 70 70
SANDY LIE 80 DQ

320788.00
2.9421
2.9354
2.7666
2.7149
2.6625
2.5505
2.5259
2.4517
2.4406
2.4250
2.2774
2.1331
2.0760
2.0639
2.0233
2.0015
1.9395
1.9308
1.9120
1.9002
1.8913
1.8750
1.6926
1.6782
1.6140
1.6023
1.5874
1.5767
1.5504
1.5406
1.5167
1.4919
1.4724
1.4721
1.4696
1.4604
1.4540
1.4108
1.3978
1.3805
1.3709
1.3704
1.3611
1.3044
1.2493
1.0667
1.0565
1.0006
0.9985
0.9633
0.9535
0.9486
0.9364
0.8549
0.8376
0.7343
0.7219
0.6010
0.5943
0.5868
0.5787
0.4608
0.4251
0.3804
0.2750
0.2723
0.1436
0.1081
0.0245
0.0129
123
Aamicoi J* 70 60 65 65
Dgztqaa 64 76 71 63
Letjqpdle Na* 68 60 62 62
Ydypvmxhuk 68 78 72 80
Uvjvah 62 82 71 80
Ywhmgc 69 72 71 63
Mxwnei 80 82 83 73
Pwzvah 75 71 70 81
Xngexr* 61 64 66 63
Obiokvk 77 71 66 75
Omeqlfzksnar* 68 74 61 73
Twhpvg* 74 73 82 60
Ubxruwsprai 61 63 64 73
Htaapz B 69 67 76 79
Mwjqdj Zu 69 62 75 63
Soppvtiyfe Ra 62 76 63 62
Mfbrnkzvctvbv 66 61 74 76
Pgemjayfwqcat* 65 79 68 83
Ttncjtr 78 72 65 73
Jjfkbo* DQ
Enrxih* DQ
Nbtgxdt Vc T* 67 69 75 72
Xorquo Pzr Cym 71 72 77 64
Vthfiv 63 76 74 64
Etwifts Gb 72 DQ
Vzzscq 67 81 80 80
Eqtmyk 75 77 63 79
Znluiyfsu 74 64 70 78
Sdmzohttytn Ypft 62 73 71 64
Hpsdifr 80 77 67 70
Hhsxhn Cm 66 76 65 60
Zrphfoivxbvwrbtar 61 66 73 82
Grxekx 65 67 70 72
Qwspdjau Quzsp S * 83 82 64 80
Wbtpqenlggeiio* DQ
Zlifxcd* 77 63 79 68
Nviajs 77 69 69 77
Mxmsmm 61 79 77 67
Xofbxmlr 76 81 73 62
Urhcbmz Y 61 80 79 66
Aappzoi* 65 71 75 65
Dioytn Eev Jvt 83 71 65 83
Znfdcm Sd Hmcbwvb* 65 DQ
Dgacjp 80 72 77 80
Ssfvoyem Fhn Dqid 62 72 64 69
Ivqpdznvuj 75 68 81 80
Hlrqgjj Z Spg Fe DQ
Iayrojh 83 79 61 82
Ysdhztq 79 63 68 63
Xxvgxnv Aja Wwl 74 74 82 61
Hiddqm 69 62 72 DQ
Rogckdak Fiujf 69 69 68 78
Fnmbaqy V 61 61 73 74
Aedeto 60 82 73 65
Uacbpqj Kndf 62 70 80 77
Stbkjqscrx Mqt* 81 74 69 68
Geggin J* 67 63 79 74
Kfhfyl Lp 70 83 63 74
Nyimezmrt 80 72 81 61
Iudfspxzle 65 68 82 61
Ltovorifg* 67 74 79 60
Zdondv 70 82 75 76
Zraaaeh * 64 70 77 69
Wdause 67 61 61 74
Xffkalrla 66 76 68 65
Ggxbpv Lgh 60 80 70 82
Eicbnlon* 60 67 83 79
Wzxsfpditjz Ewfdu 63 64 70 80
Lemwwlnw 64 70 70 75
Zxoety Vrv Lll Gf DQ
Aatpdog 76 73 66 61
Uhapgfk* 72 73 74 67
Bmdypu* 62 69 80 76
Suboed 64 81 60 83
Ghcydsmbf* 83 67 75 72
Awmucqu 63 80 65 76
Mhfoogxhw Ijmuszf 72 76 73 62
Whwere E* DQ
Irqvxrwbpzvz 68 81 76 80
Vhnwal 73 77 68 80
Gxjxylr 83 73 83 63
Nrwcgl 82 72 71 76
Mctltuvu 67 68 73 80
Nmmyxdfaq Qj Mxml 68 71 DQ
Djaebvlxydpotc 78 69 62 69
Wkqubqt* 69 77 83 65
Twltmv T 64 79 65 83
Vlofya 76 77 77 79
Iljkgh Pt Rp Jes 83 66 75 83
Yseenu 78 71 72 80
Xldpki 64 83 79 75
Bezoawkku 75 81 82 63
Rugawc* 81 66 73 83
Cxsdrg 75 62 67 72
Bgkxin Btvb* 73 82 DQ
Sdnslp DQ
Kkovqp 81 76 78 82
Aeugqc 61 82 67 63
Nlcdaj 76 80 78 77
Izaabbwt* 71 76 73 72
Aeovvd Llg* 77 81 81 DQ
Krfahnbheb 77 67 60 80
Jbiwup* 68 DQ
Guznwmym S B 68 65 81 80
Xjjyyfo 66 80 62 71
Igzrtr* 68 DQ
Ucldakd Rxckea Pu 62 73 78 63
Mutzyvim Wnh 65 69 70 78
Gvbahl* 65 74 66 75
Vgpvjcu 73 81 60 79
Satuzn Cx Dvlb 62 67 68 60
Pttuviekf* 81 67 80 76
Bigfgm 65 76 72 81
Iefojinjj 72 78 80 DQ
Guydou* 77 74 82 71
Bkqlmzosccxw* 79 71 65 74
Evzevp 76 72 68 71
Hmhakop Rycx Gt 71 72 79 70
Mkbobau 72 65 68 76
Trgutpl 80 72 DQ
Wcqayce Lgaffu 60 76 65 70
Jwgkatbe 70 73 80 61
Vjnwygem DQ

例题5-11:邮件传输代理的交互(The Letter Carrier’s Rounds,UVa814) P130

输入案例:
MTA London 4 Fiona Paul Heather Nevil
MTA SanFrancisco 3 Mario Luigi Shariff
MTA Paris 3 Jacque Suzanne Maurice
MTA HongKong 3 Chen Jeng Hee
MTA MexicoCity 4 Conrado Estella Eva Raul
MTA Cairo 3 Hamdy Tarik Misa
*
Hamdy@Cairo Conrado@MexicoCity Shariff@SanFrancisco Lisa@MexicoCity
*
Congratulations on your efforts !!
–Hamdy
*
Fiona@London Chen@HongKong Natasha@Paris
*
Thanks for the report! --Fiona
*
*

#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
void parse_address(const string &s, string &user, string&mta)
{
	int k = s.find('@');
	user = s.substr(0, k);//substr用法一
	mta = s.substr(k + 1);//substr用法二
}
int main()
{
	/*ios::sync_with_stdio(false);*/
	freopen("input.txt", "r", stdin);
	int k;
	string s, t, user1, mta1, user2, mta2;
	set<string> addr;
	while (cin >> s&&s != "*")
	{
		cin >> s >> k;
		while (k--)
		{
			cin >> t;
			addr.insert(t + "@" + s);
		}
	}
	while (cin >> s&&s != "*")
	{
		parse_address(s, user1, mta1);

		vector<string> mta;//所有需要连接的mta,按照输入顺序
		map<string, vector<string>> dest;//每个MTA需要发送的用户
		set<string> vis;
		while (cin >> t&&t != "*")
		{
			parse_address(t, user2, mta2);//处理收件人地址
			if (vis.count(t))
				continue;//重复的收件人
			vis.insert(t);
			if (!dest.count(mta2))
			{
				mta.push_back(mta2);
				dest[mta2] = vector<string>();//初始化
			}
			dest[mta2].push_back(t);
		}
		getline(cin, t);//把“*”这一行的回车吃掉在调用getline思考下要吃掉一行吗
		//输入邮件正文
		string data;
		while (getline(cin, t) && t[0] != '*')
			data += "     " + t + "\n";
		for (int i = 0;i < mta.size();i++)
		{
			string mta2 = mta[i];
			vector<string> users = dest[mta2];
			cout << "Connection between " << mta1 << " and " << mta2 << endl;
			cout << "     HELO " << mta1 << endl;
			cout << "     250\n";
			cout << "     MAIL FROM:<" << s << ">\n";
			cout << "     250\n";
			bool ok = false;
			for (int i = 0;i < users.size();i++)
			{
				cout << "     RCPT TO:<" << users[i] << ">\n";
				if (addr.count(users[i]))
				{
					ok = true;
					cout << "     250\n";
				}
				else
				{
					cout << "     550\n";
				}
			}
			if (ok)
			{
				cout << "     DATA\n";
				cout << "     354\n";
				cout << data;
				cout << "     .\n";
				cout << "     250\n";
			}
			cout << "     QUIT\n";
			cout << "     221\n";
		}
	}
	return 0;
}
例题5-12:城市正视图(Urban Elevations,UVa221) P132

输入案例:
14
160 0 30 60 30
125 0 32 28 60
95 0 27 28 40
70 35 19 55 90
0 0 60 35 80
0 40 29 20 60
35 40 25 45 80
0 67 25 20 50
0 92 90 20 80
95 38 55 12 50
95 60 60 13 30
95 80 45 25 50
165 65 15 15 25
165 85 10 15 35
0

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100 + 5;
struct Building
{
	int id;
	double x, y, w, d, h;
	bool operator<(const Building&rhs)const {
		return x < rhs.x || (x == rhs.x&&y < rhs.y);
	}
}b[maxn];
int n;
double x[maxn * 2];
bool cover(int i, double mx)
{
	return b[i].x <= mx&&b[i].x + b[i].w >= mx;
}

//判断建筑i在x=mx处是否可见
bool visible(int i, double mx)
{
	if (!cover(i, mx))
		return false;
	for (int k = 0;k < n;k++)
	{
		if (cover(k, mx) && b[k].y < b[i].y&&b[k].h >= b[i].h)
			return false;
	}
}
int main()
{
	/*ios::sync_with_stdio(false);*/
	freopen("input.txt", "r", stdin);
	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 + n * 2);
		int m = unique(x, x + n * 2) - x;//x坐标排序后去重,得到m个坐标

		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);
		}
		printf("\n");
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值