Winter training 1.21

Winter training 1.21

A - Necklace

Insert picture description here

Formula derivation problem

Find the number of rings to make the necklace the longest, n = vt/(2*v0), it should be noted that when there are two solutions, 0 is output, so it is necessary to judge the relationship between the difference between n and n rounded down and 0.5

Complete code:

#include<iostream>

using namespace std;
int vt,v0;
int n;
int main(){
	while(cin>>vt>>v0&&(vt+v0)){
		if(vt<=v0) printf("0\n");
		else if(vt<=2*v0) printf("1\n");
		else if(0.5*vt/v0-(int)(0.5*vt/v0)==0.5){
				printf("0\n");
		}
		else if(0.5*vt/v0-(int)(0.5*vt/v0)<0.5)
	            printf("%d\n",(int)(int)(0.5*vt/v0));
	    else 
	           printf("%d\n",(int)(int)(0.5*vt/v0)+1);
		
	}
	
	return 0;
}

B - Bode Plot

Consider the AC circuit below. We will assume that the circuit is in steady-state. Thus, the voltage at nodes 1 and 2 are given by v1 = VS coswt and v2 = VRcos (wt + q ) where VS is the voltage of the source, w is the frequency (in radians per second), and t is time. VR is the magnitude of the voltage drop across the resistor, and q is its phase.
img
You are to write a program to determine VR for different values of w. You will need two laws of electricity to solve this problem. The first is Ohm’s Law, which states v2 = iR where i is the current in the circuit, oriented clockwise. The second is i = C d/dt (v1-v2) which relates the current to the voltage on either side of the capacitor. "d/dt"indicates the derivative with respect to t.

Input

The input will consist of one or more lines. The first line contains three real numbers and a non-negative integer. The real numbers are VS, R, and C, in that order. The integer, n, is the number of test cases. The following n lines of the input will have one real number per line. Each of these numbers is the angular frequency, w.

Output

For each angular frequency in the input you are to output its corresponding VR on a single line. Each VR value output should be rounded to three digits after the decimal point.

Sample Input

1.0 1.0 1.0 9
0.01
0.031623
0.1
0.31623
1.0
3.1623
10.0
31.623
100.0

Sample Output

0.010
0.032
0.100
0.302
0.707
0.953
0.995
1.000
1.000

The main idea of ​​the topic: Find the relationship according to the formula given in the topic

Insert picture description here

Complete code:

#include<iostream>
#include<cmath>
using namespace std;
double vs,r,c,w;
int n;
int main(){
	cin>>vs>>r>>c>>n;
	while(n--){
		cin>>w;
		printf("%.3lf\n",r*c*w*vs*sqrt(1.0/(r*r*c*c*w*w+1)));
	}
	return 0;
}

C - Symmetric Matrix

Insert picture description here

The main idea of ​​the topic: Given a square matrix, judge whether the elements in the matrix meet the central symmetry, and require the elements in the matrix to be non-negative, and any other matrix is ​​asymmetric

Idea: Observe the matrix subscript, the non-centrosymmetric matrix satisfies M (i,j)!=M(n-1-i,n-1-j)

Complete code:

#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll M[101][101];
int main()
{
	int  T,N;
	char ch;
	cin>>T;
	for (int t = 1 ; t <= T ; ++ t) {
		getchar();
		scanf("N = %d",&N);
		for (int i = 0 ; i < N ; ++ i)
		for (int j = 0 ; j < N ; ++ j)
			scanf("%lld",&M[i][j]);
		int flag = 1;
		for (int i = 0 ; i < N ; ++ i) {
			for (int j = 0 ; j < N ; ++ j)
				if (M[i][j] < 0 || M[i][j] != M[N-1-i][N-1-j]) {
					flag = 0;
					break;
				}
			if (!flag) break;
		}
		
		printf("Test #%d: ",t);
		if (flag) 
			printf("Symmetric.\n");
		else printf("Non-symmetric.\n");
	}
    return 0;
}

D - Homogeneous Squares

