总结
习题课(兴趣小组)
Not Adjacent Matrix
题意
将一个的格子分别填上的数,使相邻的两个格子中的数不相邻。如果可以做到,输出矩阵,否则输出
知识点
构造
正解
设行为,列为。现将的格子依次填 上数字,剩余的接着填。
注意
是特例,无法做到,输出
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
int a[105][105];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
memset (a , 0 , sizeof (a));
cin >> n;
if (n==2)
{
cout << -1 << "\n";
continue;
}
int now=0;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if ((i+j)%2==0)
{
now++;
a[i][j]=now;
}
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (a[i][j]==0)
{
now++;
a[i][j]=now;
}
cout << a[i][j] << " ";
}
cout << "\n";
}
}
return 0;
}Same Differences
题意
求有几对满足
知识点
方程,
正解
化简:,统计每个只出现多少次,再每个的数量选出两个的方案数的总和
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
map <int , int> a;
int Q , n , v;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
a.clear ( );
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> v;
a[v-i]++;
}
long long cnt=0;
for (auto i:a)
{
cnt+=(long long) i.second*(i.second-1)/2;
}
cout << cnt << "\n";
}
return 0;
}Arranging The Sheep
题意
每个*可以往左边或者右边的.挪一个。求至少要挪几下才能使所有的*连在一起
知识点
贪心
正解
有个*那么所有的*全部往第点靠拢,再计算即可
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt[1000005];
int Q , n;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> s;
s=" "+s;
int tot=0;
for (int i=1;i<=n;i++)
{
if (s[i]=='*')
{
cnt[++tot]=i;
}
}
long long ans=0 , S=(tot+1)/2;
int res=cnt[S];
for (int i=1;i<=S-1;i++)
{
ans+=(res-(S-i))-cnt[i];
}
for (int i=S+1;i<=tot;i++)
{
ans+=cnt[i]-(res+(i-S));
}
cout << ans << "\n";
}
return 0;
}Corrupted Array
题意
先有一个数组,有个数下标分别为,=,为随机抽取。将打乱得到数组。已知数组,求任意一个可能的数组
知识点
思想,
正解
只可能为个数中,所以,最后判断减去两倍后是否有剩下的那个数
错因
我好像没错
心得
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[200005];
map <int , int> mp1 , mp2;
int Q , n;
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
mp1.clear ( );
mp2.clear ( );
cin >> n;
n+=2;
long long sum=0;
for (int i=1;i<=n;i++)
{
cin >> a[i];
sum+=a[i];
}
sort (a+1 , a+n+1);
for (int i=1;i<=n-1;i++)
{
mp1[a[i]]++;
}
for (int i=1;i<=n-2;i++)
{
mp2[a[i]]++;
}
mp2[a[n]]++;
if (mp1.count (sum-a[n]*2))
{
mp1[sum-a[n]*2]--;
for (auto i:mp1)
{
for (int j=1;j<=i.second;j++)
{
cout << i.first << " ";
}
}
}
else if (mp2.count (sum-a[n-1]*2))
{
mp2[sum-a[n-1]*2]--;
for (auto i:mp2)
{
for (int j=1;j<=i.second;j++)
{
cout << i.first << " ";
}
}
}
else
{
cout << -1 << " ";
}
cout << "\n";
}
return 0;
}Permutation by Sum
题意
构造一个的排列使,无法构造输出
知识点
思想
正解
毕竟,,完全可以用的方法。如果填最小的还是多了或者填最大的还是小了,输出,其他情况都能满足。先将中的每一个数分别,次操作,每次操作最后一个满足条件的数,就,最后将没出现的数分配到两边即可
错因
两边都要来一次,我的数没增加。发了,这时:比赛已经结束了
心得
应该要写出来的
复杂度
- 时间复杂度:
- 空间复杂度:
代码
#include <bits/stdc++.h>
using namespace std;
bool vis[505];
int b[505];
int Q , n , l , r , s , tot;
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> Q;
while (Q--)
{
cin >> n >> l >> r >> s;
long long sum=0;
for (int i=l;i<=r;i++)
{
b[i]=i-l+1;
sum+=b[i];
}
if (sum>s)
{
cout << -1 << "\n";
}
else
{
int now=r;
int len=r-l+1;
while (sum<s)
{
if (b[now]==n-(r-now)) now--;
if (now<l) break;
b[now]++;
sum++;
}
if (sum==s)
{
memset (vis , 0 , sizeof (vis));
for (int i=l;i<=r;i++)
{
vis[b[i]]=1;
}
int tot=1;
for (int i=1;i<=l-1;i++)
{
while (vis[tot]==1)
{
tot++;
}
cout << tot << " ";
tot++;
}
for (int i=l;i<=r;i++)
{
cout << b[i] << " ";
}
for (int i=r+1;i<=n;i++)
{
while (vis[tot]==1)
{
tot++;
}
cout << tot << " ";
tot++;
}
cout << "\n";
}
else
{
cout << -1 << "\n";
}
}
}
return 0;
}