Waterloo Cup Algorithm Training Solution _ algorithm

Source: Internet
Author: User
Tags cmath
1. Interval k large number of queries can be sorted first, you can also use the idea of fast-row.
/************************************************************************* > File Name:algo_1.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2014 year November 08 Saturday 12:58 31 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;

typedef long Long LL;

Const double ESP = 1e-5;

#define N 1010 int num[n], tmp[n]; int findk (intK, int l, int r) {int key = Tmp[l];
	int i = l;
	int J = r;
		When I==j found the key in the array subscript, is I while (I < J) {while (I < J && Tmp[j] >= key) {--j;
		} Tmp[i] = Tmp[j];
		while (I < J && Tmp[i] <= key) {++i;
	} Tmp[j] = Tmp[i];
	} Tmp[i] = key;
	if (i = = k) {return tmp[k];
	else if (I < k) {return findk (k, i + 1, R);
	else {return findk (K, L, r-1);
	int main (int argc, char *argv[]) {int n, m, L, R, K;
		while (scanf ("%d", &n)!= EOF) {for (int i = 1; I <= n; ++i) {scanf ("%d", &num[i]);
		} scanf ("%d", &m);
			for (int i = 0; i < m ++i) {scanf ("%d%d%d", &l, &r, &k);
			int cnt = 0;
			for (int j = l; J <= R; ++j) {tmp[cnt++] = num[j];
			}//This first sort, and then take the K-large complexity is MNLOGN//here to the amount of data is small, can endure, there is a way//is the use of the idea of fast, you can reduce complexity to MN, single//query K large complexity is logn.
			From small to large sort//sort (TMP, TMP + CNT);

			The subscript of the large k is the Cnt-k//printf ("%d\n", tmp[cnt-k); You need to look for the number of cnt-k subscript ("%d\n", FINDK (cnt-k, 0, cnt-1));
} return 0;
}/* Algorithm training interval k large number of query time limit: 1.0s memory Limit: 256.0MB problem description Given a sequence, each query sequence in the number of L to the first r number of K-large numbers are.
Input format the first row contains a number of n, which indicates the length of the sequence.
The second row contains n positive integers, representing the given sequence.
The third contains a positive integer m, which indicates the number of queries. Next, line m, three numbers per line, l,r,k the query sequence from left to right to the number of R number, from the large to small K large numbers are.
The sequence elements begin with 1 marking.
The output format outputs a total of M rows, one number per line, to indicate the answer to the query. Sample Input 5 1 2 3 4 5 2 1 5 2 2 3 2 sample output 4 2 data scale and convention for 30% data,n,m<=100; for 100% data,n,m<=1000; guarantee k<= (r-l+1), number of sequences <=10
^6. */

