CF R340

  A. Elephant
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An elephant decided to visit his friend. It turned out that the elephant's house is located at point0 and his friend's house is located at pointx(x > 0) of the coordinate line. In one step the elephant can move1,2,3,4 or 5 positions forward. Determine, what is the minimum number of steps he need to make in order to get to his friend's house.

Input

The first line of the input contains an integer x (1 ≤ x ≤ 1 000 000) — The coordinate of the friend's house.

Output

Print the minimum number of steps that elephant needs to make to get from point0 to pointx.

Sample test(s)
Input
5
Output
1
Input
12
Output
3
Note

In the first sample the elephant needs to make one step of length 5 to reach the point x.

In the second sample the elephant can get to point x if he moves by3,5 and4. There are other ways to get the optimal answer but the elephant cannot reachx in less than three moves.


这道题只需要5枚举到1,累加。

#include <stdio.h>
int main ( )
{
    int x, ans = 0;
    scanf ( "%d", &x );
    int i = 5;
    while ( i > 0 && x%i )
    {
        ans += x/i;
        x %= i;
        i --;
    }
    ans += x/i;
    printf ( "%d", ans );
    return 0;
}

B. Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would containexactly one nut and any break line goes between two adjacent pieces.

You are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn't.

Please note, that if Bob doesn't make any breaks, all the bar will form one piece and it still has to have exactly one nut.

Input

The first line of the input contains integer n (1 ≤ n ≤ 100) — the number of pieces in the chocolate bar.

The second line contains n integers ai (0 ≤ ai ≤ 1), where0 represents a piece without the nut and1 stands for a piece with the nut.

Output

Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut.

Sample test(s)
Input
3
0 1 0
Output
1
Input
5
1 0 1 0 1
Output
4
Note

In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks.

In the second sample you can break the bar in four ways:

10|10|1

1|010|1

10|1|01

1|01|01

此题要求分块,每块中只有一个,求分法有多少种。

如果两个1中有ai个0,那么累计分法的种数有

(a1+1)(a2+1)...*(ak+1)

最大为2^50,所以用longlong

#include <stdio.h>
#define LL long long
const int maxn = 105;
int a[maxn];
int main ( )
{
    int n;
    LL ans = 0;
    scanf ( "%d", &n );
    for ( int i = 0; i < n; i ++ )
        scanf ( "%d", &a[i] );
    int pos = 0;
    for ( int i = 0; i < n; i ++ )
        if ( a[i] == 1 )
        {
            ans = 1;
            pos = i;
            break ;
        }
    int cnt = 0;
    for ( int i = pos+1; i < n; i ++ )
    {
        if ( a[i] == 0 )
            cnt ++;
        else
        {
            ans *= ( cnt+1 );
            cnt = 0;
        }
    }
    printf ( "%I64d", ans );
    return 0;
}

C. Watering Flowers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A flowerbed has many flowers and two fountains.

You can adjust the water pressure and set any values r1(r1 ≥ 0) andr2(r2 ≥ 0), giving the distances at which the water is spread from the first and second fountain respectively. You have to set suchr1 andr2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceedr1, or the distance to the second fountain doesn't exceedr2. It's OK if some flowers are watered by both fountains.

You need to decrease the amount of water you need, that is set such r1 and r2 that all the flowers are watered and ther12 + r22 is minimum possible. Find this minimum value.

Input

The first line of the input contains integers n,x1,y1,x2,y2 (1 ≤ n ≤ 2000, - 107 ≤ x1, y1, x2, y2 ≤ 107) — the number of flowers, the coordinates of the first and the second fountain.

Next follow n lines. The i-th of these lines contains integers xi andyi ( - 107 ≤ xi, yi ≤ 107) — the coordinates of the i-th flower.

It is guaranteed that all n + 2 points in the input are distinct.

Output

Print the minimum possible value r12 + r22. Note, that in this problem optimal answer is always integer.

Sample test(s)
Input
2 -1 0 5 3
0 2
5 2
Output
6
Input
4 0 0 5 0
9 4
8 3
-1 0
1 4
Output
33
Note

The first sample is (r12 = 5,r22 = 1): The second sample is (r12 = 1,r22 = 32):

这道题求的是两个圆心为x1,y1,x2,y2,求r1*r1+r2*r2(都为圆的半径)最小,

并且覆盖所有点。

实际上算出所有点距两个圆心的距离,对其中一个值排序枚举,

不过需要注意的是有一个可以半径为0,所以枚举一次0.

排序枚举:

