灵动ICPC冬令营基础-4
A - Necklace
#include<cstdio>
int main(){
double Vt,V0;
while(~scanf("%lf%lf",&Vt,&V0)&&Vt&&V0){
if(V0>=Vt) printf("0\n");
else if(Vt<=2*V0) printf("1\n");
else {
if(Vt/(2*V0)-(int)(Vt/(2*V0))==0.5) printf("0\n");
else if(Vt/(2*V0)-(int)(Vt/(2*V0))<0.5) printf("%d\n",(int)(Vt/(2*V0)));
else printf("%d\n",(int)(Vt/(2*V0)));
}
}
return 0;
}
推公式代入,与整形数据相减求出小数与0.5比较
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(){
int i,n;
double VR,VS,R,C,w;
scanf("%lf%lf%lf%d",&VS,&R,&C,&n);
for(i=0;i<n;i++){
scanf("%lf",&w);
VR=C*R*w*VS/sqrt(1+C*C*R*R*w *w );
printf("%.3lf\n",VR);
}
return 0;
}
推推·推·公式
C - Symmetric Matrix
You‘re given a square matrix M. Elements of this matrix are Mij : {0 < i < n, 0 < j < n}. In this
problem you’ll have to find out whether the given matrix is symmetric or not.
Definition: Symmetric matrix is such a matrix that all elements of it are non-negative and symmetric
with relation to the center of this matrix. Any other matrix is considered to be non-symmetric. For
example:
M =
5 1 3
2 0 2
3 1 5
is symmetric
M =
5 1 3
2 0 2
0 1 5
is not symmetric, because 3 ̸= 0
All you have to do is to find whether the matrix is symmetric or not. Elements of a matrix given
in the input are −2
32 ≤ Mij ≤ 2
32 and 0 < n ≤ 100.
Input
First line of input contains number of test cases T ≤ 300. Then T test cases follow each described in
the following way. The first line of each test case contains n – the dimension of square matrix. Then
n lines follow each of then containing row i. Row contains exactly n elements separated by a space
character. j-th number in row i is the element Mij of matrix you have to process.
Output
For each test case output one line ‘Test #t: S’. Where t is the test number starting from 1. Line S
is equal to ‘Symmetric’ if matrix is symmetric and ‘Non-symmetric’ in any other case.
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.
#include<cstdio>
long long M[1000][1000];
int main(){
int T,t,i,j,N,flag;
while(~scanf("%d",&T)){
for(t=1;t<=T;t++){
getchar();
scanf("N = %d",&N);
flag=1;
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]) {
flag=0;
break;
}
}
if(flag==0) break;
}
if(flag) printf("Test #%d: Symmetric.\n",t);
else printf("Test #%d: Non-symmetric.\n",t);
}
}
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
#include<cstdio>
int a[1008][1008];
int main(){
int n;
while (~scanf("%d",&n)&&n){
int flag=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&a[i][j]);
}
}
for(int i=1;i<n;i++){
for(int j=1;j<n;j++){
if(a[i][j]+a[i+1][j+1]!=a[i][j+1]+a[i+1][j]){
flag=0;
break;
}
}
if(flag==0) break;
}
if(flag) 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
#include<cstdio>
const int s=108;
int a[s][s]={0};
int sum[s]={0},d[s]={0};
int main(){
int N,max=0,ans=0,i,j,k;
scanf("%d",&N);
for( i=1;i<=N;i++){
for( j=1;j<=N;j++){
scanf("%d",&a[i][j]);
a[i][j]=a[i-1][j]+a[i][j];
}
}
for(i=1;i<=N;i++){
for(j=i;j<=N;j++){
for(k=1;k<=N;k++){
sum[k]=a[j][k]-a[i-1][k];
d[k]=d[k-1]>=0?d[k-1]+sum[k]:sum[k];
if(d[k]>max) max=d[k];
}
if(max>ans) ans=max;
}
}
printf("%d",ans);
return 0;
}
读入的时候 就把每一行加上前一行。 然后算第i行到j行 压缩后的序列的最大子列和,求不同压缩的子列和,互相比较求最大子矩阵
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.
#include<cstdio>
int a[10003];
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int main(){
int N;
scanf("%d",&N);
for(int i = 0 ;i < N ; i++) scanf("%d",&a[i]);
for (int i = 0; i < N - 1; i++){
for (int j = 0; j < N - 1 - i; j++){
if (a[j] > a[j + 1]) swap(&a[j], &a[j+1]);
}
}
printf("%d",a[N/2]);
return 0;
}
排序
G - Train Swapping
At an old railway station, you may still encounter one of the last remaining “train swappers”. A train
swapper is an employee of the railroad, whose sole job it is to rearrange the carriages of trains.
Once the carriages are arranged in the optimal order, all the train driver has to do, is drop the
carriages off, one by one, at the stations for which the load is meant.
The title “train swapper” stems from the first person who performed this task, at a station close to
a railway bridge. Instead of opening up vertically, the bridge rotated around a pillar in the center of
the river. After rotating the bridge 90 degrees, boats could pass left or right.
The first train swapper had discovered that the bridge could be operated with at most two carriages
on it. By rotating the bridge 180 degrees, the carriages switched place, allowing him to rearrange the
carriages (as a side effect, the carriages then faced the opposite direction, but train carriages can move
either way, so who cares).
Now that almost all train swappers have died out, the railway company would like to automate
their operation. Part of the program to be developed, is a routine which decides for a given train the
least number of swaps of two adjacent carriages necessary to order the train. Your assignment is to
create that routine.
Input
The input contains on the first line the number of test cases (N). Each test case consists of two input
lines. The first line of a test case contains an integer L, determining the length of the train (0 ≤ L ≤ 50).
The second line of a test case contains a permutation of the numbers 1 through L, indicating the current
order of the carriages. The carriages should be ordered such that carriage 1 comes first, then 2, etc.
with carriage L coming last.
Output
For each test case output the sentence: ‘Optimal train swapping takes S swaps.’ where S is an
integer.
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.
#include<cstdio>
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
int a[60];
int main(){
int N;
scanf("%d",&N);
while(N--){
int L,c=0;
scanf("%d",&L);
for(int i = 0 ;i < L ; i++) scanf("%d",&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]),c++;
}
}
printf("Optimal train swapping takes %d swaps.\n",c);
}
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
#include<cstdio>
struct p{
char a[101];
int c;
}d[101];
void swap(struct p *a, struct p *b){
struct p temp = *a;
*a = *b;
*b = temp;
}
int main(){
int m,n,i,j,k;
scanf("%d%d",&n,&m);
for( i = 0 ; i < m ; i++){
scanf("%s",d[i].a);
}
for( k=0;k<m;k++){
for ( i = 0; i < n-1 ; i++){
for ( j = i+1; j < n ; j++){
if (d[k].a[i] > d[k].a[j]) d[k].c++;
}
}
}
for ( i = 0; i < m - 1; i++){
for ( j = 0; j < m - 1 - i; j++){
if (d[j].c > d[j + 1].c) swap(&d[j], &d[j+1]);
}
}
for(int i = 0 ; i < m ; i++){
printf("%s\n",d[i].a);
}
return 0;
}