Determine the Photo Position
题目描述
You have taken the graduation picture of graduates. The picture could be regarded as a matrix A of n × n, each element in A is 0 or 1, representing a blank background or a student, respectively.
However, teachers are too busy to take photos with students and only took a group photo themselves. The photo could be regarded as a matrix B of 1 × m where each element is 2 representing a teacher.
As a master of photoshop, your job is to put photo B into photo A with the following constraints:
- you are not allowed to split, rotate or scale the picture, but only translation.
- each element in matrix B should overlap with an element in A completely, and each teacher should overlap with a blank background, not shelter from a student.
Please calculate the possible ways that you can put photo B into photo A.
输入描述:
The first line contains two integers n nn,m mm(1 ≤ n , m ≤ 2000) indicating the size of photos A and B.
In the next n lines,each line contains n characters of ‘0’ or ‘1’,representing the matrix A.
The last line contains m characters of ‘2’, representing matrix B.
输出描述:
Output one integer in a line, indicating the answer.
示例1
输入
5 3
00000
01110
01110
01110
00000
222
输出
6
示例2
输入
3 2
101
010
101
22
输出
0
示例3
输入
3 1
101
010
101
2
输出
4
思路
签到题暴力判断就行了。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i<b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define pre1(i,a,b) for(int i=a;i>b;i--)
#define pep(i,stl) for(auto i:stl)
#define INF 0x7fffffff
#define eps 1e-6
const int N=2e6+5;
const int mod=1e9+7;
#define pep(i,stl) for(auto i:stl)
int arr[2005][2005];
int n,m,cnt;
int main() {
cin>>n>>m;
rep(i,1,n)
rep(j,1,n)
scanf("%1d",&arr[i][j]);
string s;
cin>>s;
rep(i,1,n)
{
int res=0;
rep(j,1,n)
if(!arr[i][j]){
res++;
if(res>=m) cnt++;
}
else res=0;
}
cout<<cnt;
return 0;
}
另一种做法:输入完之后预处理,记录每个空位置向右连续空位置的个数(包括当前空位置),之后遍历数组,得出答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i<b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define pre1(i,a,b) for(int i=a;i>b;i--)
#define pep(i,stl) for(auto i:stl)
#define INF 0x7fffffff
#define eps 1e-6
const int N=2e6+5;
const int mod=1e9+7;
#define pep(i,stl) for(auto i:stl)
int arr[2005][2005],num[2005][2005];
int n,m,cnt;
int main() {
cin>>n>>m;
rep(i,1,n)
rep(j,1,n)
scanf("%1d",&arr[i][j]);
string s;
cin>>s;
rep(i,1,n)
pre(j,n,1)
if(arr[i][j]) num[i][j]=-1;
else {
if(j<n&&!arr[i][j+1]) num[i][j]=num[i][j+1]+1;
else num[i][j]=1;
}
rep(i,1,n)
rep(j,1,n)
if(num[i][j]>=m)
cnt++;
cout<<cnt;
return 0;
}
Ball Dropping
题目描述
A standard sphere ball is falling in the air, and the center of the sphere is exactly on the centerline of an empty isosceles trapezoidal. The trapezoid is hanging horizontally under the sphere.

