1601: New Sort II
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 78 Solved: 47
[Submit][Status]
Description
给定一个数列,请对这个数列重新排序,得到数列a[0],a[1]…a[n-1].
使得a[0]<=a[n-1]<=a[1]<=a[n-2]<=a[3]…
Input
对于每组数据,第一行首先输入一个n(0<n<=100000),第二行是数列的n个元素
输入的所有数字均在int的范围内。
Output
对每组数据中的n个元素重新排序后输出。
要求每相邻两个数字之间用空格隔开。
每两组相邻数据之间输出一个空行。
Sample Input
5
1 2 3 4 5
3
3 2 1
Sample Output
1 3 5 4 2
1 3 2
//
// Created by DELL on 2020/4/9.
//
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Maxn 100005
using namespace std;
int a[Maxn],b[Maxn];
int main(int argc,char* argv[]) {
int n,opt = 0;
while(scanf("%d",&n) == 1) {
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
sort(a + 1,a + n + 1);
if(opt) printf("\n");
opt = 1;
//if(n == 1) { printf("%d\n",a[1]); }
int p = 1,q = n, pos = 1;
for(int i=1; i<=(n>>1); i++) {
b[p++] = a[pos++];
b[q--] = a[pos++];
}
if(n & 1) b[n / 2 + 1] = a[pos];
for(int i=1; i<n; i++) printf("%d ",b[i]);
printf("%d\n",b[n]);
}
return 0;
}
1583: Cat Food and JavaBeans
时间限制: 1 Sec 内存限制: 128 MB
提交: 22 解决: 5
[提交][状态]
题目描述
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
输入
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.
输出
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
样例输入
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
样例输出
13.333
31.500
本题说来非常奇怪 和AC代码对拍不下两千组数据,找不到错在哪里,但是就是WA掉50%,还请路过大佬指点江山
// 我的代码
// Created by DELL on 2020/4/9.
//
#include <stdio.h>
#include <algorithm>
#include <cstring>
#define Maxn 2005
using namespace std;
struct AAD{
int Javebean,Fatmouse;
double aver;
}w[Maxn];
inline bool cmp(AAD a,AAD b) { return a.aver > b.aver; }
int main(int argc,char* argv[]) {
freopen("1.in","r",stdin);
freopen("2.out","w",stdout);
int M,N; double Ans = 0;
while(scanf("%d %d",&M,&N) == 2) {
if(M == -1 && N == -1) break;
Ans = 0;
for(int i=1; i<=N; i++) {
scanf("%d %d",&w[i].Javebean,&w[i].Fatmouse);
w[i].aver = (double)w[i].Javebean * 1.0 / w[i].Fatmouse;
}
sort(w + 1, w + N + 1,cmp);
for(int i=1; i<=N; i++) {
if(M >= w[i].Fatmouse) {
Ans += w[i].Javebean;
M -= w[i].Fatmouse;
}
else {
Ans += M * 1.0 * w[i].aver;
M = 0;
break;
}
if(M == 0) break;
}
printf("%.3lf\n",Ans);
}
}
// 网上找的AC代码
#include<stdio.h>
#include<algorithm>
using namespace std;
struct f
{
double c;
int nai,cat;
};
bool pai(f x,f y)
{
if(x.c>y.c)
return true;
else
return false;
}
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
int m,zong,j=0,n,b;
f a[1005];
while(~scanf("%d%d",&zong,&m))
{
double r=0;
if(zong==-1&&m==-1)
{
break;
}
for(int i=0;i<m;i++)
{
scanf("%d%d",&a[i].nai,&a[i].cat);
a[i].c=a[i].nai*1.0/a[i].cat;
}
sort(a,a+m,pai);
for(int i=0;i<m;i++)
{
if(zong>a[i].cat||zong==a[i].cat)
{
r=a[i].nai+r;
zong=zong-a[i].cat;
}
else
{
r=zong*1.0*a[i].c+r;
zong=0;
if(zong==0)
{
break;
}
}
if(zong==0)
{
break;
}
}
printf("%.3lf\n",r);
}
return 0;
}