2. The maximum LCM is divided into several situations to discuss the line. But because of the Waterloo Cup website problem, only to 60%.
/************************************************************************* > File Name:algo_2.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2014 year November 08 Saturday 13:26 29 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;

typedef long Long LL;

Const double ESP = 1e-5;
 * * This training site on the Waterloo Cup only 60 points * do not know what the reason. */int Main (int argc, char *argv[]) {ll n;
		while (CIN >> N) {ll ans;
		if (n <= 2) {//handled well special case ans = n;
		else if (n% 2 = 1) {//when n is odd, n,n-1,n-2 three numbers must be coprime//adjacent two integers must be coprime ans = n * (n-1) * (n-2);
		else if (n% 3!= 0) {//when n is an even number, and n cannot be divisible by 3, then n,n-1,n-3//These 3 numbers must be coprime and no larger number can be found ans = n * (n-1) * (n-3);
		else {//when n is even, and can be divisible by 3, according to the above argument,//This 3 number coprime, and can not find a larger number of ans = (n-1) * (n-2) * (n-3);
	cout << ans << endl;
return 0;
}/* Algorithm training maximum LCM time limit: 1.0s memory Limit: 256.0MB problem description Known a positive integer n, ask from 1~n to choose three number, their LCM maximum can be as much.
Input format Enter a positive integer n.
The output format outputs an integer that indicates the LCM you have found.
Sample input 9 Sample output 504 data scale with Convention 1 <= N <= 10^6. */

3.K good number of simple digital DP.
/************************************************************************* > File Name:algo_3.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2014 year November 08 Saturday 20:32 00 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;

typedef long Long LL;

Const double ESP = 1e-5; #define N #define MOD (1000000007)//digit Dp,dp[i][J] indicates that the highest digit of the I-digit is the number int dp[n][n] of J;
	int main (int argc, char *argv[]) {int K, L;
		while (scanf ("%d%d", &k, &l)!= EOF) {CLR (DP, 0);
		for (int i = 0; i < K; ++i) {dp[1][i] = 1; for (int i = 2; I <= L. ++i) {for (int j = 0; j < K; ++j) {for (int K = 0; k < K; ++k) {if k
					!= j-1 && k!= J + 1) {Dp[i][j] = (Dp[i][j] + dp[i-1][k])% MOD;
		int ans = 0;
		for (int i = 1; i < K; ++i) {ans = (ans + dp[l][i])% MOD;
	printf ("%d\n", ans);
return 0; }/* Algorithm training K good number of time limit: 1.0s memory Limit: 256.0MB problem description If any of the adjacent two digits in the K-binary representation of a natural number n are not adjacent digits, then we say this number is k good.
Find the number of K in the L-bit K-number.
For example k = 4,l = 2, all k good numbers are 11, 13, 20, 22, 30, 31, 33 altogether 7.

Because this number is very large, please output it to 1000000007 after modulo the value.

Input format input contains two positive integers, K and L.
The output format outputs an integer representing the value of the answer to the 1000000007 modulo.
Sample Input 4 2 sample output 7 data size and convention for 30% data, KL <= 10^6; for 50% of the data, K <=, L <= 10, 100% <= k,l <= 1. */

4. Node selection tree DP, using dp[u][1] to express the U node is selected after the maximum subtree, with dp[u][0] to represent the U node is not selected after the maximum subtree. Because it is a tree, you use DFS to recursively deduce it.
/************************************************************************* > File Name:algo_4.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2015 year March 19 Thursday 14:04 19 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;
typedef long Long LL;

typedef unsigned long long ull;

Const double ESP = 1e-5; #define N 100010 INT Dp[n][2], N;

Vector<int> Edge[n];
		void Dfs (int u, int f) {for (int i = 0; i < sz (Edge[u]); ++i) {int v = edge[u][i];
			if (f!= v) {dfs (V, u);
			DP[U][1] + = dp[v][0];
		Dp[u][0] + = max (dp[v][0], dp[v][1]);
		int main (int argc, char *argv[]) {while (scanf ("%d", &n)!= EOF) {CLR (DP, 0);
		for (int i = 1; I <= n; ++i) {scanf ("%d", &dp[i][1]);
			for (int i = 1; i < n; ++i) {int u, v;
			scanf ("%d%d", &u, &v);
			EDGE[U].PB (v);
		EDGE[V].PB (U);
		DFS (1,-1);
	printf ("%d\n", dp[1][0]);
return 0; }/* Problem description There is a tree n nodes, each node in the tree has a positive integer weight value. If a point is selected, then the dots on the tree and its neighbors cannot be selected.

To find the right value and the maximum number of points to be chosen.

Input format the first row contains an integer n.

The next line contains n positive integers, and the first positive integer represents the weight of point I.

Next, a total of n-1 lines, each describing one edge of the tree.
The output format outputs an integer that represents the maximum value of the weighted value of the selected point.
Sample Input 5 1 2 3 4 5 1 2 1 3 2 4 2 5 Sample Output 12 Sample Description Select 3, 4, 5th points, weights and for 3+4+5 = 12.

Data size and convention for 20% of the data, N <= 20.

For 50% of the data, N <= 1000.

For 100% of the data, N <= 100000.
A positive integer with a weight of not more than 1000. */

5. Shortest path of single source, using Dijkstra algorithm, because the data is relatively large, need to use heap optimization algorithm. Just started using the normal algorithm, a part of the timeout.
/************************************************************************* > File Name:algo_5.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2015 year March 19 Thursday 14:58 31 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;
typedef long Long LL;

typedef unsigned long long ull;

Const double ESP = 1e-5; #define N 20010 Struct Node {int V, D;};
int n, M, D[n], vis[n];

Vector<node> Edge[n];
	The normal Dijkstra run timeout is done using the heap-optimized Dijkstra void Dijkstra (void) {clr (d, 0);
	CLR (VIS, 0);
	int s = 1;
		for (int i = 1; I <= n; ++i) {if (i = = s) {D[i] = 0;
		else {d[i] = INF;
		for (int i = 1; I <= n; ++i) {int x;
		int dis = INF;
				for (int j = 1; j <= N; ++j) {if (vis[j] = = 0 && dis > d[j]) {x = j;
			dis = d[j];
		}} Vis[x] = 1;
			The subscript for the vector is 0 for (int j = 0; J < sz (edge[x]); ++j) {int v = EDGE[X][J].V;
		D[v] = min (D[v], d[x] + edge[x][j].d); int main (int argc, char *argv[]) {while (scanf ("%d%d", &n, &m)!= EOF) {for (int i = 0; i < m; ++i
			) {int u, V, l;
			scanf ("%d%d%d", &u, &v, &l);
			Node x;
			X.V = v;
			X.D = l;
		EDGE[U].PB (x);
		} Dijkstra ();
		for (int i = 2; I <= n; ++i) {printf ("%d\n", D[i]);
} return 0;
}/* Problem description given an n vertex, a direction graph of the M bar (some of which may be negative but guaranteed to have no negative loops). Please calculate the number from point 1th to the other point.The shortest path (the vertices are numbered from 1 to n).

Input format first line two integers n, M.

The next line of M, each row has three integers u, V, L, which means that u to V has a side with a length of L.
The output format is a total n-1 line, and line I represents the shortest path from point 1th to I+1.

Sample Input 3 3 1 2-1 2 3-1 3 1 2 Sample output-1-2 data size with convention for 10% data, n = 2,m = 2.

For 30% of the data, n <= 5,m <= 10.
For 100% of the data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000 to ensure that all other vertices are reachable from any vertex. */

/************************************************************************* > File Name:algo_5_heap.cpp > Author:gwq > Mail:gwq5210@qq.com > Created time:2015 year March 19 Thursday 14:58 31 sec *********************************** /#include <cmath> #include <ctime> #include <cctype> #include <climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;
typedef pair<int, int> PII;
typedef long Long LL;

typedef unsigned long long ull; ConstDouble esp = 1e-5;

#define N 20010 struct Node {int V, D;};
int n, M, D[n], vis[n];

Vector<node> Edge[n];
	void Dijkstra (void) {clr (VIS, 0);
		for (int i = 1; I <= n; ++i) {if (i = = 1) {D[i] = 0;
		else {d[i] = INF;
	} priority_queue<pii, Vector<pii>, greater<pii> > PQ;
	Pq.push (Make_pair (0, 1));
		while (!pq.empty ()) {PII x = Pq.top ();
		Pq.pop ();
		int u = x.second;
		if (Vis[u]) {continue;
		} Vis[u] = 1;
			for (int i = 0; i < sz (Edge[u]); ++i) {int v = EDGE[U][I].V;
				if (D[v] > D[u] + edge[u][i].d) {D[v] = D[u] + edge[u][i].d;
			Pq.push (Make_pair (d[v], v));  int main (int argc, char *argv[]) {while (scanf ("%d%d", &n, &m)!= EOF) {for (int i = 0; i < m;
			++i) {int u, V, l;
			scanf ("%d%d%d", &u, &v, &l);
			Node x;
			X.V = v;
			X.D = l;
		EDGE[U].PB (x);
		} Dijkstra ();
		for (int i = 2; I <= n; ++i) {printf ("%d\n", D[i]);
} return 0; }/* Problem descriptionGiven an n vertex, a direction graph of the M bar (some of which may be negative, but guaranteed to have no negative loops).

Please calculate the shortest path from point 1th to other points (vertices from 1 to n).

Input format first line two integers n, M.

The next line of M, each row has three integers u, V, L, which means that u to V has a side with a length of L.
The output format is a total n-1 line, and line I represents the shortest path from point 1th to I+1.

Sample Input 3 3 1 2-1 2 3-1 3 1 2 Sample output-1-2 data size with convention for 10% data, n = 2,m = 2.

For 30% of the data, n <= 5,m <= 10.
For 100% of the data, 1 <= n <= 20000,1 <= m <= 200000,-10000 <= L <= 10000 to ensure that all other vertices are reachable from any vertex. */

6. The cost of comforting cows for each additional edge is 2 * w + c[u] + c[v], which can be used as a new weight to obtain the result with the minimum spanning tree algorithm. Finally, add the time of residence, of course, the time spent on this residence should be the smallest.
/************************************************************************* > File Name:algo_6.cpp > Author: Gwq > Mail:gwq5210@qq.com > Created time:2015 year March 20 Friday 16:29 52 sec ******************************************* /#include <cmath> #include <ctime> #include <cctype> #include < climits> #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include < set> #include <queue> #include <stack> #include <string> #include <vector> #include < sstream> #include <iostream> #include <algorithm> #define INF (INT_MAX/10) #define CLR (arr, Val) memset (
Arr, Val, sizeof (arr)) #define PB push_back #define SZ (a) ((int) (a). Size ()) using namespace Std;
typedef set<int> SI;
typedef vector<int> VI;
typedef map<int, Int> Mii;

typedef long Long LL;

Const double ESP = 1e-5;

#define N 100010 int c[n], n, p, m[n]; struct NODe {int U, V, W;
		void input (void) {scanf ("%d%d%d", &u, &v, &w); Take this for the new weight value.
	Plus, the time of residence is OK W = 2 * w + c[u] + c[v];

}}node[n];

BOOL CMP (node u, node v) {return U.W < V.W;}

int find (int x) {return ((m[x] = = x)? x: (M[x] = find (m[x)));
		int main (int argc, char *argv[]) {while (scanf ("%d%d", &n, &p)!= EOF) {int minn = INF;
			for (int i = 1; I <= n; ++i) {scanf ("%d", &c[i]);
		Minn = MIN (minn, c[i]);
		for (int i = 0; i < P; ++i) {node[i].input ();
		} Sort (node, node + P, CMP);
		for (int i = 1; I <= n; ++i) {m[i] = i;
		int ans = 0;  for (int i = 0; I < P;
			++i) {int x = find (NODE[I].U);
			int y = find (NODE[I].V);
				if (x!= y) {m[x] = y;
			Ans + + node[i].w;
	} printf ("%d\n", ANS + minn);
return 0; }/* Problem description farmer John became very lazy, and he did not want to continue to maintain the road between the cows for passage. Roads are used to connect n pastures, and pastures are numbered 1 to n consecutively. Every pasture is a cow's home. FJ plans to remove as many roads as possible from the P path, but also to maintain connectivity between pastures. The first thing you have to decide is that those roads are the N-1 path that needs to be preserved. The J Strip two-way road connects the pasture SJ and EJ (1 <= SJ &LT;= N;
1 <= Ej <= N; Sj!= Ej), and it takes LJ time to walk through it. No two pastures are connected by more than one road. The cows were very sad because their transportation system had been cut.
You need to go to every cow's place to comfort them.
Every time you get to the first ranch (even if you've been there), you have to spend CI time talking to cows.
You spend the night at the same ranch (which is for your choice), until the cows have come down from their sadness.
When you get up in the morning and go back to bed at night, you need to talk to the cows in the pasture where you sleep. So you can finish your conversation task.

Assuming that farmer John has taken your advice, calculate the minimum amount of time to be comforted by all cows.

The input format line 1th contains two integers n and p.

Next n rows, each line contains an integer CI.

Next P line, each row contains three integers SJ, EJ and LJ.
The output format outputs an integer, the total time required (including two talk time with cows on your ranch). Sample Input 5 7 10 10 20 6 30 1 2 5 2 3 5 2 4 12 3 4 17 2 5 15 3 5 Sample output 6 data scale with convention 176 <= N <= 10000,n-1 <= P <= 5
000,0 <= Lj <= 1000,1 <= Ci <= 1,000. */


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.