8VC Venture Cup 2016 - Final Round (Div. 2 Edition)

本文介绍了三道信息技术竞赛中的题目,涉及矩阵中的中提琴手计数、岛屿上雕像的重新排列以及两个正整数的和与异或值。通过解决这些问题,展示了如何运用数学方法来解决实际的编程挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

芝麻开门

A. Orchestra
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Paul is at the orchestra. The string section is arranged in an r × c rectangular grid and is filled with violinists with the exception of n violists. Paul really likes violas, so he would like to take a picture including at least k of them. Paul can take a picture of any axis-parallel rectangle in the orchestra. Count the number of possible pictures that Paul can take.

Two pictures are considered to be different if the coordinates of corresponding rectangles are different.

Input

The first line of input contains four space-separated integers rcnk (1 ≤ r, c, n ≤ 101 ≤ k ≤ n) — the number of rows and columns of the string section, the total number of violas, and the minimum number of violas Paul would like in his photograph, respectively.

The next n lines each contain two integers xi and yi (1 ≤ xi ≤ r1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.

Output

Print a single integer — the number of photographs Paul can take which include at least k violas.

Examples
input
2 2 1 1
1 2
output
4
input
3 2 3 3
1 1
3 1
2 2
output
1
input
3 2 3 2
1 1
3 1
2 2
output
4
Note

We will use '*' to denote violinists and '#' to denote violists.

In the first sample, the orchestra looks as follows

*#
**
Paul can take a photograph of just the viola, the  1 × 2 column containing the viola, the  2 × 1 row containing the viola, or the entire string section, for  4 pictures total.

In the second sample, the orchestra looks as follows

#*
*#
#*
Paul must take a photograph of the entire section.

In the third sample, the orchestra looks the same as in the second sample.


题意:给出一个r*c的矩阵,找出至少包含k个中提琴手的矩形的个数。

题解:暴力计算出每个矩形中 中提琴手 的个数。复杂度O(n^6).

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>
using namespace std;

#define N 2010
#define inf 99999999

#define fr(i,a,b) for(int i=a; i<=b; i++)
int a[12][12];
int xx[12],yy[12];
int main()
{
    int r,c,n,k,x,y,ans,cnt;
    while(~scanf("%d%d%d%d",&r,&c,&n,&k))
    {
        ans=0;
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            a[x][y]=1;
        }
        fr(i,1,r) fr(j,1,c) fr(i2,i,r) fr(j2,j,c)
        {
            cnt=0;
            fr(i3,i,i2) fr(j3,j,j2)
            {
                cnt+=a[i3][j3];
            }
            if(cnt>=k)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


芝麻开门

B. Island Puzzle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A remote island chain contains n islands, labeled 1 through n. Bidirectional bridges connect the islands to form a simple cycle — a bridge connects islands 1 and 2, islands 2 and 3, and so on, and additionally a bridge connects islands n and 1. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.

The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: First, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.

Determine if it is possible for the islanders to arrange the statues in the desired order.

Input

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.

The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently placed on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.

The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the ith island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.

Output

Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.

Examples
input
3
1 0 2
2 0 1
output
YES
input
2
1 0
0 1
output
YES
input
4
1 2 3 0
0 3 2 1
output
NO
Note

In the first sample, the islanders can first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3.

In the second sample, the islanders can simply move statue 1 from island 1 to island 2.

In the third sample, no sequence of movements results in the desired position.


题意:给出A排列,排列可循环移动一位,1--->2,2--->3·····,n--->1.如果能移动成和B排列顺序相同,则输出YES,否则输出NO。

题解:0在任何位置对排列顺序没有影响,所以把0放在最前面,然后再在排列中任选一个数移到第二位。A,B排列都进行此操作。然后比较,顺序相同则YES。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>
using namespace std;

#define N 200010
#define inf 99999999

#define fr(i,a,b) for(int i=a; i<b; i++)
int a[N],b[N],s[N];
int n;

void fun(int x[])
{
    fr(i,0,n)
    {
        if(x[i]==0)
        {
            int k=0;
            fr(j,i,n)
            {
                s[k++]=x[j];
            }
            fr(j,0,i)
            {
                s[k++]=x[j];
            }
        }
    }
    fr(i,0,n) {x[i]=s[i];}
    fr(i,0,n)
    {
        if(x[i]==1)
        {
            int k=0;
            s[k++]=0;
            fr(j,i,n)
            {
                s[k++]=x[j];
            }
            fr(j,1,i)
            {
                s[k++]=x[j];
            }
        }
    }
    fr(i,0,n) {x[i]=s[i];}
}
int main()
{
    while(~scanf("%d",&n))
    {
        int fg=1;
        fr(i,0,n) {scanf("%d",&a[i]);}
        fr(i,0,n) {scanf("%d",&b[i]);}
        fun(a);
        fun(b);
        fr(i,0,n)
        {
            if(a[i]!=b[i])
            {
                fg=0;
                break;
            }
        }
        if(fg)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}



芝麻开门

C. XOR Equation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?

Input

The first line of the input contains two integers s and x (2 ≤ s ≤ 10120 ≤ x ≤ 1012), the sum and bitwise xor of the pair of positive integers, respectively.

Output

Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.

Examples
input
9 5
output
4
input
3 3
output
2
input
5 2
output
0
Note

In the first sample, we have the following solutions: (2, 7)(3, 6)(6, 3)(7, 2).

In the second sample, the only solutions are (1, 2) and (2, 1).


题意:s是a,b之和,x是a,b异或的值。求有多少个a,b对。(a,b为有序队列)

题解:a+b可分解为a^b[个位相加]和(a&b)*2[进位],即a+b=a^b+(a&b)*2.然后进行2进制枚举。设s=(s-x)/2(即s=a&b).当x(即为a^b)=1时,s必为0,此时a,b有两种取值情况;当x=0时,s的取值决定了最终的a,b取值.具体看代码。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<string>
using namespace std;

#define LL __int64
#define inf 99999999

#define fr(i,a,b) for(int i=a; i<=b; i++)
#define sc(n) scanf("%d",&n)
#define sc2(n,m) scanf("%I64d%I64d",&n,&m)
#define sc3(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pf(n) printf("%I64d\n",n);

LL s,x;

int main()
{
        sc2(s,x);
        LL ans=0;
        LL one=1;
        s=s-x;
        if(s%2==1)
        {
            printf("0\n");
            return 0;
        }
        s/=2;
        LL a=s;
        LL b=x;
        LL c,d;
        fr(i,0,64)            //枚举所有
        { 
            c=a&1;            //判断奇偶
            d=b&1;
           if(c==1&&d==1)    
           {
              printf("0\n");
              return 0;
           }
           a>>=1;
           b>>=1;
           ans+=d;             //x中1的个数N, 
        }
        if(s!=0)
            pf(one<<ans);      //最终结果为2的N次方
        if(s==0)
            pf((one<<ans)-2);  //题目要求a,b为正整数,(0,x)和(x.0)两组舍去。
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值