冬季集训1.21

冬季集训1.21

A - Necklace

在这里插入图片描述

属于公式推导题

求圆环数量使得项链最长,n = vt/(2*v0),要注意的是有两个解时输出0,所以要判断n和n向下取整的差与0.5的关系

完整代码:

#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

题目大意:根据题目所给的式子,找出关系

在这里插入图片描述

完整代码:

#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

在这里插入图片描述

题目大意:给出一个方阵,判断矩阵内元素是否满足中心对称,且要求矩阵内元素非负,其他任何矩阵都是非对称的

思路:观察矩阵下标,非中心对称矩阵即满足M (i,j)!=M(n-1-i,n-1-j)

完整代码:

#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

题目大意:判断n个独立(位置不同,x1!=x2,y1!=y2)位置上的数的和是否相等,相等输出“homogeneous”,否则输出“not homogeneous”

思路:找一个简单的2阶及以上矩阵递推得到关系,判断n*n阶矩阵可以变成判断n个2 * 2阶矩阵是否满足题意

完整代码:

#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

题目大意:由求一维数组最大子序列和演化而来,

给出一个由正整数和负整数组成的二维数组: :一个子矩形是指位于整个数组中大小为1*1或更大的任何连续子数组。矩形的和是该矩形中所有元素的和。在本题中,具有最大和的子矩形被称为最大子矩形。

思路:将二维数组转换成一维数组求其最大子序列和

主要代码:

	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;
}

完整代码:

#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

题目大意及其思路:给出一组数值(奇数个),找其中值。排序之后取中间值

主要代码:

#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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(im在这里插入图片描述
g-1q9NDgcH-1611306743498)(C:\Users\29252\Pictures\题目\G.png)]

题目大意及其思路:给出多组测试样例,每组数据有序排列共需交换几次。简单冒泡排序累计交换次数得出答案

完整代码:

#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

题目大意:

在一个字符串中逆序数是在该串中与次序相反的字符对的数目。例如,字母序列“DAABEC”的逆序数是5,因为D比它右边的4个字母大,而E比它右边的1个字母大。序列“AACEDGG”的逆序数是1(E和 D),几乎已经排好序了而序列“ZWQM”的逆序数是6,完全没有排好序。

•您要对DNA字符串序列进行分类(序列仅包含4个字母A,C,G和T)。然而,分类不是按字母顺序,而是按“排序”的次序,从“最多已排序”到“最少已排序”进行排列。所有的字符串长度相同

思路:• “最多已排序”的串指的是串中逆序对数最少的串;而串中逆序对数最多的串就是所谓的“最少已排序”的串。所以设DNA序列为字符串数组s,其中第i个DNA串为s[i];逆序对数为cnt。 首先,j将字符串和逆序对数cnt放入结构体,使用冒泡排序,统计每个DNA串的逆序对数,再由逆序对数由少到多排序,输出字符串

主要代码:

#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;
}

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值