7 28 第二次团队赛 致队友
按照ac题的顺序
H. Making Friends
Ali is trying to make his friends happier by matching them into pairs together. In the beginning, Ali has 2 × n friends standing in a row and numbered from 1 to 2 × n. Each friend i will be matched with the friend numbered (2 × n - i + 1).
Each friend i has a happiness level equal to hi. The happiness level of a matched pair of friends i and (2 × n - i + 1) is equal to hi + h2 × n - i + 1.
Your task is to find the maximum happiness level of a pair among all matched pairs. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 50) specifying the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 1000), giving that Ali has 2 × n friends. Then a line follows containing 2 × n integers h1, ..., h2 × n (1 ≤ hi ≤ 1000), in which hi represents the happiness level of the ith friend.
Output
For each test case, print a single line containing the maximum happiness level of a pair among all matched pairs.
Example
Input
1
3
2 4 5 6 7 8
Output
11
题意 找每次i跟2n-i+1配对的最大值
#include<cstdio>
#include <cstring>
#include <cmath>
#include<algorithm>
#include <stdlib.h>
using namespace std;
int main()
{
int T,i,n,a[10100];
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
for(i=1;i<=2*n;i++)
scanf("%d",&a[i]);
int maxx=0;
for(i=1;i<=n;i++)
{
if(a[i]+a[2*n-i+1]>maxx)
maxx=a[i]+a[2*n-i+1];
}
printf("%d\n",maxx);
}
return 0;
}
C. Flip the Bits
You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of n such that m is less than n (m < n) and it is as maximal as possible. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 105) specifying the number of test cases.
Each test case consists of a single line containing one integer n (1 ≤ n ≤ 109), as described in the statement above.
Output
For each test case, print a single line containing the minimum number of bits you need to flip in the binary representation of n to build the number m.
Example
Input
2
5
10
Output
1
2
题意:有一个n 将2进制中每个位翻转 找到一个比n小且最大的 输出翻转了几位 注意翻转指像硬币一样翻转
思路:找到右边开始第一位是1的位置输出就行 例如二进制的1100 变成1011 3位
AC代码
#include<cstdio>
#include <cstring>
#include <cmath>
#include<algorithm>
#include <stdlib.h>
using namespace std;
int main()
{
long long int T,i,n,ans=1;
scanf("%lld",&T);
while(T--)
{
ans=1;
scanf("%lld",&n);
while(1)
{
if(n%2==1)
break;
n/=2;
ans++;
}
printf("%lld\n",ans);
}
return 0;
}
I. Split the Number
You are given an integer x. Your task is to split the number x into exactly n strictly positive integers such that the difference between the largest and smallest integer among them is as minimal as possible. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.
Each test case consists of a single line containing two integers x and n (1 ≤ x ≤ 109, 1 ≤ n ≤ 1000), as described in the statement above.
Output
For each test case, print a single line containing n space-separated integers sorted in a non-decreasing order. If there is no answer, print - 1.
Example
Input
1
5 3
Output
1 2 2
Note
The strictly positive integers are the set defined as: . That is, all the integers that are strictly greater than zero: .
题意:例如样例 7 4 可以把7分成1 3 3 也可以分成2 2 3 答案是后一种 因为题目要保证最大值与最小值差最小 注意输出-1的情况
#include<cstdio>
#include <cstring>
#include <cmath>
#include<algorithm>
#include <stdlib.h>
using namespace std;
int main()
{
long long int T,x,n,a[10100],i;
scanf("%lld",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%lld%lld",&x,&n);
if(x<n)
printf("-1\n");
else
{
long long int p,q;
p=x/n;
q=x%n;
for(i=0; i<n; i++)
a[i]+=p;
long long int d=0;
for(i=n-1; i>=0; i--)
{
if(d==q)
break;
d++;
a[i]+=1;
}
for(i=0; i<n; i++)
{
if(i==n-1)
printf("%lld\n",a[i]);
else
printf("%lld ",a[i]);
}
}
}
return 0;
}
B. Friends and Cookies
Abood's birthday has come, and his n friends are aligned in a single line from 1 to n, waiting for their cookies, Abood has x cookies to give to his friends.
Here is an example to understand how Abood gives away the cookies. Suppose Abood has 4 friends and x cookies, then Abood will do the following:
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 4th friend.
- Give a cookie to the 3rd friend.
- Give a cookie to the 2nd friend.
- Give a cookie to the 1st friend.
- Give a cookie to the 2nd friend.
- And so on until all the x cookies are given away.
Your task is to find how many cookies each friend will get. Can you?
Input
The first line contains an integer T (1 ≤ T ≤ 100) specifying the number of test cases.
Each test case consists of a single line containing two integers x and n (1 ≤ x ≤ 1018, 1 ≤ n ≤ 1000), in which x is the number of cookies Abood has, and n is the number of his friends.
Output
For each test case, print a single line containing n space-separated integers a1, ..., an, in which ai represents how many cookies the ith friend got.
Example
Input
1
5 3
Output
2 2 1
题意:分蛋糕 就像下表一样 输出没人分得的蛋糕数
假设有5个人 10块蛋糕 看下表
A | B | C | D | E |
1 | 2 | 3 | 4 | 5 |
9 | 8 | 7 | 6 | |
10 |
所以最后输出 2 3 2 2 1
。。。。这个题交了7遍 一直都是第三组运行错误 就在最后终于找到了错误 要特判一下n=1的情况
下面是ac代码
#include<cstdio>
#include <cstring>
#include <cmath>
#include<algorithm>
#include <stdlib.h>
using namespace std;
int main()
{
long long int T,m,n,a[101000],i;
scanf("%lld",&T);
while(T--)
{
memset(a,0,sizeof(a));
scanf("%lld%lld",&m,&n);
if(n==1)
{
printf("%lld\n",m);
}
else
if(m<n)
{
for(i=0;i<m;i++)
a[i]=1;
for(i=0;i<n;i++)
{
if(i==n-1)
printf("%lld\n",a[i]);
else
printf("%lld ",a[i]);
}
}
else
{
long long x,y;
for(i=0;i<n;i++)
a[i]=1;
m=m-n;
x=m/(n-1);
y=m%(n-1);
for(i=1;i<n-1;i++)
a[i]+=x;
if(x%2!=0)
{
a[0]+=x/2+1;
a[n-1]+=x/2;
for(i=1;i<=y;i++)
a[i]+=1;
}
else
{
a[0]+=x/2;
a[n-1]+=x/2;
long long int f=0;
for(i=n-2;i>=1;i--)
{
if(f>=y)
break;
a[i]++;
f++;
}
}
for(i=0;i<n;i++)
{
if(i==n-1)
printf("%lld\n",a[i]);
else
printf("%lld ",a[i]);
}
}
}
return 0;
}
这次较上次有进步了 继续努力~~~~