Codeforces Round #296 (Div. 2)

A. Playing with Paper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm  ×  b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.

Codeforces Round #296 (Div. 2)_第1张图片

After making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm  ×  b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.

Can you determine how many ships Vasya will make during the lesson?

Input

The first line of the input contains two integers ab (1 ≤ b < a ≤ 1012) — the sizes of the original sheet of paper.

Output

Print a single integer — the number of ships that Vasya will make.

Sample test(s)
input
2 1
output
2
input
10 7
output
6
input
1000000000000 1
output
1000000000000
Note

Pictures to the first and second sample test.

Codeforces Round #296 (Div. 2)_第2张图片




AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;

LL a, b;

int main() {
	while(cin >> a >> b) {
		LL ans = 0;
		while(a != 0 && b != 0) {
			ans += (a / b);
			a = a % b;
			if(a < b) swap(a, b);
		}
		cout << ans << endl;
	} 
	return 0;
} 








B. Error Correct System
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters.

Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.

Help him do this!

Input

The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T.

The second line contains string S.

The third line contains string T.

Each of the lines only contains lowercase Latin letters.

Output

In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.

In the second line, either print the indexes i and j (1 ≤ i, j ≤ ni ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print "-1 -1", if it is not necessary to swap characters.

If there are multiple possible answers, print any of them.

Sample test(s)
input
9
pergament
permanent
output
1
4 6
input
6
wookie
cookie
output
1
-1 -1
input
4
petr
egor
output
2
1 2
input
6
double
bundle
output
2
4 1
Note

In the second test it is acceptable to print i = 2j = 3.




AC代码:

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

const int maxn = 200005;
char s1[maxn], s2[maxn];
int N, cnt;
int pos[30][30];	//记录字符对的位置 

int work1() {		//找出是否可以交换一下,使得有两对相同字母 
	for(int i = 0; i < 26; i++) {
		for(int j = 0; j < 26; j++) {
			if(i == j) continue;
			if(pos[i][j] != 0 && pos[j][i] != 0) {
				cout << cnt - 2 << endl;
				cout << pos[i][j] << ' ' << pos[j][i] << endl;
				return 0;
			}
		}
	}
	return 1;
}

int work2() {	//找出是否可以交换一下,使得有一对相同字母 
	for(int i = 0; i < 26; i++) {
		for(int j = 0; j < 26; j++) {
			if(i != j && pos[i][j] != 0)
				for(int k = 0; k < 26; k++)
					if(j != k && pos[j][k] != 0)
					{
						cout << cnt - 1 << endl;
						cout << pos[i][j] << ' ' << pos[j][k] << endl;
						return 0;
					}
		}
	}
	return 1;
}

int main() {
	while(cin >> N) {
		cin >> (s1 + 1) >> (s2 + 1);
		
		cnt = 0;
		memset(pos, 0, sizeof(pos));
		
		for(int i = 1; i <= N; i++) {
			if(s1[i] != s2[i]) cnt++;
			pos[s1[i] - 'a'][s2[i] - 'a'] = i;
		}
		
		if(work1())
			if(work2()) {
				cout << cnt << endl;
				cout << "-1 -1\n";
			}
	}
	return 0;
}






C. Glass Carving
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular wmm  ×  h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.

In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.

After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.

Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?

Input

The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 0001 ≤ n ≤ 200 000).

Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.

Output

After each cut print on a single line the area of the maximum available glass fragment in mm2.

Sample test(s)
input
4 3 4
H 2
V 2
V 3
V 1
output
8
4
4
2
input
7 6 5
H 4
V 3
V 5
H 2
V 1
output
28
16
12
6
4
Note

Picture for the first sample test:

Codeforces Round #296 (Div. 2)_第3张图片 Picture for the second sample test: Codeforces Round #296 (Div. 2)_第4张图片




AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#define LL long long
using namespace std;

set<int> H, W;
int hc[200005], wc[200005];
int w, h ,n;

int main() {
	while(cin >> w >> h >> n) {
		memset(hc, 0, sizeof(hc));
		memset(wc, 0, sizeof(wc));
		H.clear(); W.clear();
		set<int> :: iterator it1, it2;
		
		hc[h]++; wc[w]++;
		H.insert(0);
		H.insert(h);
		W.insert(0);
		W.insert(w);
		
		LL wm = w, hm = h;
		for(int i = 0 ; i < n; i++) {
			char s[5];
			int x;
			scanf("%s %d", s, &x);
			if(s[0] == 'V') {
				W.insert(x);
				it1 = it2 = W.find(x);
				it1 ++; it2--;
				int l = *it2, r = *it1;
				wc[r - l] --;
				wc[x - l] ++;
				wc[r - x] ++;
			}
			else {
				H.insert(x);
				it1 = it2 = H.find(x);
				it1 ++; it2 --;
				int l = *it2, r = *it1;
				hc[r - l] --;
				hc[x - l] ++;
				hc[r - x] ++;
			}
			
			while(!wc[wm]) wm --;
			while(!hc[hm]) hm --;
			
			cout << wm * hm << endl;
		}
	}
	return 0;
} 






你可能感兴趣的:(ACM,codeforces,贪心)