An exam for n students will take place in a long and narrow room, so the students will sit in a line in some order. The teacher suspects that students with adjacent numbers (i and i + 1) always studied side by side and became friends and if they take an exam sitting next to each other, they will help each other for sure.
Your task is to choose the maximum number of students and make such an arrangement of students in the room that no two students with adjacent numbers sit side by side.
A single line contains integer n (1 ≤ n ≤ 5000) — the number of students at an exam.
In the first line print integer k — the maximum number of students who can be seated so that no two students with adjacent numbers sit next to each other.
In the second line print k distinct integers a1, a2, ..., ak (1 ≤ ai ≤ n), where ai is the number of the student on the i-th position. The students on adjacent positions mustn't have adjacent numbers. Formally, the following should be true: |ai - ai + 1| ≠ 1 for all i from 1 tok - 1.
If there are several possible answers, output any of them.
6
6 1 5 3 6 2 4
3
2 1 3
这道题的大致意思是:
给你一个数n,然后标号为从1到n,要你输出最多有k个人有相邻的序号不坐在一起,并且要你判断最多有几种可能,并把其中的一种情况输出。
我的想法是把奇数从大到小保存在一个数组中,偶数也一样; 然后先输出奇数,再输出偶数就好了;
#include<stdio.h>
#include<string.h>
int a[5555],b[5555];
int main(){
int i,j,k,n;
scanf("%d",&n);
for(i=1;i<=n;i++) a[i]=i;
k=1;
if(n==1||n==2) {printf("1\n"); printf("1\n");}
else if(n==3){
printf("2\n");
printf("1 3\n");
}
else if(n>=4){
for(i=n;i>=1;i--){
if(a[i]%2) b[k++]=a[i];
}
for(i=n;i>=1;i--){
if(a[i]%2==0) b[k++]=a[i];
}
printf("%d\n",k-1);
for(i=1;i<k;i++){
if(i!=k-1) printf("%d ",b[i]);
else printf("%d\n",b[i]);
}
}
}
The on-board computer on Polycarp's car measured that the car speed at the beginning of some section of the path equals v1 meters per second, and in the end it is v2 meters per second. We know that this section of the route took exactly t seconds to pass.
Assuming that at each of the seconds the speed is constant, and between seconds the speed can change at most by d meters per second in absolute value (i.e., the difference in the speed of any two adjacent seconds does not exceed d in absolute value), find the maximum possible length of the path section in meters.
The first line contains two integers v1 and v2 (1 ≤ v1, v2 ≤ 100) — the speeds in meters per second at the beginning of the segment and at the end of the segment, respectively.
The second line contains two integers t (2 ≤ t ≤ 100) — the time when the car moves along the segment in seconds, d (0 ≤ d ≤ 10) — the maximum value of the speed change between adjacent seconds.
It is guaranteed that there is a way to complete the segment so that:
- the speed in the first second equals v1,
- the speed in the last second equals v2,
- the absolute value of difference of speeds between any two adjacent seconds doesn't exceed d.
Print the maximum possible length of the path segment in meters.
5 6 4 2
26
10 10 10 0
100
In the first sample the sequence of speeds of Polycarpus' car can look as follows: 5, 7, 8, 6. Thus, the total path is 5 + 7 + 8 + 6 = 26meters.
In the second sample, as d = 0, the car covers the whole segment at constant speed v = 10. In t = 10 seconds it covers the distance of 100 meters.
这道题的大致意思是:
数学题;
首先给出两个数字代表的是v1,v2,分别是起始速度与末速度,然后第二行给出两个数字分别代表的是汽车总共要开的时间t与加速度a。
最后要你求的是在满足时间到达t的时候汽车最多能开的路程。
一开始我想的很复杂,但是后来看了别人的想法后思路就开阔了。
这里要注意,题目中所给出的都是一秒一秒的,所以直接把每秒的速度加起来就好了。我一开始还在推公式,醉了。所以,即转换为求每秒的最大速度并且同时满足在ts时要为v2的速度。
#include<stdio.h>
#include<string.h>
#include<math.h>
int min1(int a,int b){
return a<b?a:b;
}
int main(){
int a1,a2,x;
int t,i,j,k,d,v1,v2,t1,t2,tx;
scanf("%d%d",&v1,&v2);
scanf("%d%d",&t,&a1);
int a[111]={0},b[111]={0};
//从前面往后面加a1,即为a每秒的最大速度。
a[1]=v1;
for(i=2;i<=t;i++){
a[i]+=a[i-1]+a1;
}
//对于v2,从后面往前面加,即为b的每秒的最大速度。
b[t]=v2;
for(i=t-1;i>=1;i--){
b[i]+=b[i+1]+a1;
}
//这里注意sum每次加上的都是a,b中的较小速度;
int sum=0;
for(i=1;i<=t;i++){
sum+=min1(a[i],b[i]);
}
printf("%d\n",sum);
}
Polycarp has n dice d1, d2, ..., dn. The i-th dice shows numbers from 1 to di. Polycarp rolled all the dice and the sum of numbers they showed is A. Agrippina didn't see which dice showed what number, she knows only the sum A and the values d1, d2, ..., dn. However, she finds it enough to make a series of statements of the following type: dice i couldn't show number r. For example, if Polycarp had two six-faced dice and the total sum is A = 11, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible).
For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is A.
The first line contains two integers n, A (1 ≤ n ≤ 2·105, n ≤ A ≤ s) — the number of dice and the sum of shown values where s = d1 + d2 + ... + dn.
The second line contains n integers d1, d2, ..., dn (1 ≤ di ≤ 106), where di is the maximum value that the i-th dice can show.
Print n integers b1, b2, ..., bn, where bi is the number of values for which it is guaranteed that the i-th dice couldn't show them.
2 8 4 4
3 3
1 3 5
4
2 3 2 3
0 1
In the first sample from the statement A equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3.
In the second sample from the statement A equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5.
In the third sample from the statement A equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3.
这道题目的大致意思是:
就是给你n个骰子,然后给你一个和A,然后第二行给你n个数,分别代表骰子能够取到的最大的数字,然后叫你输出n个数,分别代表第i个骰子不能取到的数有几个。
这也是一道数学题,想法是分别求出每个骰子的能够取到的取值区间,然后用总的减去能够取到的就好了。
第i个数的上限:
min(a[i],A-(n-1)), //意思是:上限要不就是那个数字本身,要不就是A-其余数字全部取1的情况。
下限:
max(A-(sum-a[i]),1); //意思是:下限是全部为别人的最大值与1中的最大值;
sum为所有数字的和
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll d[222222]={0},s[222222]={0};
ll cmax(ll a,ll b){
return a>b?a:b;
}
ll cmin(ll a,ll b){
return a<b?a:b;
}
int main(){
ll n,i,j,k,a;
scanf("%lld%lld",&n,&a);
ll sum=0;
for(i=1;i<=n;i++){
scanf("%lld",&d[i]);
sum+=d[i];
}
ll max1=-1,min1=99999999,ans=0;
for(i=1;i<=n;i++){
min1=cmin(d[i],a-(n-1));
max1=cmax(a-(sum-d[i]),1);
ll temp=min1-max1+1;
ans=d[i]-temp;
if(i!=n) printf("%lld ",ans);
else printf("%lld\n",ans);
}
}
虽说有很多题与想法有关,但是慢慢想总会进步的,质变!加油!