#include <stdio.h>
#include <algorithm>
#define LL long long
const int maxn = 2005;
struct node
{
    LL d1, d2;
    friend bool operator < ( node n1, node n2 )
    {
        return n1.d1 < n2.d1;
    }
} a[maxn];
LL distance ( LL x1, LL y1, LL x2, LL y2 )
{
    return ( LL )( x1-x2 )*( x1-x2 )+( y1-y2 )*( y1-y2 );
}
inline LL Min ( LL a, LL b ) { return a < b ? a : b; }
inline LL Max ( LL a, LL b ) { return a > b ? a : b; }
int main ( )
{
    int n;
    LL x, y, x1, y1, x2, y2;;
    scanf ( "%d%I64d%I64d%I64d%I64d", &n, &x1, &y1, &x2, &y2 );
    for ( int i = 1; i <= n; i ++ )
    {
        scanf ( "%I64d%I64d", &x, &y );
        a[i].d1 = distance ( x, y, x1, y1 );
        a[i].d2 = distance ( x, y, x2, y2 );
    }
    a[0].d1 = 0;
    std :: sort ( a+1, a+1+n ); //对d1排序
    LL ans = 1e18;
    for ( int i = 0; i <= n; i ++ ) //需要考虑不使用r1的情况
    {
        LL v1 = a[i].d1, v2;    //取d1,然后找d2的最大值
        v2 = 0;
        for ( int j = i+1; j <= n; j ++ )
            v2 = Max ( v2, a[j].d2 );
        ans = Min ( ans, v1+v2 );
    }
    printf ( "%I64d", ans );
    return 0;
}

暴力枚举也可以,不过需要枚举两次,一次为r1,另一次r2,还是只取一边或只有一组的情况。

#include <stdio.h>
#define LL long long
const int maxn = 2005;
struct node
{
    LL x, y;
}a[maxn];
LL pr ( int x ) { return ( LL ) x*x; }
LL distance ( int x1, int y1, int x2, int y2 )
{
    return pr ( x2-x1 ) + pr ( y2-y1 );
}
LL Min ( LL a, LL b ) { return a < b ? a : b; }
LL Max ( LL a, LL b ) { return a > b ? a : b; }
int main ( )
{
    int n, x1, x2, y1, y2, x, y;
    LL ans = 1e18;
    scanf ( "%d%d%d%d%d", &n, &x1, &y1, &x2, &y2 );
    for ( int i = 1; i <= n; i ++ )
    {
        scanf ( "%d%d", &x, &y );
        a[i].x = distance ( x, y, x1, y1 );
        a[i].y = distance ( x, y, x2, y2 );
    }
    //取r1的最大值或取r2的最大值 只有一组数据两个中一个最优值
    for ( int i = 1; i <= n; i ++ ) //对r1枚举
    {
        LL v = 0;
        for ( int j = 1; j <= n; j ++ )
        {
            if ( a[i].x >= a[j].x )
                continue ;
            v = Max ( v, a[j].y );
        }
        ans = Min ( ans, v+a[i].x );
    }
    for ( int i = 1; i <= n; i ++ ) //对r2枚举
    {
        LL v = 0;
        for ( int j = 1; j <= n; j ++ )
        {
            if ( a[i].y >= a[j].y )
                continue ;
            v = Max ( v, a[j].x );
        }
        ans = Min ( ans, v+a[i].y );
    }
    printf ( "%I64d", ans );
    return 0;
}



D. Polyline
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

There are three points marked on the coordinate plane. The goal is to make a simple polyline, without self-intersections and self-touches, such that it passes through all these points. Also, the polyline must consist of only segments parallel to the coordinate axes. You are to find the minimum number of segments this polyline may consist of.

Input

Each of the three lines of the input contains two integers. The i-th line contains integers xi andyi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th point. It is guaranteed that all points are distinct.

Output

Print a single number — the minimum possible number of segments of the polyline.

Sample test(s)
Input
1 -1
1 1
1 2
Output
1
Input
-1 -1
-1 3
4 3
Output
2
Input
1 1
2 3
3 2
Output
3
Note

The variant of the polyline in the first sample: The variant of the polyline in the second sample: The variant of the polyline in the third sample:

这题需要注意,如果两个两个点连一条直线,而另一个点在

它们的中间,那么是需要三条才能连起来的,

因为题目要求直接与直接相连必须是端点相接。

所以只有两个点在一条直线上的情况需要考虑一下。

