雅礼兴趣小组总结-2
习题课
Number of Pairs
题意
在数组中有几个满足:
知识点
排序,二分查找
正解
从小到大排序,再用二分查找范围
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int a[200005];
int Q , n , l , r;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
long long cnt=0;
cin >> n >> l >> r;
for (int i=1;i<=n;i++)
{
cin >> a[i];
}
sort (a+1 , a+n+1);
for (int i=1;i<=n;i++)
{
int pos1=lower_bound (a+i+1 , a+n+1 , l-a[i])-a;
int pos2=upper_bound (a+i+1 , a+n+1 , r-a[i])-a;
cnt+=pos2-pos1;
}
cout << cnt << "\n";
}
return 0;
}Dorms War
题意
每次可以将所有特殊字母前的字母删除,再继续直到只有一个特殊字母且在第一位,求有几次
知识点
找规律
正解
~~找规律中…~~找到了
将特殊字母分别的位置记下来,再取
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
map <char , bool> mp;
string s;
int v[200005];
int Q , n , m , tot;
char p;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
tot=0;
mp.clear ( );
cin >> n >> s >> m;
while (m--)
{
cin >> p;
mp[p]=1;
}
for (int i=0;i<=n-1;i++)
{
if (mp[s[i]]==1) v[++tot]=i;
}
int cnt=0;
for (int i=1;i<=tot;i++)
{
cnt=max (cnt , v[i]-v[i-1]);
}
cout << cnt << "\n";
}
return 0;
}Keshi Is Throwing a Party
题意
选第个人要使:
- 前面的人数
- 后面的人数
求最多能选几个人
知识点
二分答案,贪心
正解
用二分查找,在内判断是否有:
且-
错因
我觉得只跟有关
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int a[200005] , b[200005];
int Q , n;
bool check (int mid)
{
int cnt=0;
for (int i=1;i<=n;i++)
{
if (cnt<mid&&a[i]>=mid-cnt-1&&b[i]>=cnt) cnt++;
}
return (cnt==mid);
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i] >> b[i];
}
int l=1 , r=n;
while (l<r)
{
int mid=(l+r+1)/2;
if (check (mid)==1)
{
l=mid;
}
else
{
r=mid-1;
}
}
cout << l << "\n";
}
return 0;
}Restoring the Permutation
题意
,给出,求字典序最小和最大的
知识点
或+
正解
从前往后选择,将新来的可以的数加进来,再分别弹出:
- 队首,队尾
- + 队首,栈顶
错因
我好像没错老师前2天前讲过
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
deque <int> x , y;
int a[200005] , b[200005] , c[200005];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
if (a[i]!=a[i-1])
{
b[i]=a[i];
c[i]=a[i];
for (int j=a[i-1]+1;j<=a[i]-1;j++)
{
x.push_back (j);
y.push_back (j);
}
}
else
{
b[i]=x.front ( );
x.pop_front ( );
c[i]=y.back ( );
y.pop_back ( );
}
}
for (int i=1;i<=n;i++)
{
cout << b[i] << " ";
}
cout << "\n";
for (int i=1;i<=n;i++)
{
cout << c[i] << " ";
}
cout << "\n";
}
return 0;
}Same Count One
题意
将每行的1都弄相等,输出步骤,不可行输出
知识点
,思想
正解
对于每列,选出当前可以给出1和还需要1的行,再两两配对
错因
题都没看
心得
要给题留一点时间
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
vector <int> g[100005];
vector <int> x , y;
int cnt[100005];
int X[1000005] , Y[1000005] , Z[1000005];
int Q , n , m , tot , ans;
bool p;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
tot=0 , ans=0;
cin >> n >> m;
for (int i=1;i<=n;i++)
{
cnt[i]=0;
g[i].clear ( );
g[i].push_back (0);
for (int j=1;j<=m;j++)
{
cin >> p;
cnt[i]+=(p==1);
g[i].push_back (p);
tot+=(p==1);
}
}
if (tot%n>0)
{
cout << -1 << "\n";
}
else
{
tot/=n;
for (int i=1;i<=m;i++)
{
x.clear ( );
y.clear ( );
for (int j=1;j<=n;j++)
{
if (cnt[j]<tot&&g[j][i]==0) x.push_back (j);
if (cnt[j]>tot&&g[j][i]==1) y.push_back (j);
}
if (min (x.size ( ) , y.size ( ))==0) continue;
for (int j=0;j<=min (x.size ( ) , y.size ( ))-1;j++)
{
ans++;
X[ans]=x[j] , Y[ans]=y[j] , Z[ans]=i;
cnt[x[j]]++;
cnt[y[j]]--;
}
}
cout << ans << "\n";
for (int i=1;i<=ans;i++)
{
cout << X[i] << " " << Y[i] << " " << Z[i] << "\n";
}
}
}
return 0;
}Sending a Sequence Over the Network
题意
是否可以将拆成若干段,使每一段的长度在最左边或最右边
知识点
正解
超级无敌简单的推使,将两种都写一遍
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
bool f[200005];
int a[200005];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
f[i]=0;
}
f[0]=1;
for (int i=1;i<=n;i++)
{
if (i-a[i]>=0)
{
f[i]|=f[i-a[i]-1];
}
if (i+a[i]<=n)
{
f[i+a[i]]|=f[i-1];
}
}
if (f[n]==1) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}习题课(兴趣小组)
Strange Table
题意
行列转换,求转换后的值
知识点
除法
正解
简单死了,不用讲
错因
我好像没错
心得
看反了,浪费了分钟
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int Q , n , m , x , y , a , b;
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> m >> x;
swap (n , m);
if (x%m==0)
{
b=x/m;
a=m;
cout << (a-1)*n+b << "\n";
}
else
{
b=x/m;
a=x%m;
cout << (a-1)*n+(b+1) << "\n";
}
}
return 0;
}Partial Replacement
题意
首尾两个要变成,其它的可变可不变,在相邻两个的距离的情况下,至少有几个要变成
知识点
贪心,
正解
记录每次在哪里,每次取满足条件的最大下标,计数即可
错因
我好像没错
心得
Double-ended Strings
题意
求两个的最长公共子串
知识点
,最长公共子串
正解
暴力
暴力枚举即可
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
string a , b;
int Q , n , m;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> a >> b;
n=a.size ( ) , m=b.size ( );
a=" "+a , b=" "+b;
int ans=n+m;
for (int len=1;len<=min (n , m);len++)
{
for (int j=1;j<=n-len+1;j++)
{
for (int k=1;k<=m-len+1;k++)
{
if (a.substr (j , len)==b.substr (k , len))
{
ans=min (ans , (n+m)-len*2);
}
}
}
}
cout << ans << "\n";
}
return 0;
}DP
用最长公共子串的方法
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int dp[25][25];
string s , t;
int Q , n , m , maxn;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
maxn=0;
cin >> s >> t;
n=s.size ( ) , m=t.size ( );
s=" "+s , t=" "+t;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (s[i]==t[j])
{
dp[i][j]=dp[i-1][j-1]+1;
}
else
{
dp[i][j]=0;
}
maxn=max (maxn , dp[i][j]);
}
}
cout << n+m-maxn*2 << "\n";
}
return 0;
} Epic Transformation
题意
每次可以移除两个不相同的数,求最后最少剩下几个数
知识点
_,
正解
模拟
用_模拟两个次数最多的数
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
priority_queue <int> q , qp;
map <int , int> mp;
int Q , n , v;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
while (!(q.empty ( )))
{
q.pop ( );
}
mp.clear ( );
cin >> n;
while (n--)
{
cin >> v;
mp[v]++;
}
for (auto i:mp)
{
q.push (i.second);
}
while (q.size ( )>1)
{
qp=q;
int v1=q.top ( );
q.pop ( );
int v2=q.top ( );
q.pop ( );
if (v1>1) q.push (v1-1);
if (v2>1) q.push (v2-1);
}
if (q.empty ( )) cout << 0 << "\n";
else cout << q.top ( ) << "\n";
}
return 0;
}结论
暂无
复杂度
- 时间复杂度:
- 空间复杂度:
代码
Restoring the Permutation
题意
,给出,求字典序最小和最大的
知识点
或+
正解
从前往后选择,将新来的可以的数加进来,再分别弹出:
- 队首,队尾
- + 队首,栈顶
错因
我好像没错老师前2天前讲过
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
deque <int> x , y;
int a[200005] , b[200005] , c[200005];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
if (a[i]!=a[i-1])
{
b[i]=a[i];
c[i]=a[i];
for (int j=a[i-1]+1;j<=a[i]-1;j++)
{
x.push_back (j);
y.push_back (j);
}
}
else
{
b[i]=x.front ( );
x.pop_front ( );
c[i]=y.back ( );
y.pop_back ( );
}
}
for (int i=1;i<=n;i++)
{
cout << b[i] << " ";
}
cout << "\n";
for (int i=1;i<=n;i++)
{
cout << c[i] << " ";
}
cout << "\n";
}
return 0;
}Triangular Paths
题意
求从到的最短路长度
知识点
排序,思想
正解
分情况递归int x=a-b , y=c-d;
- 如果
x==y&&x%2==0,那么一定要每次往右走但是偶数,就必须付全部的,即c-a - 如果
x%2==0,那么直接按照0消耗的走 - 如果
y%2==0,那么一定是从起点往右下方走,先求出到右上方的,在 - 否则,无论如何也要跨过
(y-x)/2次
错因
觉得难,直接跳过
心得
要看一下题
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
struct node
{
int r , c;
};
node v[200005];
int Q , n , sum;
bool cmp (node x , node y)
{
return x.r<y.r;
}
int jl (int a , int b , int c , int d)
{
int x=a-b , y=c-d;
if (x==y&&x%2==0) return c-a;
if (x%2==0) return jl (a+1 , b , c , d);
if (y%2==0) return jl (a , b , c-1 , d)+1;
return (y-x)/2;
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> v[i].r;
}
for (int i=1;i<=n;i++)
{
cin >> v[i].c;
}
sort (v+1 , v+n+1 , cmp);
sum=jl (1 , 1 , v[1].r , v[1].c);
for (int i=1;i<=n-1;i++)
{
int a=v[i].r , b=v[i].c , c=v[i+1].r , d=v[i+1].c;
sum+=jl (a , b , c , d);
}
cout << sum << "\n";
}
return 0;
}
//Maximize the Remaining String
题意
在做到在中出现的字符都只保留一个(保留当前位置)变成的字符串,求
知识点
思想
正解
将每一位字符从大往小一位一位去试,成功就试下一位
错因
一下觉得要打暴力,一下觉得要存很多数组,一下觉得要用vector存说白了,就全都是错的
代码
心得
要先推样例,不然一大堆想法都是错的
复杂度
- 时间复杂度:
- 空间复杂度:
#include <bits/stdc++.h>
using namespace std;
string s , t;
bool vis[30];
int Q , cnt;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
memset (vis , 0 , sizeof (vis));
cin >> s;
t="" , cnt=0;
for (int i=0;i<=int (s.size ( ))-1;i++)
{
vis[s[i]-'a'+1]=1;
}
for (int i=1;i<=26;i++)
{
cnt+=vis[i];
}
for (int i=1;i<=cnt;i++)
{
int k , id;
char op;
int idid;
for (int j=26;j>=1;j--)
{
int ID;
for (int k=0;k<=int (s.size ( ))-1;k++)
{
if (s[k]==char (j+'a'-1))
{
ID=k;
break;
}
}
if (vis[j]==0) continue;
bool abc=1;
for (char k='z';k>='a';k--)
{
if (char (j+'a'-1)==k||vis[k-'a'+1]==0) continue;
bool kkk=0;
if (id>=ID) kkk=1;
for (int P=ID;P<=int (s.size ( ))-1;P++)
{
if (s[P]==k)
{
kkk=1;
break;
}
}
abc&=kkk;
}
if (abc==1)
{
k=j;
idid=ID;
break;
}
}
op=char (k+'a'-1);
t+=op;
vis[k]=0;
s=s.substr (idid);
for (int j=0;j<=int (s.size ( ))-1;j++)
{
if (s[j]==op)
{
s.erase (j , 1);
}
}
}
cout << t << "\n";
}
return 0;
}
//新课
B3647 【模板】Floyd
模板题,传递闭包
代码
#include <bits/stdc++.h>
using namespace std;
long long dis[105][105];
long long z;
int n , m , x , y;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset (dis , 0x3f , sizeof (dis));
cin >> n >> m;
while (m--)
{
cin >> x >> y >> z;
dis[x][y]=min (dis[x][y] , z);
dis[y][x]=min (dis[y][x] , z);
}
for (int k=1;k<=n;k++)
{
dis[k][k]=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
dis[i][j]=min (dis[i][j] , dis[i][k]+dis[j][k]);
}
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
cout << dis[i][j] << " ";
}
cout << "\n";
}
return 0;
}P4306 [JSOI2010] 连通数
本来可以用加一下优化的,但这是我一百年前写的代码,也是非常的复杂而且长得离谱:行
~~请不要学习哦,~~因为代码太长,选择的看,折叠框就不写{open}了
代码
#include <bits/stdc++.h>
#define int unsigned long long//神秘
using namespace std;
vector <int> g[2005];
vector <int> G[2005];
bitset <2005> p[2005];
int dfn[2005] , low[2005];
bool vis[2005];
int sz[2005] , in[2005] , scc[2005];
unsigned long long sum=0;
int n , tot=0 , id=0;
char P;
stack <int> st;
void tarjan (int now)//缩点
{
tot++;
dfn[now]=tot;
low[now]=tot;
vis[now]=1;
st.push (now);
for (auto i:g[now])
{
if (dfn[i]==0)
{
tarjan (i);
low[now]=min (low[now] , low[i]);
}
else if (vis[i]==1)
{
low[now]=min (low[now] , dfn[i]);
}
}
if (dfn[now]==low[now])
{
id++;
while (1)
{
int p=st.top ( );
st.pop ( );
vis[p]=0;
sz[id]++;
scc[p]=id;
if (p==now) break;
}
}
}
void bfs ( )//拓扑排序
{
queue <int> q;
for (int i=1;i<=id;i++)
{
if (in[i]==0)
{
q.push (i);
}
p[i][i]=1;
}
while (!(q.empty ( )))
{
int P=q.front ( );
q.pop ( );
for (auto i:G[P])
{
p[i]|=p[P];
in[i]--;
if (in[i]==0)
{
q.push (i);
}
}
}
}
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
cin >> P;
if (P=='1')
{
g[i].push_back (j);//建原图
}
}
}
for (int i=1;i<=n;i++)
{
if (dfn[i]==0) tarjan (i);//缩点
}
for (int i=1;i<=n;i++)//建新图
{
for (auto j:g[i])
{
if (scc[i]==scc[j]) continue;
G[scc[i]].push_back (scc[j]);
in[scc[j]]++;
}
}
bfs ( );//拓扑排序
for (int i=1;i<=id;i++)
{
for (int j=1;j<=id;j++)
{
sum+=(long long) p[i][j]*sz[i]*sz[j];计数
}
}
cout << sum;//输出
return 0;
}P6175 无向图的最小环问题
枚举三个点,在三个点互相能到且到,到,到不经过时,取
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int dis[105][105];
int a[105][105];
int n , m , u , v , w , minn=1e18;
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
memset (dis , 0x3f , sizeof (dis));
cin >> n >> m;
while (m--)
{
cin >> u >> v >> w;
dis[u][v]=dis[v][u]=a[u][v]=a[v][u]=min (dis[u][v] , w);
}
for (int k=1;k<=n;k++)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (i!=j&&a[i][k]&&a[k][j])
{
minn=min (minn , dis[i][j]+a[i][k]+a[k][j]);
}
dis[i][j]=min (dis[i][j] , dis[i][k]+dis[k][j]);
}
}
}
if (minn==1e18) cout << "No solution.";
else cout << minn;
return 0;
}