>Q;
const int maxn=3000+10;
int n,a[maxn];
struct Node
{
int x,y;
}node[maxn];
int main()
{
int cnt=0;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
int p=i;
for(int j=i+1;j<=n;j++)
{
if(a[j]
題目b
題意:告訴n個男孩,和m個女孩,然後分別告訴他們跳舞的水平,然後男女能夠匹配的條件是他們的水平最多相差1,。
思路:直接對他們的水平進行排列,那麼就可以愉快的貪心了,然後找出每個男孩能夠匹配的水平,然後從小到大選擇,越小越好,因為如果選的稍微大的,可能會影響後面的水平高的男孩的舞伴,所以從小到大貪是正確的。
題目:
B. BerSU Ball
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls
are already busy rehearsing waltz, minuet, polonaise and quadrille moves.
We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.
For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys
and m girls.
Input
The first line contains an integer n (1?≤?n?≤?100)
— the number of boys. The second line contains sequence a1,?a2,?...,?an (1?≤?ai?≤?100),
where ai is
the i-th boy's dancing skill.
Similarly, the third line contains an integer m (1?≤?m?≤?100)
— the number of girls. The fourth line contains sequence b1,?b2,?...,?bm (1?≤?bj?≤?100),
where bj is
the j-th girl's dancing skill.
Output
Print a single number — the required maximum possible number of pairs.
Sample test(s)
input
4
1 4 6 2
5
5 1 5 7 9
output
3
input
4
1 2 3 4
4
10 11 12 13
output
0
input
5
1 1 1 1 1
3
1 2 3
output
2
代碼:
#include
#include
#include
#include
using namespace std;
const int maxn=100+10;
int a[maxn],b[maxn],n,m;
bool vis[maxn];
int main()
{
int ans,l,mid,r;
while(~scanf("%d",&n))
{
ans=0;
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+m);
for(int i=1;i<=n;i++)
{
l=a[i]-1,mid=a[i],r=a[i]+1;
for(int j=1;j<=m;j++)
{
if(!vis[j])
{
if(b[j]==l)
{
ans++;
vis[j]=true;
break;
}
else if(b[j]==mid)
{
ans++;
vis[j]=true;
break;
}
else if(b[j]==r)
{
ans++;
vis[j]=true;
break;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
題目3:
題目,給出m,s,意思就是確定兩個數,他們都有m位,然後每位的和加起來都是s。
思路:構造,最大值比較好求,就是從最高位向最低位選擇,然後最後不滿9的,下一位就是這個數,然後直接跳出來後面的全是0,在構造最小的值,最小的值首先最高位直接賦值為1,然後從最低位每位按9算,最後不足9的直接把最後的值賦給這一位,如果直到最高位還有值,那麼最高位==ss+1,最小值也構造完成。。
ps:還要注意幾個問題,首先是直接輸出-1 -1的,就是s==0或者9*m
題目:
C. Given Length and Sum of Digits...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have a positive integer m and a non-negative integer s.
Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s.
The required numbers should be non-negative integers written in the decimal base without leading zeroes.
Input
The single line of the input contains a pair of integers m, s (1?≤?m?≤?100,?0?≤?s?≤?900)
— the length and the sum of the digits of the required numbers.
Output
In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1
-1" (without the quotes).
Sample test(s)
input
2 15
output
69 96
input
3 0
output
-1 -1
代碼:
#include
#include
#include
#include
#include