Winter training 1.22

Winter training 1.22

A - Brainman

Background
Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.

Problem
Here’s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 2 8 0 3
swap (2 8) 8 2 0 3
swap (2 0) 8 0 2 3
swap (2 3) 8 0 3 2
swap (8 0) 0 8 3 2
swap (8 3) 0 3 8 2
swap (8 2) 0 3 2 8
swap (3 2) 0 2 3 8
swap (3 8) 0 2 8 3
swap (8 3) 0 2 3 8
So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:
Start with: 2 8 0 3
swap (8 0) 2 0 8 3
swap (2 0) 0 2 8 3
swap (8 3) 0 2 3 8
The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond’s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

Input

The first line contains the number of scenarios.
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.

Output

Start the output for every scenario with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.

Sample Input

4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0

Sample Output

Scenario #1:
3

Scenario #2:
0

Scenario #3:
5

Scenario #4:
0

The main idea of ​​the topic: to sort the given sequence, what is the minimum number of interchanges of adjacent numbers

Idea: The exchange of adjacent elements satisfies the merge sort. For a number in the sequence, the number of numbers that are greater in the front and less than it in the back is

The reverse logarithm of the number. The reverse logarithm of a sequence is the sum of the reverse logarithm of all the numbers in the sequence. So the meaning of the question is to calculate the reverse logarithm of a sequence

The merge sort is to divide the sequence a [ l , r ] into two sequences a [ l , mid ] and a [ mid + 1, r ], merge and sort them separately, and then merge and sort the two ordered sequences. In In the process of merging and sorting, let l <= i <= mid , mid +1<= j <= r , when a [ i ] <= a [ j ], no reverse logarithm will be generated; and when a [ i ]> a [ j ], then in the ordered sequence a [ l , mid ], inThe numbers after a [ i ] are larger than a [ j ]. Put a [ j ] before a [ i ], and the reverse logarithm must add mid-i +1

The main code:

void merge(int p,int m,int q){
	int i=p,j=m+1,k=i;
	while(i<=m&&j<=q){
		if(a[i]<=a[j])
		b[k++] = a[i++];
		else
		b[k++]=a[j++],ans+=m-i+1;
	}
	while(i<=m) b[k++] =a[i++];
	while(j<=q) b[k++] =a[j++];
	for(i = p;i<=q;++i)
	 a[i] = b[i];
}
void mergesort(int p,int q){
	if(p==q) return ;
	else{
		int m = (p+q)/2;
		mergesort(p,m);
		mergesort(m+1,q);
		merge(p,m,q); 
	}
} 

Complete code:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e4+6;
int a[N];
int b[N];
int ans = 0;
void merge(int p,int m,int q){
	int i=p,j=m+1,k=i;
	while(i<=m&&j<=q){
		if(a[i]<=a[j])
		b[k++] = a[i++];
		else
		b[k++]=a[j++],ans+=m-i+1;
	}
	while(i<=m) b[k++] =a[i++];
	while(j<=q) b[k++] =a[j++];
	for(i = p;i<=q;++i)
	 a[i] = b[i];
}
void mergesort(int p,int q){
	if(p==q) return ;
	else{
		int m = (p+q)/2;
		mergesort(p,m);
		mergesort(m+1,q);
		merge(p,m,q); 
	}
} 
int main(){
	int t,n;
	cin>>t;
	for(int i = 1; i<=t;++i){
		cin>>n;
		ans = 0;
		for(int j = 1;j<=n;++j)
		   cin>>a[j];
		mergesort(1,n);
		printf("Scenario #%d:\n",i);
		cout<<ans<<endl;
		cout<<endl; 
	}
	return 0;
}

B - Ultra-QuickSort

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.

Input

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 – the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0

The main idea of ​​the topic: give n integers, sort from small to large, exchange two adjacent numbers that need to be exchanged, and find the minimum number of exchanges

The idea is the same as the first question, find the reverse order

Complete code:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 5e5+6;
int a[N];
int b[N];
long long ans;
void merge(int p,int m,int q){
	int i=p,j=m+1,k=i;
	while(i<=m&&j<=q){
		if(a[i]<=a[j])
		b[k++] = a[i++];
		else
		b[k++]=a[j++],ans+=m-i+1;
	}
	while(i<=m) b[k++] =a[i++];
	while(j<=q) b[k++] =a[j++];
	for(i = p;i<=q;++i)
	 a[i] = b[i];
}
void mergesort(int p,int q){
	if(p==q) return ;
	else{
		int m = (p+q)/2;
		mergesort(p,m);
		mergesort(m+1,q);
		merge(p,m,q); 
	}
}
int main(){
	int n;
	while(cin>>n&&n){
		ans = 0;
		for(int i = 1;i<=n;++i)
		    cin>>a[i];
		mergesort(1,n);
		cout<<ans<<endl;
	}
	
}