Assume you have a square of size n that is divided into n × n positions just as a checkerboard. Two positions (x1, y1) and (x2, y2), where 1 ≤ x1, y1, x2, y2 ≤ n, are called “independent” if they occupy different rows and different columns, that is, x1 ≠ x2 and y1 ≠ y2. More generally, n positions are called independent if they are pairwise independent. It follows that there are n! different ways to choose n independent positions.Assume further that a number is written in each position of such an n × n square. This square is called “homogeneous” if the sum of the numbers written in n independent positions is the same, no matter how the positions are chosen. Write a program to determine if a given square is homogeneous!

Input

The input contains several test cases.The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines contains n numbers, separated by exactly one space character. Each number is an integer from the interval [−1000000, 1000000].The last test case is followed by a zero.

Output

For each test case output whether the specified square is homogeneous or not. Adhere to the format shown in the sample output.

Sample Input

2
1 2
3 4
3
1 3 4
8 6 -2
-3 4 0
0

Sample Output

homogeneous
not homogeneous

The main idea of ​​the topic: Determine whether the sum of the numbers in n independent (different positions, x1!=x2, y1!=y2) positions are equal, and output "homogeneous" if they are equal, otherwise output "not homogeneous"

Idea: Find a simple matrix of order 2 and above to get the relationship recursively, judging the n*n order matrix can be turned into judging whether n 2*2 order matrices meet the meaning of the question

Complete code:

#include<iostream>
#include<cstdio>
int n;
int mp[1001][1001];
int f;
int main()
{
    int i,j;
    while(~scanf("%d",&n)&&n)
    {
        f=1;
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
                scanf("%d",&mp[i][j]);
        for(i=1;f&&i<n;i++)
            for(j=1;f&&j<n;j++)
                if(mp[i][j]+mp[i+1][j+1]!=mp[i][j+1]+mp[i+1][j])
                    f=0;
        if(f)
        printf("homogeneous\n");
        else
        printf("not homogeneous\n");
    }
    return 0;
}

E - To the Max

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:

0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:

9 2
-4 1
-1 8
and has a sum of 15.

Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

Sample Output

15

The main idea of ​​the topic: It is derived from seeking the largest subsequence and evolution of a one-dimensional array,

Given a two-dimensional array consisting of positive and negative integers:: A sub-rectangle refers to any continuous sub-array with a size of 1*1 or greater in the entire array. The sum of a rectangle is the sum of all elements in the rectangle. In this problem, the sub-rectangle with the largest sum is called the largest sub-rectangle.

Idea: Convert a two-dimensional array into a one-dimensional array to find the maximum subsequence sum

The main code:

	for(int i = 1;i<=n;++i){//二维转一维
		fill(a,a+N,0);
		for(int j = i;j<=n;++j){
			for(int k = 1;k<=n;++k)
			a[k]+=mp[j][k];
			int maxx = maxl(a,n);
			if(maxx>ans)
			  ans = maxx;
		}
	}
int maxl(int a[],int n){//求最大子列和
	int maxsum = -inf,thissum=0;
	for(int i = 1;i<=n;++i){
		thissum+=a[i];
		if(thissum>maxsum)
		   maxsum = thissum;
		if(thissum<0)
		thissum = 0;
	}
	return maxsum;
}

Complete code:

#include<iostream>
using namespace std;
const int N = 106;
const int inf = 99999999;
int mp[N][N];
int a[N];
int n,ans=-inf;
int maxl(int a[],int n){
	int maxsum = -inf,thissum=0;
	for(int i = 1;i<=n;++i){
		thissum+=a[i];
		if(thissum>maxsum)
		   maxsum = thissum;
		if(thissum<0)
		thissum = 0;
	}
	return maxsum;
}
int main(){	
	cin>>n;
	for(int i = 1; i<=n;++i)
	  for(int j = 1; j<=n;++j)
	     cin>>mp[i][j];
	for(int i = 1;i<=n;++i){
		fill(a,a+N,0);
		for(int j = i;j<=n;++j){
			for(int k = 1;k<=n;++k)
			a[k]+=mp[j][k];
			int maxx = maxl(a,n);
			if(maxx>ans)
			  ans = maxx;
		}
	}
	    cout<<ans<<endl;
	return 0;
}

F - Who’s in the Middle

FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median’ cow gives: half of the cows give as much or more than the median; half give as much or less.

