c++ - 如何找到我的 Codechef 解决方案中的缺陷
问题描述
基本问题是找到一个数组的最大子段,该数组在输入中没有至少一个 k 值。
#include<bits/stdc++.h>
using namespace std ;
#define ll long long int
void solve()
{
// here the approach is to check from start and keep checking, it take O(n^2) approach but it
// should satisfy at least the sub-task but I am getting that too wrong, i.e only one of the
// two sub-task is getting AC while other WA.
ll i, j, n, k, ans=0, flag=0 ;
scanf("%lld %lld", &n, &k) ;
ll a[n+1] ;
set <int> s ;
for(i=0 ;i<n ;i++)
{
scanf("%lld", &a[i]) ;
}
for(i=0 ;i<n ;i++) // this is to start searching for the longest subsegment from start
{
s.insert(a[i]) ; // this to keep the count of no. of flavours occuring
for(j=i+1 ;j<n ;j++) // to continue checking
{
if(s.size()<k && flag==0)
{
s.insert(a[j]) ;
if(s.size()==k) // if after adding it becomes that condition the size would be one less
{
ans=max(ans,j-i) ;
flag=1 ;
}
else
ans=max(ans,j-i+1) ;
}
else
{
s.clear() ;
break ;
}
}
flag=0 ;
}
printf("%lld\n", ans);
}
int main()
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
ll t ;
scanf("%lld", &t) ;
while (t--)
{
solve() ; // solving no. of test cases
}
return 0 ;
}
解决方案
推荐阅读
- python - 如何将海关类型数组传递给 PostgreSQL 函数
- visual-studio-code - 在 VS Code 编辑器窗口中的行号和代码之间添加左边的空格/边距/填充
- c++ - 为什么循环索引会超出循环范围
- android - 将 .aar 文件添加到 android studio 中的 java 模块(子项目)
- terraform - 在 Terraform 0.11.x 模板数据源中转义控制关键字
- parallels - 在 Parallels for Mac 上的 Windows 中重新映射本地主机
- css - CSS动画轮播在幻灯片之间暂停
- android - 我们如何为我们的应用程序编写自定义 lint 规则?
- node.js - SequelizeDatabaseError:“名称”列中的空值违反非空约束(NodeJS + Sequelize + Postgres)
- java - 除非明确设置资源目录,否则从 Idea 运行找不到任何资源