D - Word Amalgamation

In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.

Input

The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled ‘words’ that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.

Output

For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.

Sample Input

tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX

Sample Output

score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******

The main idea of ​​the topic: First, give a series of strings, as a dictionary, the input encounters ******, and then enter a series of words that need to be sorted, and output the words that exist in the dictionary for each word that needs to be sorted. , The arrangement of words can be different. If you don’t know a word in the dictionary, arrange them in lexicographical order. If you don’t find the corresponding word, output NOT A VALID WORD. Each output corresponds to a set of words or NOT A. ****** after VALID WORD.

Idea: Use str[] to store the dictionary of string numbers, s store the words that need to be sorted, and s1 is used to temporarily receive a string of str;

First use sort to sort the dictionary, and then enter a string to be sorted to sort it. At the same time, s1 receives the words in the dictionary and also sorts them, s1==s, indicating that the word exists in the dictionary

Complete code:

#include<iostream>
#include<algorithm>
#include<string> 
using namespace std;
const int N = 1e4+6;
string str[N],s,s1;
int cnt,flag;
int main(){
	while(cin>>str[cnt]&&str[cnt]!="XXXXXX"){
		cnt++;
	}
	sort(str,str+cnt);
	while(cin>>s&&s!="XXXXXX"){
		flag = 1;
		sort(s.begin(),s.end());
		for(int i = 0;i<cnt;++i){
			s1 = str[i];
			sort(s1.begin(),s1.end());
			if(s1==s){
				cout<<str[i]<<endl;
				flag = 0;
			}
		}
		if(flag) puts("NOT A VALID WORD");
		puts("******");
	}
	return 0;
}

E ranking

Although there is a real-time Ranklist for the computer test today, the ranking above is only based on the number of completed questions, without considering
the score of each question, so it is not the final ranking. Given the admission score line, please write a program to find the
candidates who passed the score line last , and print their scores in descending order.

Input

The test input contains information for several exams. The first line of each test information gives the number of candidates N (0 <N
<1000 ), the number of test questions M (0 <M <= 10 ), and the score line (positive integer) G; the second line gives the first question to The positive integer score of question M; the following N lines, each line gives a
candidate's admission ticket number (a string of no more than 20), the total number m of questions the student has solved, and the question number of the m question
(The question number is from 1 to M).
When the number of candidates read is 0, the input is over and the exam will not be processed.

Output

For each test, first output the number n of candidates who are not lower than the score line on the first line, and then
output the test number and score of the online candidates from high to low in line n, separated by a space. If multiple candidates have the same score, they will be
output in ascending order of their exam numbers.

Sample Input

4 5 25
10 10 12 13 15
CS004 3 5 1 3
CS003 5 2 4 1 3 5
CS002 2 1 2
CS001 3 2 3 5
1 2 40
10 30
CS001 1 2
2 3 20
10 10 10
CS000000000000000001 0
CS000000000000000002 2 1 2
0

Sample Output

3
CS003 60
CS001 37
CS004 37
0
1
CS000000000000000002 20


        
  

The main idea: the highest score

Idea: Use a structure to store the student number and score, calculate the total score based on the number of questions solved and the score of each question, and calculate the number of people who cross the line, and sort according to the total score

Complete code:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N = 1006;
struct data{
	string num;
	int score;
	bool operator <(data x)const{
	    return (score!=x.score)? score>x.score:num<x.num; 
	}
}s[N];
int cot;
int a[N];
int main(){
	int n,m,line;
	while(cin>>n&&n){
	   cin>>m>>line;
	   int sum,cnt;
	   cot = 0;
	   for(int i = 1;i<=m;++i)
	       cin>>a[i];
	    for(int i = 1;i<=n;++i){
	    	cin>>s[i].num>>cnt;
	    	sum = 0;
	    	for(int j = 1;j<=cnt;++j){
	    		int id;
	    		cin>>id;
	    		sum+=a[id];
			}
			s[i].score = sum;
			if(sum>=line) cot++;
		}
		cout<<cot<<endl;
		sort(s+1,s+1+n);
		for(int i = 1;i<=cot;++i){
			cout<<s[i].num<<" "<<s[i].score<<endl;
		}
	}

	return 0;
} 

F - Election Time

The cows are having their first election after overthrowing the tyrannical Farmer John, and Bessie is one of N cows (1 ≤ N ≤ 50,000) running for President. Before the election actually happens, however, Bessie wants to determine who has the best chance of winning.

