Codeforces Round #341 (Div. 2)

http://codeforces.com/contest/621

A. Wet Shark and Odd and Even
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains one integer, n (1 ≤ n ≤ 100 000). The next line contains n space separated integers given to Wet Shark. Each of these integers is in range from 1 to 109, inclusive.

Output

Print the maximum possible even sum that can be obtained if we use some of the given integers.

Sample test(s)
input
3
1 2 3
output
6
input
5
999999999 999999999 999999999 999999999 999999999
output
3999999996
Note

In the first sample, we can simply take all three integers for a total sum of 6.

In the second sample Wet Shark should take any four out of five integers 999 999 999.

题目很简单,判断一下奇数的个数就好了,如果奇数的个数为奇数,那么就找一个最小小的奇数去掉。还有一些细节问题

/* ***********************************************
Author        :
Created Time  :2016/2/1 9:36:28
File Name     :cf341a.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
ll a[100010];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n;
    while(cin>>n){
        int num=0;
        ll Sum=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            if(a[i]&1)num++;
            Sum+=a[i];
        }
        sort(a+1,a+1+n);
        if(n==1&&num==1){
            cout<<0<<endl;continue;
        }
        if(num==n&&num%2==1){
            cout<<Sum-a[1]<<endl;
            continue;
        }
        if(num%2==0){
            cout<<Sum<<endl;continue;
        }
        if(num&1){
            for(int i=1;i<=n;i++){
                if(a[i]&1){
                    cout<<Sum-a[i]<<endl;break;
                }
            }
            continue;
        }
    }
    return 0;
}
View Code
B. Wet Shark and Bishops
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample test(s)
input
5
1 1
1 5
3 3
5 1
5 5
output
6
input
3
1 1
2 3
3 5
output
0
Note

In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5)and (4, 5) do not attack each other because they do not share the same diagonal.

 有点像8皇后那个对角线处理。直接统计主对角线副对角线的棋子数量就好了。

/* ***********************************************
Author        :
Created Time  :2016/2/1 10:00:38
File Name     :cf341b.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int a[200010];
int b[200010];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n;
    while(cin>>n){
        int m1=-INF,m2=-INF;
        int x,y;
        for(int i=1;i<=n;i++){
            cin>>x>>y;
            a[x+y+2000]++;
            if(x+y+2000>m1)m1=x+y+2000;
            b[x-y+2000]++;
            if(x-y+2000>m2)m2=x-y+2000;
        }
        ll sum=0;
        for(int i=0;i<=m1;i++){
            if(a[i]>1){
                int x=a[i];
                sum+=x*(x-1)/2;
            }
        }
        for(int i=0;i<=m2;i++){
            if(b[i]>1){
                int x=b[i];
                sum+=x*(x-1)/2;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
View Code
C. Wet Shark and Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains two space-separated integers n and p (3 ≤ n ≤ 100 000, 2 ≤ p ≤ 109) — the number of sharks and Wet Shark's favourite prime number. It is guaranteed that p is prime.

The i-th of the following n lines contains information about i-th shark — two space-separated integers li and ri (1 ≤ li ≤ ri ≤ 109), the range of flowers shark i can produce. Remember that si is chosen equiprobably among all integers from li to ri, inclusive.

Output

Print a single real number — the expected number of dollars that the sharks receive in total. You answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Sample test(s)
input
3 2
1 2
420 421
420420 420421
output
4500.0
input
3 5
1 4
2 3
11 14
output
0.0
Note

A prime number is a positive integer number that is divisible only by 1 and itself. 1 is not considered to be prime.

Consider the first sample. First shark grows some number of flowers from 1 to 2, second sharks grows from 420 to 421 flowers and third from 420420 to 420421. There are eight cases for the quantities of flowers (s0, s1, s2) each shark grows:

  1. (1, 420, 420420): note that ss1 = 420, ss2 = 176576400, and ss0 = 420420. For each pair, 1000 dollars will be awarded to each shark. Therefore, each shark will be awarded 2000 dollars, for a total of 6000 dollars.
  2. (1, 420, 420421): now, the product ss0 is not divisible by 2. Therefore, sharks s0 and s2 will receive 1000 dollars, while shark s1will receive 2000. The total is 4000.
  3. (1, 421, 420420): total is 4000
  4. (1, 421, 420421): total is 0.
  5. (2, 420, 420420): total is 6000.
  6. (2, 420, 420421): total is 6000.
  7. (2, 421, 420420): total is 6000.
  8. (2, 421, 420421): total is 4000.

The expected value is .

In the second sample, no combination of quantities will garner the sharks any money.

期望问题。算一下l[i]~r[i]有多少个不能被p整出的概率x。再算一下l[i+1]~r[i+1]中不能被p整除的概率y。那么相邻的两个数能被p整除的概率为1-x*y;

/* ***********************************************
Author        :
Created Time  :2016/2/1 10:23:27
File Name     :cf341c.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
    return a>b;
}
int l[maxn],r[maxn];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    //freopen("out.txt","w",stdout);
    int n,p;
    while(cin>>n>>p){
        for(int i=0;i<n;i++){
            cin>>l[i]>>r[i];
        }
        l[n]=l[0];
        r[n]=r[0];
        double sum=0;
        for(int i=0;i<n;i++){
            double x=((r[i]-l[i]+1)-((r[i]-l[i]+1)/p)*1.0)/(r[i]-l[i]+1);
            double y=((r[i+1]-l[i+1]+1)-((r[i+1]-l[i+1]+1)/p)*1.0)/(r[i+1]-l[i+1]+1);
            sum+=(1-x*y)*2000;
        }
        printf("%.10lf\n",sum);
    }
    return 0;
}
View Code

 

posted on 2016-02-01 11:34  Beserious  阅读(316)  评论(0编辑  收藏  举报