Codeforces Round #775 (Div. 2)
A - Game
- 签到题~
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+5;
int a[N];
void solve()
{
int n,m;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
int l=1, r=n;
while(a[l] && l<=n) l++;
while(a[r] && r>=1) r--;
int ans=r-l+2;
if(ans>0) cout<<ans<<"\n";
else cout<<0<<"\n";
}
signed main()
{
int t=1;
cin>>t;
while(t--) solve();
}
B - Game of Ball Passing
分析:
-
很有意思的一道题,类似于消消乐,思维
只需考虑最大的即可
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N];
void solve()
{
int n,m;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+1+n);
reverse(a+1,a+1+n);
int tmp=a[1];
if(tmp==0) { cout<<0<<"\n"; return;}
for(int i=2;i<=n;i++)
{
tmp-=a[i];
}
if(tmp<=0) cout<<"1\n";
else cout<<tmp<<"\n";
}
signed main()
{
int t=1;
cin>>t;
while(t--) solve();
}
C - Weird Sum
分析:
- 模拟,曼哈顿距离求和化简
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int a[N];
vector <int> gx[N], gy[N];
void solve()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int x; cin>>x;
gx[x].push_back(i);
gy[x].push_back(j);
}
}
int ans=0;
for(int i=1;i<=100000;i++)
{
if(gx[i].size())
{
sort(gx[i].begin(),gx[i].end());
sort(gy[i].begin(),gy[i].end());
for(int j=0;j<gx[i].size();j++)
{
ans+=j*(gx[i][j]+gy[i][j]);
ans-=(gx[i].size()-j-1)*(gx[i][j]+gy[i][j]);
}
gx[i].clear(); gy[i].clear();
}
}
cout<<ans<<"\n";
}
signed main()
{
int t=1;
//cin>>t;
while(t--) solve();
}
D - Integral Array
分析:
- 差分判断某一区间有无某个数出现(小技巧)
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
int a[N], s[N];
set <int> st;
void solve()
{
int n,c;
scanf("%lld%lld",&n, &c);
st.clear();
for(int i=1;i<=c;i++) a[i]=0;
for(int i=1;i<=n;i++)
{
int x; scanf("%lld",&x);
if(x<=c) a[x]=1;
}
if(!a[1]) { cout<<"No\n"; return ;}
for(int i=1;i<=c;i++)
{
s[i]=s[i-1]+a[i]; //小技巧
}
for(int x=2;x<=c;x++)
{
if(!a[x]) continue;
for(int i=2;i*x<=c;i++)
{
int r=(i+1)*x-1;
if(r>c) r=c;
if(s[r]-s[i*x-1]>0)
{
if(a[i]==0)
{
cout<<"No\n"; return ;
}
}
}
}
cout<<"Yes\n";
}
signed main()
{
int t=1;
cin>>t;
while(t--) solve();
}