The election consists of two rounds. In the first round, the K cows (1 ≤ KN) cows with the most votes advance to the second round. In the second round, the cow with the most votes becomes President.

Given that cow i expects to get Ai votes (1 ≤ Ai ≤ 1,000,000,000) in the first round and Bi votes (1 ≤ Bi ≤ 1,000,000,000) in the second round (if he or she makes it), determine which cow is expected to win the election. Happily for you, no vote count appears twice in the Ai list; likewise, no vote count appears twice in the Bi list.

Input

* Line 1: Two space-separated integers: N and K
* Lines 2…N+1: Line i+1 contains two space-separated integers: Ai and Bi

Output

* Line 1: The index of the cow that is expected to win the election.

Sample Input

5 3
3 10
9 2
5 6
8 4
6 5

Sample Output

5

The main idea: The cows are running for president. There are two rounds of voting. The first k cows in the first round of voting can vote in the second round, and finally output the number of the cow that is most likely to succeed in the election (the most votes)

Idea: The structure stores the votes and numbers of each cow in two rounds. The first round is sorted by the number of votes obtained in round a in descending order, and the second round is sorted by the number of votes obtained by the k cows before the first round, and finally the cow number is output.

Complete code:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 5e4+6;
struct data{
	ll a,b;
	int id;
}d[N];
int n,k;
bool cmpa(data p,data q){
	return p.a>q.a;
}
bool cmpb(data p,data q){
	return p.b>q.b;
}
int main(){
	cin>>n>>k;
	for(int i = 1;i<=n;++i){
		cin>>d[i].a>>d[i].b;
		d[i].id = i;
	}
	sort(d+1,d+n+1,cmpa);
	sort(d+1,d+k+1,cmpb);
	printf("%d\n",d[1].id);
	return 0;
}

G - Holiday Hotel

Mr. and Mrs. Smith are going to the seaside for their holiday. Before they start off, they need to choose a hotel. They got a list of hotels from the Internet, and want to choose some candidate hotels which are cheap and close to the seashore. A candidate hotel M meets two requirements:

  1. Any hotel which is closer to the seashore than M will be more expensive than M.
  2. Any hotel which is cheaper than M will be farther away from the seashore than M.

Input

There are several test cases. The first line of each test case is an integer N (1 <= N <= 10000), which is the number of hotels. Each of the following N lines describes a hotel, containing two integers D and C (1 <= D, C <= 10000). D means the distance from the hotel to the seashore, and C means the cost of staying in the hotel. You can assume that there are no two hotels with the same D and C. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, you should output one line containing an integer, which is the number of all the candidate hotels.

Sample Input

5
300 100
100 300
400 200
200 400
100 500
0

Sample Output

2

Main idea:

The Smiths are going to the beach for vacation. Before they leave, they have to choose a hotel. They obtained a list of hotels from the Internet, from which they had to choose some cheap hotels close to the beach. Candidate Hotel M has to meet two requirements:

• 1. Hotels closer to the beach than M are more expensive than M.

• 2. than M cheap hotels away from the beach than M away.

Output the number of candidate hotels that can meet the conditions

Idea: The structure stores the distance and price of each hotel. There are two sorting methods. 1. Sort by price as the first keyword, then find the nearest hotel, 2. Sort by distance as the first keyword, find the cheapest s price

Complete code:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e4+6;
struct data{
	int dis,cs;
	bool operator<(data x){
		return cs==x.cs? dis<x.dis:cs<x.cs;
	}
}d[N];
int n;
int main(){
	while(cin>>n&&n){
		int ans = 0;
		for(int i = 1; i<=n;++i){
			cin>>d[i].dis>>d[i].cs;
		}
		sort(d+1,d+n+1);
		int minx = 99999999;
		for(int i = 1;i<=n;++i){
			if(d[i].dis<minx){
				minx = d[i].dis;
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

The structure stores the distance and price of each hotel. There are two sorting methods, 1. Sort by price as the first key, then find the nearest hotel, 2. Sort by distance as the first key, find the cheapest price

Complete code:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e4+6;
struct data{
	int dis,cs;
	bool operator<(data x){
		return cs==x.cs? dis<x.dis:cs<x.cs;
	}
}d[N];
int n;
int main(){
	while(cin>>n&&n){
		int ans = 0;
		for(int i = 1; i<=n;++i){
			cin>>d[i].dis>>d[i].cs;
		}
		sort(d+1,d+n+1);
		int minx = 99999999;
		for(int i = 1;i<=n;++i){
			if(d[i].dis<minx){
				minx = d[i].dis;
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45719435/article/details/112991584