AtCoder Beginner Contest 346
A - Adjacent Product
思路:
b[i] = a[ i ] * a[ i+1 ]
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
void solve(){
int n;
cin>>n;
std::vector<int> a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
std::vector<int> b(n);
for(int i=1;i<n;i++){
b[i]=a[i]*a[i+1];
}
// for(auto x:b+1){
for(int i=1;i<n;i++){
cout<<b[i]<<" ";
}
cout<<endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}
B - Piano
思路
模拟一下,看数据量构造一个长度位2000的字符串在遍历一下就可以了
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
void solve(){
int w,b;
cin>>w>>b;
string a="wbwbwwbwbwbw";
string s=" ";
while(s.size()<=2000){
s+=a;
}
std::vector<int> n1(2010),n2(2010);
for(int i=1;i<s.size();i++){
n1[i]=n1[i-1]+(s[i]=='w');
n2[i]=n2[i-1]+(s[i]=='b');
}
for(int i=1;i<=s.size();i++){
for(int j=i+1;j<=s.size();j++){
if((n1[j]-n1[i]==w)&&(n2[j]-n2[i]==b)){
cout<<"Yes"<<endl;
return ;
}
}
}
cout<<"No"<<endl;
return ;
}
signed main(){
int t=1;
while(t--){
solve();
}
return 0;
}
C - Σ
思路:
map和数组简单的应用一下就可以了,感觉很好想到
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
void solve(){
int n,k;
cin>>n>>k;
std::map<int, int> mp;
std::vector<bool> st(n+1);
int ans=(k+1)*k/2;
std::vector<int> a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
if(mp[a[i]]==0){
mp[a[i]]=i;
}
if(a[i]<=k&&a[i]>=1){
if(st[mp[a[i]]]){
continue;
}
else{
st[mp[a[i]]]=true;
ans-=a[i];
}
}
}
cout<<ans<<endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}
D - Gomamayo Sequence
思路
处理+贪心
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin()+1,x.end()
const int INF = 1e18;
const int N = 2e5+10;
int fst[N][2];
int lst[N][2];
void solve(){
int n;
cin>>n;
string s;
cin>>s;
s=" "+s;//处理一下字符串
std::vector<int> a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for (int i = 1; i <= n; i++) {
fst[i][0] = fst[i - 1][1] + a[i] * (s[i] != '0');
fst[i][1] = fst[i - 1][0] + a[i] * (s[i] != '1');
}
for (int i = n; i >= 1; i--) {
lst[i][0] = lst[i + 1][1] + a[i] * (s[i] != '0');
lst[i][1] = lst[i + 1][0] + a[i] * (s[i] != '1');
}
int ans = INF;//记录最小次数
for (int i = 1; i < n; i++) {
int now = a[i] * (s[i] != '0') + a[i + 1] * (s[i + 1] != '0');
now += fst[i - 1][1] + lst[i + 2][1];
ans = min(ans, now);
}
for (int i = 1; i < n; i++) {
int now = a[i] * (s[i] != '1') + a[i + 1] * (s[i + 1] != '1');
now += fst[i - 1][0] + lst[i + 2][0];
ans = min(ans, now);
}
cout << ans << endl;
return ;
}
signed main(){
int t=1;
while(t--) solve();
return 0;
}
标签:std,AtCoder,Beginner,int,lst,long,346,ans,define
From: https://www.cnblogs.com/du463/p/18102197