B. Average Sleep Time
题目地址:http://codeforces.com/contest/808/problem/B
思路:简单数组压缩,注意要用long
AC代码:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main {
static long[] num = new long[200010];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
int k = in.nextInt();
num[0] = 0;
long x;
for (int i=1; i<=n; i++) {
x = in.nextLong();
num[i] = num[i-1] + x;
}
long sum = 0;
for (int i=1; i<=n-k+1; i++) {
sum += num[i+k-1] - num[i-1];
}
DecimalFormat df = new DecimalFormat("0.0000000000");
double y = sum * 1.0 / (n - k + 1);
System.out.println(df.format(y));
}
}
}
C. Tea Party
题目地址:http://codeforces.com/contest/808/problem/C
思路:先把每个人的一般倒满,把剩下的依次在最大的瓶子中倒,倒满一个倒下一个,直到倒完,有一个注意点是,如果瓶子i的体积比瓶子j的体积大时,i瓶子放的茶大于等于j瓶子放的茶,如茶总共有8升,杯子为8升 7升,2个杯子的体积都是4升
AC代码;
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct cup
{
int total;
int save;
int index;
}cups[110];
bool cmp(cup a,cup b)
{
return a.total > b.total;
}
bool cmp2(cup a,cup b)
{
return a.index < b.index;
}
int main()
{
int n,w;
while (scanf("%d%d",&n,&w) != EOF)
{
int total = 0;
int temp = 0;
for (int i=0; i<n; i++)
{
scanf("%d",&cups[i].total);
temp = (cups[i].total + 1) >> 1;
total += temp;
cups[i].save = temp;
cups[i].index = i;
}
if (total > w)
{
printf("-1\n");
}
else
{
sort(cups,cups+n,cmp);
w -= total;
for (int i=0; i<n; i++)
{
if (w >= cups[i].total - cups[i].save)
{
//这一句一定要放前面
w -= (cups[i].total - cups[i].save);
cups[i].save = cups[i].total;
}
else if (w < cups[i].total - cups[i].save)
{
cups[i].save += w;
w = 0;
}
if (!w)
{
break;
}
}
sort(cups,cups+n,cmp2);
for (int i=0; i<n; i++)
{
printf("%d ",cups[i].save);
}
printf("\n");
}
}
return 0;
}