Please determine whether the ball will get stuck in the trapezoid or drop past the trapezoid.
输入描述:
The input contains four integers r,a,b,h(1≤r,a,b,h≤1000,a>b), indicating the radius of the ball, the top base, the bottom base, and the height of the isosceles trapezoid.
It is guaranteed that 2r≠b,2r<a,2r<h.
输出描述:
Output ‘Drop’ if the sphere ball will drop past the empty trapezoid, otherwise output ‘Stuck’.
If the answer is ‘Stuck’, please also calculate the stuck position(the height between the center of the sphere and the midpoint of the bottom base). Your answer is considered correct if its absolute or relative error does not exceed 10^−6.
示例1
输入
2 8 2 5
输出
Stuck2.2206345966
示例2
输入
1 8 3 5
输出
Drop
思路:
高中的几何题,用相似三角形和勾股定理即可求出
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i<b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define pre1(i,a,b) for(int i=a;i>b;i--)
#define pep(i,stl) for(auto i:stl)
#define INF 0x7fffffff
#define eps 1e-6
const int N=2e6+5;
const int mod=1e9+7;
#define pep(i,stl) for(auto i:stl)
int r,a,b,h;
int main() {
cin>>r>>a>>b>>h;
if(2*r<b)
cout<<"Drop";
else{
cout<<"Stuck"<<endl;
double x=1.0*b*h/(a-b),q=1.0*b/2/r,r=sqrt(x*x+b/2.0*b/2.0);
printf("%.10f",r/q-x);
}
return 0;
}
Alice and Bob
题目描述
Alice and Bob like playing games. There are two piles of stones with numbers n and m. Alice and Bob take turns to operate, each operation can take away k(k>0) stones from one pile and take away s×k(s≥0) stones from another pile. Alice plays first. The person who cannot perform the operation loses the game.
Please determine who will win the game if both Alice and Bob play the game optimally.
输入描述:
The first line contains an integer T(1≤T≤10^4) denotes the total number of test cases.
Each test case contains two integers n,m(1≤n,m≤5×10^3) in a line, indicating the number of two piles of stones.
输出描述:
For each test case, print “Alice” if Alice will win the game, otherwise print “Bob”.
示例1
输入
52 33 55 77 57 7
输出
BobAliceBobBobAlice
思路:
把{0,0}作为最初的必败状态,那么,所有可以一步推导为{0,0}的就是先手必胜。能一步推导为{0,0}的状态{x,y}为:{x+k,y+l*k}(x+k<=5000,y+l*k<=5000),{x+l*k,y+k}(y+k<=5000,x+l*k<=5000)
将先手必胜点记为1,先手必败点记为0。以此递推出所有的必胜必败点,并记录在数组中。之后每输入一个数据直接判断即可。
注意数组要用bool类型,不然会超时。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i<b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define pre1(i,a,b) for(int i=a;i>b;i--)
#define pep(i,stl) for(auto i:stl)
#define INF 0x7fffffff
#define eps 1e-6
const int N=2e6+5;
const int mod=1e9+7;
#define pep(i,stl) for(auto i:stl)
int arr[2005][2005];
bool dp[5005][5005];
ll n,m,cnt;
int main() {
rep(i,0,5000)
rep(j,0,5000){
if(!dp[i][j]){
for(int k=1;i+k<=5000;k++)
for(int l=0;j+l*k<=5000;l++)
dp[i+k][j+l*k]=1;
for(int k=1;j+k<=5000;k++)
for(int l=0;i+l*k<=5000;l++)
dp[i+l*k][j+k]=1;
}
}
int t;
cin>>t;
while(t--){
cin>>n>>m;
if(dp[n][m]) puts("Alice");
else puts("Bob");
}
return 0;
}
Find 3-friendly Integers
题目描述
A positive integer is 3-friendly if and only if we can find a continuous substring in its decimal representation, and the decimal integer represented by the substring is a multiple of 3.
For instance:
- 104 is 3-friendly because “0” is a substring of “104” and 0 mod 3=0.
- 124 is 3-friendly because “12” is a substring of “124” and 12 mod 3=0. “24” is also a valid substring.
- 17 is not 3-friendly because 1mod 3≠0, 7mod 3≠0, 17mod 3≠0.
Note that the substring with leading zeros is also considered legal.
Given two integers L and R, you are asked to tell the number of positive integers x such that L≤x≤R and x is 3-friendly.
输入描述:
There are multiple test cases. The first line of the input contains an integer T(1≤T≤10000), indicating the number of test cases. For each test case:
The only line contains two integers L,R(1≤L≤R≤10^18), indicating the query.
输出描述:
For each test case output one line containing an integer, indicating the number of valid x.
示例1
输入
34 101 201 100
输出
31176
思路:
当数据大到1e^18时,这时通常的做法就是打表找规律了。
通过打表发现,大于等于100的数全是3-friendly的数。因此,记录100以内不是3-friendly的数
判断这些数是否在区间内,若在则将区间长度减去不是3-friendly的数的个数,若都不在则输出区间长度
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep1(i,a,b) for(int i=a;i<b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define pre1(i,a,b) for(int i=a;i>b;i--)
#define pep(i,stl) for(auto i:stl)
#define INF 0x7fffffff
#define eps 1e-6
const int N=2e6+5;
const int mod=1e9+7;
#define pep(i,stl) for(auto i:stl)
int a[24]= {1,2,4,5,7,8,11,14,17,22,25,28,41,44,47,52,55,58,71,74,77,82,85,88};
int main() {
ll t,l,r,cnt=0;
cin>>t;
while(t--) {
cin>>l>>r;
cnt=r-l+1;
rep(i,0,23)
if(a[i]>=l&&a[i]<=r) cnt--;
cout<<cnt<<endl;
}
return 0;
}
博客介绍了四道算法题,包括确定照片位置、球下落情况、Alice和Bob的游戏以及寻找3友好整数。每道题给出了题目描述、输入输出要求和示例,还提供了解题思路,如暴力判断、利用几何定理、状态推导和打表找规律等。
1291

被折叠的 条评论
为什么被折叠?



