zhclg个人题目题单题解
A++B Problem
没啥好说的,根据题意模拟。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
int sum=a+b;
int ans=0;
while(sum){//按位拆分
ans+=sum%10;
sum/=10;
}
cout<<ans;
return 0;
}
地图行走
地图搜索算法,深搜、广搜均可,在这里介绍深搜做法。
时间复杂度
#include<iostream>
using namespace std;
int n,m,ans=0;
int dx[4]={0,0,1,-1},dy[4]={-1,1,0,0};//存储偏移量
char mp[10][10];
bool vis[10][10];//标记是否访问
bool check(int x,int y){//check点是否合法
if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&mp[x][y]!='#'){
return 1;
}
else{
return 0;
}
}
void dfs(int x,int y){
if(x==n&&y==m){//发现到达终点
ans++;
return;
}
for(int i=0;i<4;i++){
int tox=x+dx[i],toy=y+dy[i];
if(check(tox,toy)){
vis[tox][toy]=1;
dfs(tox,toy);//递归的搜索
vis[tox][toy]=0;//回溯
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j];
}
}
vis[1][1]=1;
dfs(1,1);
if(ans==0){
cout<<-1;//无解
}
else cout<<ans;//有解
return 0;
}
巡视教室
看到数据范围就炸了,考虑前缀和。
这题还是个二维的,画个图就能推出前缀和式子。
时间复杂度
具体见代码。
#include<iostream>
#define int long long
using namespace std;
int a[5010][5010],n,m,q;
signed main(){
cin>>n>>m>>q;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int tp;
cin>>tp;
a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+tp;//由左、上、左上三个点计算前缀和,但重复
}
}
while(q--){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
cout<<a[x2][y2]-a[x1-1][y2]-a[x2][y1-1]+a[x1-1][y1-1]<<endl;//减掉重复的
}
return 0;
}
玩小飞棍
搜。不要爆搜。剪枝。
爆搜应该都会,剪枝就是发现任何一个比边长要长了就回溯。
理论复杂度
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
bool flag=false;
int wood[30],cnt=0,m;
void dfs(int x,int a,int b,int c,int d){
if(flag||a>cnt||b>cnt||c>cnt||d>cnt){
return;
}//剪枝
if(x>m){
if(a==b&&b==c&&c==d){
cout<<"yes"<<endl;//有合法解
flag=true;
return;
}
return;
}
dfs(x+1,a+wood[x],b,c,d);
dfs(x+1,a,b+wood[x],c,d);
dfs(x+1,a,b,c+wood[x],d);
dfs(x+1,a,b,c,d+wood[x]);//搜
}
int main(){
int T;
cin>>T;
while(T--){
cin>>m;
cnt=0,flag=false;
for(int i=1;i<=m;++i){
cin>>wood[i];
cnt+=wood[i];
}
if(cnt%4!=0){
cout<<"no"<<endl;//包不行的
}
else{
cnt/=4;//边长
dfs(1,0,0,0,0);
if(flag==false){
cout<<"no"<<endl;
}
}
}
return 0;
}
疯狂的不互质
数学题。可以用容斥原理。
设
容易想到,因数个数为奇数的要加上
时间复杂度
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>get_(int k){//分解质因数
vector<int>factors;
for(int i=2;i*i<=k;i++){
if(k%i==0){
factors.push_back(i);
while(k%i==0){
k/=i;
}
}
}
if(k>1){
factors.push_back(k);
}
return factors;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin>>q;
while(q--){
int n,k;
cin>>n>>k;
if(k==1){
cout<<0<<'\n';
continue;
}
vector<int>primes=get_(k);
int m=primes.size(),ans=0;
for(int l=1;l<(1<<m);l++){
int dx=1,bits=0;
for(int i=0;i<m;i++){
if(l&(1<<i)){
dx*=primes[i];//求积
bits++;
if(dx>n)break;//小优化
}
}
if(dx>n)continue;
if(bits%2==1){
ans+=n/dx;
}
else{
ans-=n/dx;
}//加减
}
cout<<ans<<'\n';
}
return 0;
}
手捧雷游戏
模拟题,不说了。
//说明:人坤代码
#include <bits/stdc++.h>
using namespace std;
int rank_id(char c) {
if (c == 'A') return 0;
if (c >= '2' && c <= '9') return c - '1'; // '2'->1, ... '9'->8
if (c == 'D') return 9;
if (c == 'J') return 10;
if (c == 'Q') return 11;
if (c == 'K') return 12;
return -1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, K;
cin >> N >> K;
vector<array<int,13>> cnt(N+1);
vector<int> sz(N+1,4);
vector<bool> hasBomb(N+1,false);
vector<bool> banned(N+1,false);
string line;
for(int i=1;i<=N;i++){
cin >> line;
for(char c: line){
int r = rank_id(c);
cnt[i][r]++;
}
}
hasBomb[K] = true;
sz[K] = 5;
banned[K] = true;
int cur = K;
while(true){
for(int i=1;i<=N;i++){
if(sz[i]==4){
for(int r=0;r<13;r++){
if(cnt[i][r]==4){
cout << i << "\n";
return 0;
}
}
}
}
int nxt = cur==N ? 1 : cur+1;
bool sentBomb = false;
if(hasBomb[cur] && !banned[cur]){
sentBomb = true;
hasBomb[cur] = false;
sz[cur]--;
}
else{
int choice = -1, mn = INT_MAX;
for(int r=0;r<13;r++){
if(cnt[cur][r]>0 && cnt[cur][r]<mn){
mn = cnt[cur][r];
choice = r;
}
}
cnt[cur][choice]--;
sz[cur]--;
sentBomb = false;
cnt[nxt][choice]++;
sz[nxt]++;
}
if(sentBomb){
hasBomb[nxt] = true;
sz[nxt]++;
banned[nxt] = true;
}
banned[cur] = false;
cur = nxt;
}
return 0;
}
二叉树的翻转颜色
考虑差分优化。
首先在树上跑一个dfs,记录一下时间戳(in时间和out时间),读入操作之后,借助跑出来的时间戳,进行差分。
最后借助差分结果与in时间戳,统计答案。
时间复杂度
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;
vector<int>mp[MAXN];
int in[MAXN],out[MAXN],diff[MAXN];
int color[MAXN];
int dfsCnt=0;
void dfs(int u){
in[u]=++dfsCnt;
for(int v:mp[u]){
dfs(v);
}
out[u]=dfsCnt;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
for(int i=1;i<n;i++){
int x;
cin>>x;
mp[x].push_back(i+1);
}
dfs(1);
string s;
cin>>s;
for(int i=1;i<=n;i++){
color[i]=s[i-1]-'0';
}
int Q;
cin>>Q;
while(Q--){
int x;
cin>>x;
diff[in[x]]++;
diff[out[x]+1]--;
}
for(int i=1;i<=n;i++){
diff[i]+=diff[i-1];
}
string ans(n,'0');
for(int i=1;i<=n;i++){
int flip=diff[in[i]]%2;
ans[i-1]=(color[i]^flip)+'0';
}
cout<<ans<<endl;
return 0;
}
逛庙会
看到范围,广搜走起。
其实跑两遍两个人就行了,没什么难的,只是偏移量特殊一点。
坑:注意过滤空格
时间复杂度
#include<bits/stdc++.h>
using namespace std;
const int dx8[]={-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[]={-1, 0, 1, -1, 1, -1, 0, 1};
const int dx4[]={-1, 1, 0, 0};
const int dy4[]={0, 0, -1, 1};
int n,m;
char mp[1010][1010];
int cx,cy,Dx,Dy;
bool check(int x,int y){
return x>=0&&x<n&&y>=0&&y<m;
}
void bfsC(vector<vector<int>>&distC){
queue<pair<int,int>>q;
q.push({cx,cy});
distC[cx][cy]=0;
while(!q.empty()){
pair<int,int> tp=q.front();
int x=tp.first,y=tp.second;
q.pop();
for(int i=0;i<8;i++){
int nx=x+dx8[i],ny=y+dy8[i];
if(check(nx,ny)&&mp[nx][ny]=='.'&&distC[nx][ny]>distC[x][y]+1){
distC[nx][ny]=distC[x][y]+1;
q.push({nx,ny});
}
}
}
}
void bfsD(vector<vector<int>>&distD){
queue<pair<int,int>>q;
q.push({Dx,Dy});
distD[Dx][Dy]=0;
while(!q.empty()){
pair<int,int> tp=q.front();
int x=tp.first,y=tp.second;
q.pop();
for(int i=0;i<4;i++){
int nx=x+dx4[i],ny=y+dy4[i];
if(check(nx,ny)&&mp[nx][ny]=='.'&&distD[nx][ny]>distD[x][y]+1){
distD[nx][ny]=distD[x][y]+1;
q.push({nx,ny});
}
}
for(int i=0;i<4;i++){
int nx1=x+dx4[i],ny1=y+dy4[i];
if(!check(nx1,ny1)||mp[nx1][ny1]!='.') continue;
for(int i1=0;i1<4;i1++){
int nx2=nx1+dx4[i1],ny2=ny1+dy4[i1];
if(check(nx2,ny2)&&mp[nx2][ny2]=='.'&&distD[nx2][ny2]>distD[x][y]+1){
distD[nx2][ny2]=distD[x][y]+1;
q.push({nx2,ny2});
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
char ch;
cin>>ch;
while(ch==' '){
cin>>ch;
}
mp[i][j]=ch;
if(mp[i][j]=='C'){
cx=i,cy=j;
mp[i][j]=':';
}
if(mp[i][j]=='D'){
Dx=i,Dy=j;
mp[i][j]=':';
}
}
}
vector<vector<int>>distC(n,vector<int>(m,1e9));
vector<vector<int>>distD(n,vector<int>(m,1e9));
bfsC(distC);
bfsD(distD);
int ans=1e9;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mp[i][j]=='.'&&distC[i][j]!=1e9&&distD[i][j]!=1e9){
ans=min(ans,max(distC[i][j],distD[i][j]));
}
}
}
if(ans==1e9){
cout<<"NO";
}
else{
cout<<"YES"<<endl<<ans;
}
return 0;
}
竞赛真理
数据不大。考虑背包DP。
首先,按结束时间排序。
状态:
转移:当i在这题结束时间,有dp[i][j]=max(dp[i][j],dp[a[idx].s-1][j]+a[idx].w);
否则,有dp[i][j]=max(dp[i][j],dp[i-1][j]);
最后,大脑2和大脑1copy一下就可以了。
时间复杂度
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
const int MAXN = 110 ,MAXT = 510;
LL t ,n ,dp[MAXT][MAXT] ,ans = 0;
struct node {
int s, e ,w;
}a[MAXN];
bool cmp(node a,node b) {
return a.e < b.e;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t >> n;
for (int i = 1;i <= n;++i) {
cin >> a[i].s >> a[i].e >> a[i].w;
}
sort(a + 1 , a + 1 + n , cmp);//排序
if (n == 1) {
cout << a[1].w << '\n';
return 0;//特判
}
dp[0][0] = 0;
for (int idx = 1;idx <= n;++idx) {
for (int i = a[idx].e;i <= t;++i) {
for (int j = 0;j <= t;++j) {
if (i == a[idx].e)dp[i][j]=max(dp[i][j],dp[a[idx].s-1][j]+a[idx].w);
else dp[i][j]=max(dp[i][j],dp[i-1][j]);//dp式
}
}
for (int i = 0;i < MAXT;++i) {
for (int j = 0;j < MAXT;++j) {
dp[i][j]=dp[j][i];//copy
}
}
}
cout <<dp[t][t];
return 0;
}
魔法水晶
“第k小”,很容易看出二分。
在check中考虑用双指针优化至
注意要排序。
时间复杂度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll k ,a[100010];
bool check(ll mid) {
ll cnt = 0;
int j = n - 1;//双指针
for (int i = 0; i < n; i++) {
while (j > i && (ll)a[i] + a[j] > mid) {
j--;
}
if (j > i) {
cnt += (j - i);
}
}
return cnt >= k;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a+n);//排序
ll l = (ll)a[0] + a[1] ,r = (ll)a[n-1] + a[n-2];
ll ans = 0;
while (l <= r) {//二分板子
ll mid = l + (r - l) / 2;
if (check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
cout << ans << endl;
return 0;
}