欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > 每日一题2024年8月30日

每日一题2024年8月30日

2024/10/25 7:32:46 来源:https://blog.csdn.net/2303_79552392/article/details/141723360  浏览:    关键词:每日一题2024年8月30日

E-不知道叫什么名字_2023年广东工业大学腾讯杯新生程序设计竞赛 (nowcoder.com)

大致思路:题目要求选择连续的,男女人数相等的。

题目给出 男生为1,女生为0,这里猜测要把女生的0改成-1(猜测,不知道对不对)
通过一些手段,前缀和+map,发现可以完美解决这个问题。

#include <bits/stdc++.h>using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define lc u << 1
#define rc u << 1 | 1void solve();const int N = 1e6 + 10;
const int NN = 1e5;signed main() {IOS;ll t = 1;while (t--)solve();return 0;
}void solve() {ll n;cin >> n;vector<ll> a(n+1);for(int i = 1; i <= n; ++ i){cin >> a[i];if(a[i] == 0){a[i] = -1;}}vector<ll> sum(n+1);for(int i = 1; i <= n; ++ i){sum[i] = a[i] + sum[i-1];}ll ans = 0;map<ll,ll> mp;mp[0] = 0;//最后一个是0的时候,这种情况没办法解决for(int i = 1; i <= n; ++ i){if(mp.find(sum[i]) != mp.end()){continue;}mp[sum[i]] = i;}for(int i = 1; i <= n; ++ i){ans = max(ans,i - mp[sum[i]]);}cout << ans << endl;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com