#include <stdio.h>
int x[3], y[3];
void swap ( int & a, int & b )
{
    int t = a;
    a = b;
    b = t;
}
int inside ( int x, int a, int b )
{
    if ( a > b )
        swap ( a, b );
    return x > a && x < b;
}
int main ( )
{   //注意连线要求连接端点
    for ( int i = 0; i < 3; i ++ )
        scanf ( "%d%d", &x[i], &y[i] );
    if ( x[0] == x[1] && x[1] == x[2] || y[0] == y[1] && y[1] == y[2] )
    //三点一线
        printf ( "1" );
    else if ( x[0] == x[1] || x[0] == x[2] ||
             x[1] == x[2] || y[0] == y[1] ||
             y[0] == y[2] || y[1] == y[2] )
    {
        //有两个相同时,如果另一个在两点的中间就需要三条线
        if ( x[0] == x[1] )
        {
            if ( inside ( y[2], y[0], y[1] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
        else if ( x[1] == x[2] )
        {
            if ( inside ( y[0], y[1], y[2] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
        else if ( x[0] == x[2] )
        {
            if ( inside ( y[1], y[0], y[2] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
        else if ( y[0] == y[1] )
        {
            if ( inside ( x[2], x[0], x[1] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
        else if ( y[1] == y[2] )
        {
            if ( inside ( x[0], x[1], x[2] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
        else if ( y[0] == y[2] )
        {
            if ( inside ( x[1], x[0], x[2] ) )
                printf ( "3" );
            else
                printf ( "2" );
        }
    }
    else    //三个不在同一直线要3条线连接
        printf ( "3" );
    return 0;
}

E. XOR and Favorite Number
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

Input

The first line of the input contains integers nm and k (1 ≤ n, m ≤ 100 0000 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.

The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.

Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.

Output

Print m lines, answer the queries in the order they appear in the input.

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

In the first sample the suitable pairs of i and j for the first query are: (12), (14), (15), (23), (36), (56), (66). Not a single of these pairs is suitable for the second query.

In the second sample xor equals 1 for all subarrays of an odd length.


这道题是莫队算法,本人对此算法处于似懂非懂的状态,
此算法采用分块的形式,将每部分分成sqrt(n),然后查询的
的位置先进行块的排序然后再对右边界排序,不过左边界注意
需要减1,因为是可以从a[l]^...a[r],不减1就表示的a[l+1]^...a[r].
s[L-1]^s[R]==K,或者说是K^s[R]==s[L-1]
#include <stdio.h>
#include <algorithm>
#include <math.h>
#define LL long long
const int maxn = 100005, N = 2000005;
int a[maxn], blo;
LL ans[maxn], s[N];
struct node
{
    int l, r, block;
    int id;
    void init ( int l, int r, LL ans, int id )
    {
        this -> l = l;
        this -> r = r;
        this -> id = id;
        this -> block = ( l-1 )/blo+1;
    }
    friend bool operator < ( node n1, node n2 )
    {
        if ( n1.block != n2.block ) //先对块排序,在对右边界排序
            return n1.block < n2.block;
        return n1.r < n2.r;
    }
} q[maxn];
int read ( )
{
    char ch;
    int sign = 1, ret = 0;
    while ( ( ch = getchar ( ) ) && ! ( ch >= '0' && ch <= '9' || ch == '-' ) )
        if ( ch == '-' )
            sign = -1;
    ret = ch-'0';
    while ( ( ch = getchar ( ) ) && ch >= '0' && ch <= '9' )
        ret = ret*10+( ch-'0' );
    return ret*sign;
}
int main ( )
{
    int n, m, k;
    n = read ( );
    m = read ( );
    k = read ( );
    blo = ( int )sqrt ( n );
    for ( int i = 1; i <= n; i ++ )
        a[i] = a[i-1]^read ( );
    for ( int i = 1; i <= m; i ++ )
    {
        int l = read ( ), r = read ( );
        q[i].init ( l-1, r, 0, i );
        //a[l]^...^a[r]
    }
    std :: sort ( q+1, q+1+m );
    int l = 1, r = 0;
    LL ret = 0;
    for ( int i = 1; i <= m; i ++ )
    {
        while ( l < q[i].l )
        {
            s[ a[l] ] --;
            ret -= s[ k^a[l] ];
            l ++;
        }
        while ( l > q[i].l )
        {
            l --;
            ret += s[ k^a[l] ];
            s[ a[l] ] ++;
        }
        while ( r < q[i].r )
        {
            r ++;
            ret += s[ k^a[r] ];
            s[ a[r] ] ++;
        }
        while ( r > q[i].r )
        {
            s[ a[r] ] --;
            ret -= s[ k^a[r] ];
            r --;
        }
        ans[ q[i].id ] = ret;
    }
    for ( int i = 1; i <= m; i ++ )
        printf ( "%I64d\n", ans[i] );
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值