比赛成绩
T1 100
T2 100
T3 6
T4 20
T5 0
T6 9
T3 T639117 Int128 游乐园
错误原因
你的初始解法没有使用队列,而是直接遍历数组,导致逻辑不够清晰
在边界条件处理上也有问题(ans输出时需要+1)
错误代码
#include<bits/stdc++.h>
using namespace std;
int n,k,a[1005];
int main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int ans=0,r;
for(int i=1;i<=n;i++)
{
if(r+a[i]<=k)
{
r+=a[i];
}
else
{
ans++;
r=a[i];
}
if(i==n)
ans++;
}
cout<<ans;
return 0;
}正确思路
使用队列(或直接遍历)维护当前段的和。
若加入新元素后超过K,则开新段,计数器+1。
正确代码
#include<bits/stdc++.h>
using namespace std;
int n, k, ans;
queue<int> q;
int main(){
cin >> n >> k;
for(int i = 1; i <= n; i++){
int tmp;
cin >> tmp;
q.push(tmp);
}
int sum = 0;
while(!q.empty()){
if(sum + q.front() <= k){
sum += q.front();
q.pop();
}else{
ans++;
sum = 0;
}
}
cout << ans + 1;
return 0;
}T4 T639126 落落的去的数学问题2
错误原因
写的暴力
#include<bits/stdc++.h>
using namespace std;
long long n,a[300005],cnt=0;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
long long ans;
ans=(a[i]+a[j])%100000000;
cnt+=ans;
}
}
cout<<cnt;
return 0;
}正确思路
应为每个数都要加n-1次的,所以只要用for循环枚举a[i],sum+=a[i]*(n-1),再找第一个大于等于1e8-a[i]的数为pos,接着cnt+=n-pos+1,最后输出sum-cnt1e8
真确代码
#include<bits/stdc++.h>
using namespace std;
long long n,a[300005],cnt=0,sum;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
sum+=a[i]*(n-1);
int pos=lower_bound(a+i+1,a+n+1,100000000-a[i])-a;
cnt+=n-pos+1;
}
cout<<sum-cnt*100000000;
return 0;
}T5 T638485 第K小
错误原因
没时间写了
正确思路
维护一个最小堆,初始放入每个单独的元素。
每次取出堆顶(当前最小值),生成新和(堆顶 + 每个元素),若未出现过则入堆。
重复直到取出第K个元素。
正确代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll n,m,a[300005],cnt=0,sum,ans;
map<ll,bool>mp;
ll dfs(int ooo)
{
priority_queue<ll,vector<ll>,greater<ll> >pq;
for(int i=1;i<=n;i++)
{
if(mp[a[i]])
continue;
pq.push(a[i]);
mp[a[i]];
mp[a[i]]=1;
}
while(pq.size())
{
ans++;
ll t=pq.top();
if(ans==m)
return t;
pq.pop();
for(int i=1;i<=n;i++)
{
if(mp[t+a[i]])
continue;
mp[t+a[i]]=1;
pq.push(t+a[i]);
}
}
return 0;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
cout<<dfs(1);
return 0;
}T6 U591734 磁铁迷宫
错误原因
写的暴搜
错误代码
#include<bits/stdc++.h>
using namespace std;
int n,m,maxx=0,ans=0;
char a[10005][1005];
bool vis[1005][1005];
struct S{
int x,y;
};
int bx[4]={0,0,-1,1};
int by[4]={1,-1,0,0};
bool check(int x,int y)
{
return (x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0);
}
bool check1(int x,int y)
{
return (a[x+1][y]!='#'&&a[x-1][y]!='#'&&a[x][y+1]!='#'&&a[x][y-1]!='#');
}
void bfs(int ax,int ay)
{
queue<S>q;
q.push({ax,ay});
vis[ax][ay]=1;
while(q.size())
{
S u=q.front();
q.pop();
int f=0;
if(check1(u.x,u.y))
{
for(int i=0;i<4;i++)
{
int xx=u.x+bx[i];
int yy=u.y+by[i];
if(check(xx,yy))
{
f=1;
ans++;
vis[xx][yy]=1;
q.push({xx,yy});
}
}
}
}
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cin>>a[i][j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='.')
{
ans=1;
bfs(i,j);
maxx=max(maxx,ans);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
vis[i][j]=0;
}
}
}
}
cout<<maxx;
return 0;
}正确思路
标记所有与磁铁相邻的格子为障碍。
对剩余格子求最大连通块
正确代码
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e3 + 10;
int n, m, a[N][N];
int ans = 1;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int vis[N][N]; // vis[i][j] = color;
int cnt, color;
void dfs(int x, int y) {
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] == color || a[xx][yy] == 1)
continue;
vis[xx][yy] = color;
cnt++;
if (a[xx][yy] == 2) continue;
dfs(xx, yy);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == '#') a[i][j] = 1;
else a[i][j] = 0;
}
}
// Mark cells adjacent to walls as 2
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 1) continue;
if (a[i-1][j] == 1 || a[i+1][j] == 1 || a[i][j-1] == 1 || a[i][j+1] == 1) {
a[i][j] = 2;
}
}
}
// Find largest connected region of 0s
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (vis[i][j] == 0 && a[i][j] == 0) {
color++;
cnt = 1;
vis[i][j] = color;
dfs(i, j);
ans = max(ans, cnt);
}
}
}
cout << ans;
return 0;
}