Given an odd number of cows N (1 <= N < 10,000) and their milk output (1…1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.

Input

* Line 1: A single integer N

* Lines 2…N+1: Each line contains a single integer that is the milk output of one cow.

Output

* Line 1: A single integer that is the median milk output.

Sample Input

5
2
4
1
3
5

Sample Output

3

The main idea and idea of ​​the topic: Give a set of values ​​(odd number) and find the value. Take the middle value after sorting

The main code:

#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5;
int a[N];
int n;
int main(){
	cin>>n;
	for(int i = 1;i<=n;++i){
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	cout<<a[n/2+1]<<endl;
	
	return 0;
}

G - Train Swapping

[External link image transfer failed. The source site may have an anti Insert picture description here
-leech link mechanism. It is recommended to save the image and upload it directly (im g-1q9NDgcH-1611306743498)(C:\Users\29252\Pictures\topic\G.png)]

The main idea of ​​the topic and its ideas: Given multiple test samples, each group of data needs to be exchanged several times in order. Simple bubble sorting, the cumulative number of exchanges to get the answer

Complete code:

#include<iostream> 
using namespace std;
const int N = 1e4+5;
int a[N];
int n,l,ans;
int main(){
	cin>>n;
	while(n--){
		ans = 0;
		cin>>l;
		for(int i = 0; i<l;++i){
			cin>>a[i];
		}
		for(int i = 0;i<l-1;++i){
			for(int j = 0;j<l-1-i;++j)
			if(a[j]>a[j+1]){
				swap(a[j],a[j+1]);
				ans++;
			}
		}
		printf("Optimal train swapping takes %d swaps.\n",ans);
	}
	return 0;
}

H - DNA Sorting

One measure of unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequenceDAABEC’’, this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequenceZWQM’’ has 6 inversions (it is as unsorted as can be—exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of sortedness'', frommost sorted’’ to ``least sorted’’. All the strings are of the same length.

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.

Output

Output the list of input strings, arranged from most sorted'' toleast sorted’’. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

Main idea:

The reverse number in a character string is the number of character pairs that are in reverse order in the character string. For example, the reverse number of the letter sequence "DAABEC" is 5, because D is larger than the 4 letters to the right, and E is larger than 1 letter to the right. The inverse number of the sequence "AACEDGG" is 1 (E and D), which is almost in order, while the inverse number of the sequence "ZWQM" is 6, which is not in order at all.

• You want to classify the DNA string sequence (sequence contains only 4 letters A, C, G and T). However, the classification is not in alphabetical order, but in the order of "sorted", from "most sorted" to "least sorted". All strings are the same length

Ideas: • The "most sorted" string refers to the string with the least number of reverse logarithms in the string; and the string with the most reverse logarithm in the string is the so-called "least sorted" string. So suppose the DNA sequence is a string array s , where the i- th DNA string is s [ i ]; the logarithm in reverse order is cnt. First, j puts the string and the reverse logarithm cnt into the structure, uses bubble sorting to count the reverse logarithm of each DNA string, and then sorts the reverse logarithm from less to more, and outputs the string

The main code:

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N = 1e4;
struct data{
	string str;
	int cnt,id;
	bool operator < (data x)const{
	    return cnt!=x.cnt? cnt<x.cnt:id<x.id ;
	}
}d[N];
int main(){
	int n,m;
	cin>>n>>m;
	for(int i = 1;i<=m;++i){
		cin>>d[i].str;
		d[i].cnt=0;
		for(int j = 0;j<n;++j){
			for(int k = j+1;k<n;++k)
			if(d[i].str[k]<d[i].str[j]) d[i].cnt++;
			d[i].id = i;
		}
	}
	sort(d+1,d+m+1);
	for(int i=1;i<=m;++i){
		cout<<d[i].str<<endl;
	}
	
}

#include
using namespace std;
const int N = 1e4;
struct data{
string str;
int cnt,id;
bool operator < (data x)const{
return cnt!=x.cnt? cnt<x.cnt:id<x.id ;
}
}d[N];
int main(){
int n,m;
cin>>n>>m;
for(int i = 1;i<=m;++i){
cin>>d[i].str;
d[i].cnt=0;
for(int j = 0;j<n;++j){
for(int k = j+1;k<n;++k)
if(d[i].str[k]<d[i].str[j]) d[i].cnt++;
d[i].id = i;
}
}
sort(d+1,d+m+1);
for(int i=1;i<=m;++i){
cout<<d[i].str<<endl;
}

}


Guess you like

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