B.平均睡眠時間
トピック住所:のhttp://codeforces.com/contest/808/problem/B
長く使用するための単純な配列の圧縮、注意を払う:アイデア
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.ティーパーティー
トピック住所:のhttp://codeforces.com/contest/808/problem/C
アイデア:最初の、一般的には皆を満たし、最後まで倒れ1を満たす最大のボトルダウンでのターンの残りの部分は、ノートがあり、その点、もしボトル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;
}