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.
The first line of input contains four space-separated integers r, c, n, k (1 ≤ r, c, n ≤ 10, 1 ≤ 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 ≤ r, 1 ≤ yi ≤ c): the position of the i-th viola. It is guaranteed that no location appears more than once in the input.
Print a single integer — the number of photographs Paul can take which include at least k violas.
2 2 1 1 1 2
4
3 2 3 3 1 1 3 1 2 2
1
3 2 3 2 1 1 3 1 2 2
4
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;
}
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.
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.
Print "YES" (without quotes) if the rearrangement can be done in the existing network, and "NO" otherwise.
3 1 0 2 2 0 1
YES
2 1 0 0 1
YES
4 1 2 3 0 0 3 2 1
NO
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;
}
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)?
The first line of the input contains two integers s and x (2 ≤ s ≤ 1012, 0 ≤ x ≤ 1012), the sum and bitwise xor of the pair of positive integers, respectively.
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
9 5
4
3 3
2
5 2
0
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;
}