【概率与期望】【暴力搜索】[Codeforces#621]题解+总结

Wet Shark and Odd and Even

题目描述

Today, Wet Shark is given n integers. Using any of these integers no more than once, Wet Shark wants to get maximum possible even (divisible by 2) sum. Please, calculate this value for Wet Shark.

Note, that if Wet Shark uses no integers from the n integers, the sum is an even integer 0.

样例输入

3
1 2 3

样例输出

6

题目解析

首先要保证尽量的大所有给定的数字均为正数,把所有数字加起来获得最大值,那么我们如果此时得到的是一个奇数那么我们只需要减去一个最小的奇数就可以得到答案

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 100000;
LL Sum, Min=10000000000000LL;
int main(){
    int n;
    LL tmp;
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%I64d", &tmp);
        Sum += tmp;
        if((tmp&1) > 0)
            Min = min(Min, tmp);
    }
    if((Sum&1)>0)
        printf("%I64d\n", Sum - Min);
    else printf("%I64d\n", Sum);

    return 0;
}

Wet Shark and Bishops

题目描述

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

样例输入

5
1 1
1 5
3 3
5 1
5 5

样例输出

6

题目解析

可以发现直接暴力然后求组合数

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 1000;
bool Map[MAXN+10][MAXN+10];
LL C(LL n, LL r)
{
    LL ans = 1;
    for (int i = 1; i <= r; i++)  {
        ans *= (n - i + 1);
        ans /= i;
    }
    return ans;
}
int main(){
    int n, x, y;
    scanf("%d", &n);
    for(int i=1;i<=n;i++){
        scanf("%d%d", &x, &y);
        Map[x][y] = true;
    }
    LL ans = 0, cnt;
    x = 1001, y=1;
    while(--x){
        cnt = 0;
        for(int x1=x, y1=y;x1<=1000&&y1<=1000;x1++, y1++){
            if(Map[x1][y1])
                cnt++;
        }
        if(cnt > 0)
            ans += C(cnt, 2);
    }
    x = 1, y = 1;
    while(++y <= 1000){
        cnt = 0;
        for(int x1=x, y1=y;x1<=1000&&y1<=1000;x1++, y1++){
            if(Map[x1][y1])
                cnt++;
        }
        if(cnt > 0)
            ans += C(cnt, 2);
    }
    x = 0, y = 1;
    while(++x <= 1000){
        cnt = 0;
        for(int x1=x, y1=y;x1&&y1<=1000;x1--, y1++){
            if(Map[x1][y1])
                cnt++;
        }
        if(cnt > 0)
            ans += C(cnt, 2);
    }
    x = 1000, y = 1;
    while(++y <= 1000){
        cnt = 0;
        for(int x1=x, y1=y;x1&&y1<=1000;x1--, y1++){
            if(Map[x1][y1])
                cnt++;
        }
        if(cnt > 0)
            ans += C(cnt, 2);
    }
    printf("%I64d\n", ans);

    return 0;
}

Wet Shark and Flowers

题目描述

There are n sharks who grow flowers for Wet Shark. They are all sitting around the table, such that sharks i and i + 1 are neighbours for all i from 1 to n - 1. Sharks n and 1 are neighbours too.

Each shark will grow some number of flowers si. For i-th shark value si is random integer equiprobably chosen in range from li to ri. Wet Shark has it’s favourite prime number p, and he really likes it! If for any pair of neighbouring sharks i and j the product si·sj is divisible by p, then Wet Shark becomes happy and gives 1000 dollars to each of these sharks.

At the end of the day sharks sum all the money Wet Shark granted to them. Find the expectation of this value.

样例输入

3 2
1 2
420 421
420420 420421

样例输出

4500.0

题目解析

真是英语不好害死人。。。这里要注意几点1.p是个质数 2.任意两个相邻的要么获取2000要么获取0那么对任意一组相邻的,其他不相邻的无论如何改变无法对当前改组的期望造成影响那么我们分成n组然后分别计算期望求和就行了

代码

#include <cstdio>
int L[100010], R[100010], n, p;
double ans;
int main(){
    scanf("%d%d", &n, &p);
    for(int i=1;i<=n;i++)
        scanf("%d%d", &L[i], &R[i]);
    ans += 2000.0 * (1.0-(((R[1]-L[1]+1-R[1]/p+(L[1]-1)/p)/1.0/(R[1]-L[1]+1)) * ((R[n]-L[n]+1-R[n]/p+(L[n]-1)/p)/1.0/(R[n]-L[n]+1))));
    for(int j=1;j<n;j++)
        ans += 2000.0 * (1.0-((R[j]-L[j]+1-R[j]/p+(L[j]-1)/p)/1.0/(R[j]-L[j]+1) * ((R[j+1]-L[j+1]+1-R[j+1]/p+(L[j+1]-1)/p)/1.0/(R[j+1]-L[j+1]+1))));
    printf("%lf\n", ans);

    return 0;
}

你可能感兴趣的:(ACM,codeforces,暴力搜索,概率与期望)