T639104 信奥大联赛
简化题意
给定 个字符串 其中 表示为第 个字符串。和 个正整数,设正整数的和为 ,求 。
思路
1.模拟
- 依次读取每个字符串和对应的正整数,将正整数累加到 中。
- 对字符串数组进行排序。
- 输出 。
2.复杂度分析
- 时间复杂度:,主要是排序的时间复杂度。
代码实现
#include<bits/stdc++.h>
using namespace std;
int n,t;
string s[110];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>s[i];
int x;
cin>>x;
t+=x;
}
sort(s+1,s+1+n);
cout<<s[(t%n)+1];
return 0;
} T660516 来追梦运动会之慢跑赛
简化题意
给定变量 ,A选手以每秒 米的速度走 秒,然后休息 秒,B选手以每秒 米的速度走 秒,然后休息 秒。求在 秒内,谁跑得更远。
思路
1. 模拟
- 使用两个变量分别记录A、B两位选手的总距离。
- 使用循环模拟每一秒的状态,判断当前是行走还是休息。
- 在循环结束后,比较两位选手的总距离,输出结果。
2. 复杂度分析
- 时间复杂度:,其中 是比赛的总时间。
- 空间复杂度:。
代码实现
#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,f,x;
int main(){
cin>>a>>b>>c>>d>>e>>f>>x;
int cnt1=0,cnt2=0,sum1=0,sum2=0;
bool op1=1,op2=1;
while(x--){
if(sum1==a&&op1==1){
sum1=0;
op1=0;
}
else if(sum1==c&&op1==0){
sum1=0;
op1=1;
}
if(op1){
cnt1+=b;
sum1++;
}
else{
sum1++;
}
if(sum2==d&&op2==1){
sum2=0;
op2=0;
}
else if(sum2==f&&op2==0){
sum2=0;
op2=1;
}
if(op2){
cnt2+=e;
sum2++;
}
else{
sum2++;
}
}
if(cnt1>cnt2){
cout<<"Takahashi";
}
else if(cnt1<cnt2){
cout<<"Aoki";
}
else{
cout<<"Draw";
}
return 0;
} T642548 引爆炸弹
简化题意
给定一个 的矩阵,矩阵中的元素为 ‘0’ 或 ‘1’。每次操作可以选择一个位置 ,将该位置所在行和列的所有 ‘1’ 变为 ‘0’。求最少操作次数使得矩阵中所有元素都变为 ‘0’。
思路
1.模拟(DFS)
- 首先遍历整个数组,对每个 ‘1’ 进行深度优先搜索(DFS),将其所在行和列的所有的 ‘1’ 继续进行 DFS 操作。
- 每次进行 DFS 操作时,记录一次操作。
- 在 DFS 结束后,继续遍历数组,直到所有 ‘1’ 都被处理完。
- 输出结果。
2.复杂度分析
- 时间复杂度:,其中 是矩阵的行数, 是矩阵的列数。
- 空间复杂度:,用于存储矩阵。
代码实现
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
char c[1010][1010];
void dfs(int x,int y){
if(c[x][y]=='1'){
c[x][y]='0';
}
for(int i=1;i<=m;i++){
if(c[x][i]=='1') dfs(x,i);
}
for(int i=1;i<=n;i++){
if(c[i][y]=='1') dfs(i,y);
}
return;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>c[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(c[i][j]=='1'){
dfs(i,j);
cnt++;
}
}
}
cout<<cnt;
return 0;
}T642551 卡牌
简化题意
给定一个数组 ,连续自然数子序列的个数。
思路
1.贪心
- 首先对数组进行排序,然后重复遍历数组,直到元素全部清除。
- 每次遍历首先判断当前元素是否为前一个元素的 ,如果是,移除此元素并且计数器加一,否则跳过当前元素。
- 时间复杂度:,主要是排序的时间复杂度,会爆。
2.优化(map)
- 使用map记录每个元素出现的次数,然后遍历map,每次如果可以加入到连续子序列中,就将其map减一。
3.复杂度分析
- 时间复杂度:,主要是遍历map的时间复杂度。
- 空间复杂度:,用于存储map。
代码实现
#include<bits/stdc++.h>
using namespace std;
int t,a[200010];
int main(){
cin>>t;
while(t--){
int n;
cin>>n;
map<int,int> mp;
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]++;
}
int cnt=0,ans=n;
while(mp.size()){
int c=0;
for(auto x:mp){
c=x.first;
break;
}
mp[c]--;
if(mp[c]==0) mp.erase(c);
while(1){
c++;
if(mp.count(c)==0) break;
mp[c]--;
if(mp[c]==0) mp.erase(c);;
}
cnt++;
}
cout<<cnt<<'\n';
}
return 0;
}