A.Hcode OnlineJudge
看明白题面的话就是求第N阶矩形,第N阶矩形又由N-1阶合成而来,所以只需要从0阶递推就行
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 998244353;
int n, m, k;
int qmi(int x){
int res=1;
int y=3;
while(x){
if(x&1) res*=y;
x>>=1;
y*=3;
}return res;
}
void sovle(){
cin>>n;
int x=n-1;
char ch='#';
vector<string>v;
//v.push_back("#");
if(n==0){
cout<<"#"<<endl;
return;
}else if(n==1){
cout<<"###"<<endl;
cout<<"#.#"<<endl;
cout<<"###"<<endl;
return;
}
v.push_back("###");
v.push_back("#.#");
v.push_back("###");
int y=1;
while(x>0){
y*=3;
x--;
vector<string>q;
string j;
for(int i=0;i<y;i++){
j.push_back('.');
}
for(int i=0;i<y*3;i++){
string s=v[i%y];//cout<<s<<endl;
if(i>=y&&i<2*y) q.push_back(s+j+s);
else q.push_back(s+s+s);
}cout<<endl;
if(x==0){
for(auto ed:q){
cout<<ed<<endl;
}
}else{
while(v.size()){
v.pop_back();
}
for(auto ed:q){
v.push_back(ed);
}
}
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t --){
sovle();
}
return 0;
}
B.Hcode OnlineJudge
询问字符串在区间内si==si+1的个数,用前缀和预处理然后O(1)查询就行了
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e9+10;
const int mod = 998244353;
int n,m,k;
void sovle(){
cin>>n>>m;
string s;
cin>>s;
vector<int>a(s.size()+1,0);
for(int i=1;i<s.size();i++){
if(i) a[i]+=a[i-1];
if(s[i-1]==s[i]) a[i]++;
//cout<<i<<endl;cout<<a[i]<<endl;
}
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
cout<<a[y-1]-a[x-1]<<endl;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t--){
sovle();
}
return 0;
}
C.Hcode OnlineJudge
n个单词m行,询问每行至少多少宽度才能完成放下n个单词,如果在同一行,单词间有一个空格
显然答案具有单调性,如果x能满足,那么x+1也一定能满足。二分答案查找即可
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 2e5;
int n,m,k;
bool ok(int x,vector<int>a){
int sum=1;
int u=x;
bool ok=1;
for(int i=0;i<n;i++){
if(ok){
if(a[i]>x) return 0;
else if(u<a[i]){
sum++;
u=x-a[i];
continue;
}else if(u==a[i]) {u=0;ok=1;}
else u-=a[i];
ok=0;
}else{
u--;
if(a[i]>x) return 0;
else if(u<a[i]){
sum++;
u=x-a[i];
}else if(u==a[i]) {u=0;ok=1;}
else u-=a[i];
}
}
if(sum<=m) return 1;
return 0;
}
void sovle(){
cin>>n>>m;
vector<int>a(n);
for(int i=0;i<n;i++) cin>>a[i];
int l=0,r=2e18,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(ok(mid,a)){
ans=mid;
r=mid-1;
}else l=mid+1;
}
cout<<ans<<endl;
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t --){
sovle();
}
return 0;
}
D.Hcode OnlineJudge
贪心地取最小的开始的时间,时间相同则先取最小的离开时间,用优先队列维护离开的时间
如果贪心优先取离开最早的,那么有可能会浪费前面的时间
#include<bits/stdc++.h>
#define endl '\n'
#define mk make_pair
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 1e6+10;
int n,m,k;
void sovle(){
cin>>n;
vector<PII>v(n);
priority_queue<int,vector<int>,greater<int> >q;
for(int i=0;i<n;i++){
cin>>v[i].first>>v[i].second;
v[i].second+=v[i].first;
}
int i=0,ans=0,last=0;
sort(v.begin(),v.end());
while(1){
if(q.empty()){
if(i==n){
cout<<ans<<endl;
return;
}
last=v[i].first;
}
while(i<n&&v[i].first==last){
q.push(v[i].second);
i++;
}
while(last>q.top()&&q.size()){
q.pop();
}
if(q.size()){
ans++;
q.pop();
}
last+=1;
}
}
signed main()
{
ios::sync_with_stdio(false), cin.tie(0),cout.tie(0);
int t = 1;
//cin>>t;
while (t --){
sovle();
}
return 0;
}