A.Piling Up (\(\operatorname{Difficulty} 11\))
让你求某个数距离最近的一个 \(k\times 100\) 的距离是多少.
水.
#include<bits/stdc++.h>
using namespace std;
namespace hdk{
namespace fastio{
void rule(bool setting=false){std::ios::sync_with_stdio(setting);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline int read(int &A){A=read();return A;}
inline char read(char &A){A=getchar();return A;}
inline void write(int A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(long long A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(char A){putchar(A);}
inline void space(){putchar(' ');}
inline void endl(){putchar('\n');}
#define w(a) write(a)
#define we(a) write(a);endl()
#define ws(a) write(a);space()
}
}
int power(int n,int k,int p){
int ans,base;
ans=1,base=n;
while(k){
if(k&1){
ans*=base;
}
base*=base;
k>>=1;
}
return ans;
}
using namespace hdk::fastio;
#define speed ios::sync_with_stdio(false);
#define tests int cases;cin>>cases;while(cases--)
#define ftests int cases=read();while(cases--)
int main(){
int n;
cin>>n;
if(n<=99) cout<<99-n+1;
else if(n<=199) cout<<199-n+1;
else if(n<=299) cout<<299-n+1;
// ftests{
//
// }
}
B.Japanese Cursed Doll (\(\operatorname{Difficulty} 32\))
显然这个题排个序直接取数组第 \(p\) 位就行了,在这里吃一发罚时太糖了,排序忘记写 less<>.
感觉这场 ABC 前面都太水了
#include<bits/stdc++.h>
using namespace std;
namespace hdk{
namespace fastio{
void rule(bool setting=false){std::ios::sync_with_stdio(setting);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline int read(int &A){A=read();return A;}
inline char read(char &A){A=getchar();return A;}
inline void write(int A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(long long A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(char A){putchar(A);}
inline void space(){putchar(' ');}
inline void endl(){putchar('\n');}
#define w(a) write(a)
#define we(a) write(a);endl()
#define ws(a) write(a);space()
}
}
int power(int n,int k,int p){
int ans,base;
ans=1,base=n;
while(k){
if(k&1){
ans*=base;
}
base*=base;
k>>=1;
}
return ans;
}
using namespace hdk::fastio;
#define speed ios::sync_with_stdio(false);
#define tests int cases;cin>>cases;while(cases--)
#define ftests int cases=read();while(cases--)
int a[101];
bool cmp(int a,int b){
return a>b;
}
int main(){
int n,t,p;
cin>>n>>t>>p;
for(int i=1;i<=n;++i){
cin>>a[i];
}
sort(a+1,a+n+1,cmp);
int ans=max(0,t-a[p]);
cout<<ans;
}
C.Avoid K Palindrome 2 (\(\operatorname{Difficulty} 602\))
给一个 \(S\),求 \(S\) 的全部排列中,不包括长度为 \(k\) 的字串的有多少个.
主要是没看到数据范围,这题 \(n\le10\),感觉再高点还有别的办法,但是既然都给这么小了那就直接爆搜吧.
第一次知道 string 能用 sort 和 next_permutation
#include<bits/stdc++.h>
using namespace std;
namespace hdk{
namespace fastio{
void rule(bool setting=false){std::ios::sync_with_stdio(setting);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){f=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline int read(int &A){A=read();return A;}
inline char read(char &A){A=getchar();return A;}
inline void write(int A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(long long A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
inline void write(char A){putchar(A);}
inline void space(){putchar(' ');}
inline void endl(){putchar('\n');}
#define w(a) write(a)
#define we(a) write(a);endl()
#define ws(a) write(a);space()
}
}
int power(int n,int k,int p){
int ans,base;
ans=1,base=n;
while(k){
if(k&1){
ans*=base;
}
base*=base;
k>>=1;
}
return ans;
}
using namespace hdk::fastio;
#define speed ios::sync_with_stdio(false);
#define tests int cases;cin>>cases;while(cases--)
#define ftests int cases=read();while(cases--)
int a[101];
bool cmp(int a,int b){
return a>b;
}
bool check(string x,int len){
// cout<<"check "<<x<<endl;
for(int i=0;i+len-1<=(int)x.length()-1;++i){
int j=i,k=i+len-1;
bool ishw=true;
while(j<k){
if(x[j]!=x[k]){
// cout<<x[j]<<" != "<<x[k]<<endl;
ishw=false;
break;
}
j++;k--;
}
if(ishw) return false;
}
return true;
}
int main(){
int n,m;
string x;
cin>>n>>m>>x;
sort(x.begin(),x.end());
int ans=0;
do{
ans+=check(x,m);
}while(next_permutation(x.begin(),x.end()));
cout<<ans<<endl;
}
D.Palindromic Number (\(\operatorname{Difficulty} 975\))
可能是前几道里最有含金量的一个了,可惜还是道原题.
求第 \(k\) 大的回文数. 可以考虑直接预处理出一些数用来逼近
显然,对于 \(n (n \operatorname{mod} 2=0)\) 来说,\(n\) 位的回文数的个数实际上就是 \(\frac{n}{2}\) 位的自然数的个数,对于 \(n (n \operatorname{mod} 2=1)\) 的数也同理,因此可以考虑直接通过这样预处理算出第 \(k\) 大的回文数一共有几位,再然后直接按照左半部分递增逼近就行了.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
int a[]={0,9,9,90,90,900,900,9000,9000,90000,90000,900000,900000,9000000,9000000,90000000,90000000,900000000,900000000,9000000000,9000000000,90000000000,90000000000,900000000000,900000000000,9000000000000,9000000000000,90000000000000,90000000000000,900000000000000,900000000000000,9000000000000000,9000000000000000,90000000000000000,90000000000000000,900000000000000000,900000000000000000,9000000000000000000,9000000000000000000};
int b[100],len;
signed main(){
int k,t,i,bk;
cin>>n;
if(n==1){
cout<<0;
return 0;
}
n--;
len=0;
k=1;
while(n-a[k]>0){
n-=a[k++];
}
bk=(k+1)>>1;
t=1;
for(i=2;i<=bk;i++){
t*=10;
}
t+=n-1;
while(t){
b[++len]=t%10;
t/=10;
}
for(i=len;i>=1;i--){
cout<<b[i];
}
for(i=k&1?2:1;i<=len;i++){
cout<<b[i];
}
cout<<endl;
}
E.Sinking Land (\(\operatorname{Difficulty} 1307\))
其实就是个二维弱化版的 WaterTank,所以一开始还想着用 WaterTank 的那种单调队列去实现,后来发现数据范围能过 \(n^{2}\log n\),所以就改了一个优先队列广搜.
优先队列广搜?这不是 DIJ?
好像是吧... 还挺一样的,但是我有一半用的深搜,其实是一样的.
#include<bits/stdc++.h>
using namespace std;
const int N=1001;
struct node{
int x,y,w;
bool operator <(const node &A)const{
return w>A.w;
}
};
int n,m,t;
priority_queue<node> q;
int a[N][N];
bool vis[N][N];
int tot;
void search(int x,int y,int t){
if(x>1 and !vis[x-1][y]){
if(a[x-1][y]<=t){
vis[x-1][y]=true;
// cout<<"Yes"<<x-1<<" "<<y<<endl;
tot++;
search(x-1,y,t);
}
else{
q.push({x-1,y,a[x-1][y]});
}
}
if(x<n and !vis[x+1][y]){
if(a[x+1][y]<=t){
vis[x+1][y]=true;
// cout<<"Yes"<<x+1<<" "<<y<<endl;
tot++;
search(x+1,y,t);
}
else{
q.push({x+1,y,a[x+1][y]});
}
}
if(y>1 and !vis[x][y-1]){
if(a[x][y-1]<=t){
vis[x][y-1]=true;
// cout<<"Yes"<<x<<" "<<y-1<<endl;
tot++;
search(x,y-1,t);
}
else{
q.push({x,y-1,a[x][y-1]});
}
}
if(y<m and !vis[x][y+1]){
if(a[x][y+1]<=t){
vis[x][y+1]=true;
// cout<<"Yes"<<x<<" "<<y+1<<endl;
tot++;
search(x,y+1,t);
}
else{
q.push({x,y+1,a[x][y+1]});
}
}
}
int main(){
cin>>n>>m>>t;
// cout<<"fin"<<endl;
for(int i=1;i<=n;++i){
// cout<<i<<endl;
for(int j=1;j<=m;++j){
// cout<<"input";
cin>>a[i][j];
}
}
for(int i=1;i<=m;++i){
q.push({1,i,a[1][i]});
q.push({n,i,a[n][i]});
}
for(int i=2;i<n;++i){
q.push({i,1,a[i][1]});
q.push({i,m,a[i][m]});
}
for(int k=1;k<=t;++k){
while(!q.empty() and q.top().w<=k){
node u=q.top();
q.pop();
if(vis[u.x][u.y]) continue;
vis[u.x][u.y]=true;
// cout<<"Yes"<<u.x<<" "<<u.y<<endl;
tot++;
search(u.x,u.y,k);
}
cout<<n*m-tot<<endl;
}
}
后记
纪念传奇 ABC 出 \(\operatorname{Difficulty} 3358\) 的题干掉 Tourist
标签:AtCoder,putchar,Beginner,int,void,write,while,363,define From: https://www.cnblogs.com/HaneDaCafe/p/18314118