灵动ICPC冬令营基础-4

A - Necklace

题目

在这里插入图片描述
Sample Input

10 1
10 2
0 0

Sample Output

5
0

解析

在这里插入图片描述
注意:先看函数形式分类讨论,判断二次函数时,要判断函数极大值点在不在区间内,利用二次函数的性质判断出去最大值的点。

代码

#include <cstdio>
double v, v0, vt;
int main() {
    
    
	int n;
	while(~scanf("%lf %lf", &v, &v0)&&(v+v0)) {
    
    
		if(v>v0) {
    
    
			if(v>2*v0) {
    
    
				vt=1.*v/(2*v0);
				if(vt-(int)vt<0.5) n=(int)vt;
				else if(vt-(int)vt==0.5) n=0;
				else n=(int)vt+1;
			}
			else {
    
    
				n=1;
			}
		}
		else n=0;
		printf("%d\n", n);
	}
	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.
在这里插入图片描述

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

解析

在这里插入图片描述
注意:结合物理知识联立方程,解出输出解方程;

代码

#include <cstdio>
#include <cmath>
int main() {
    
    
	double vs, r, c, w, vr;
	int n;
	while(scanf("%lf%lf%lf%d", &vs, &r, &c, &n)!=EOF) {
    
    
		while(n--) {
    
    
			scanf("%lf", &w);
			vr=r*c*w*vs*sqrt(1/(r*r*c*c*w*w+1));
			printf("%.3lf\n", vr);
		}
	}
	return 0;
}

C - Symmetric Matrix

题目

在这里插入图片描述
Sample Input

2
N = 3
5 1 3
2 0 2
3 1 5
N = 3
5 1 3
2 0 2
0 1 5

Sample Output

Test #1: Symmetric.
Test #2: Non-symmetric.

解析

给出的方阵用二维数组m[100][100]表示。对称矩阵的所有元素都是非负的,并且相对于这个矩阵的中心是对称的,所以,如果有元素是负数,或者存在相对于中心不对称,即m[i][j]!=M[N-1-i][N- 1-j],则给出的方阵不是对称的。

代码

#include <cstdio>
long long m[100][100];
int main() {
    
    
	int T, i, j, t, n, p;
	char ch;
	while(~scanf("%d", &T))
	for(p=1;p<=T;p++) {
    
    
		ch=getchar();
		t=1;
		scanf("N = %d", &n);
		for(i=0;i<n;i++) {
    
    
			for(j=0;j<n;j++) {
    
    
				scanf("%lld", &m[i][j]);
			}
		}
		for(i=0;i<n;i++) {
    
    
			for(j=0;j<n;j++) {
    
    
				if(m[i][j]<0||m[i][j]!=m[n-1-i][n-1-j]) {
    
    
					t=0;
					break;
				}
			}
			if(!t) break;
		}
		printf("Test #%d: ", p);
		if(t) 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

解析

在这里插入图片描述
由这一局部解递推出规律:对于一个nn方阵,只要它的所有的(n-1)(n-1)子方阵是homogeneous的,则该n×n方阵是homogeneous的;进一步递推可得,只要该nn方阵的所有的22的子方阵符合两对角线相加相等,该该n*n方阵是homogeneous
的。

代码

#include <cstdio>
int a[1008][1008];
int main() {
    
    
	int n, i, j, t;
	while(~scanf("%d", &n)&&n) {
    
    
		t=1;
		for(i=1;i<=n;i++) {
    
    
			for(j=1;j<=n;j++) {
    
    
				scanf("%lld", &a[i][j]);
			}
		}
		for(i=1;i<n;i++) {
    
    
			for(j=1;j<n;j++) {
    
    
				if(a[i][j]+a[i+1][j+1]!=a[i+1][j]+a[i][j+1]) {
    
    
					t=0;
					break;
				}
			}
			if(!t) break;
		}
		if(t) 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 N2 integers separated by whitespace (spaces and newlines). These are the N2 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

解析

在这里插入图片描述
在这里插入图片描述
注意:这里定义INF的含义 仅仅使用小范围数据测试,真正提交上去,判题时,会有大量的测试数据对提交的程序进行检验。所以样例数据通过了,不代表对所有的测试数据都能输出正确的结果。

代码

#include <cstdio>
#include <cstring>
#define INF 0x3f3f3f3f
int a[108][108];
int s[108];
int solve(int a[], int n) {
    
    
	int i, t=-1, m=-INF;
	for(i=0;i<n;i++) {
    
    
		if(t>=0) t+=a[i];
		else t=a[i];
		if(t>m) m=t;
	}
	return m;
}
int main() {
    
    
	int n, i, j, k, ans=-INF, t;
	scanf("%d", &n);
	for(i=0;i<n;i++) {
    
    
		for(j=0;j<n;j++) {
    
    
			scanf("%d", &a[i][j]);
		}
	}
	for(i=0;i<n;i++) {
    
    
		memset(s, 0, sizeof(int)*n); 
		for(j=i;j<n;j++) {
    
    
			for(k=0;k<n;k++) {
    
    
				s[k]+=a[j][k];
			}
			t=solve(s, n);
			if(t>ans) ans=t;
		}
	}
	printf("%d", ans);
	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

Hint
INPUT DETAILS:
Five cows with milk outputs of 1…5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.

解析

递增排序N头奶牛的产奶量,排序后的中间元素即为位于中点的产奶量。
参照选择排序、冒泡排序、插入排序C语言程序段,对输入的产奶量序列进行排序,然后输出中点的产奶量。

代码

#include <cstdio>
int a[10008];
int main() {
    
    
	int n, i, j, t;
	scanf("%d", &n);
	for(i=1;i<=n;i++)
		scanf("%d", a+i);
	for(i=1;i<n;i++) {
    
    
		for(j=1;j<=n-i;j++) {
    
    
			if(a[j]>a[j+1]) {
    
    
				t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
			}
		}
	}
	printf("%d", a[(n+1)/2]);
	return 0;
}

G - Train Swapping

题目

在这里插入图片描述
Sample Input

3
3
1 3 2
4
4 3 2 1
2
2 1

Sample Output

Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.

解析

输入列车的排列次序a[1]‥a[m]后,对a[ ]进行递增排序,在排序过程中数据互换的次数即为问题解。由于m的上限仅为50,因此使用冒泡排序亦满足时效要求。

代码

#include <cstdio>
int main() {
    
    
	int n, t, l, a[58], f, i, j;
	scanf("%d", &n);
	while (n--) {
    
    
		f=0;
		scanf("%d", &l);
		for (i=0;i<l;i++)
			scanf("%d", &a[i]);
		for (i=0;i<l-1; i++)
			for (j=0;j<l-1-i;j++)
				if (a[j]>a[j+1]) {
    
    
					t=a[j];
					a[j]=a[j+1];
					a[j+1]=t;
					f++;
				}
		printf("Optimal train swapping takes %d swaps.\n", f);
	}
	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 sequence DAABEC, 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 sequence ZWQM 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, from most 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 to least 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

解析

“最多已排序”的串指的是串中逆序对数最少的串;而串中逆序对数最多的串就是所谓的“最少已排序”的串。所以设DNA序列为字符串数组s,其中第i个DNA串为s[i];逆序对数为f[i],1≤i≤m。
首先,使用冒泡排序,统计每个DNA串的逆序对数f[i];然后,使用插入排序,按逆序对数递增排序s;最后,输出s[1]‥s[m]。

代码

#include <cstdio>
struct DNA{
    
    
	char a[58];
	int t;
}s[108], p;
char ch;
int main() {
    
    
	int n, m, i, j, f;
	scanf("%d%d", &n, &m);
	for(i=0;i<n-1;i++)
		s[i].t=0;
	ch=getchar();
	for (i=0;i<m;i++) {
    
    
		for(j=0;j<n;j++) {
    
    
			scanf("%c", &s[i].a[j]);
		}
		ch=getchar();
	}
	for(f=0;f<m;f++) {
    
    
		for(i=0;i<n-1;i++) {
    
    
			for(j=i+1;j<n;j++) {
    
    
				if(s[f].a[i]>s[f].a[j]) {
    
    
					s[f].t++;
				}
			}
		}
	}
	for(i=0;i<m-1;i++) {
    
    
		for(j=0;j<m-1-i;j++) {
    
    
			if(s[j].t>s[j+1].t) {
    
    
				p=s[j];
				s[j]=s[j+1];
				s[j+1]=p;
			}
		}
	}
	for (i=0;i<m;i++) {
    
    
		for(j=0;j<n;j++) {
    
    
			printf("%c", s[i].a[j]);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_52328032/article/details/112964706
今日推荐