AtCoder Beginner Contest 351
A
easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #include<bits/stdc++.h> using ll=long long; using namespace std; int main() { ll sum=0; ll ans=0; for(int i=1;i<=9;i++) { ll x; cin>>x; sum+=x; } for(int i=1;i<=8;i++) { ll x; cin>>x; ans+=x; } cout<<max(0ll,sum-ans+1); return 0; }
|
B
easy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include<bits/stdc++.h> using ll=long long; using namespace std; char a[102][102]; int main() { int n; cin>>n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>a[i][j]; } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { char x; cin>>x; if(x!=a[i][j]) { cout<<i<<' '<<j; return 0; } } } return 0; }
|
C
用一个栈模拟
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| #include<bits/stdc++.h> using ll=long long; using namespace std; stack<int>a; int main() {
int n; cin>>n; for(int i=1;i<=n;i++) { int x; cin>>x; if(a.empty()) { a.push(x); continue; } if(x==a.top()) { while(!a.empty()&&x==a.top()) { a.pop(); x++; } a.push(x); } else { a.push(x); } } cout<<a.size(); return 0; }
|
D
dfs,不过有一个地方就是走过的点那他肯定不是最优的,就直接Vis掉。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include<bits/stdc++.h> #pragma GCC optimize("O1,O2,O3,O4,O5,Og,Os,Ofast,inline") using namespace std; int n,m; struct point { int x,y; }; int dx[] = {0,0,1,-1}; int dy[] = {1,-1,0,0}; char ch[1005][1005],vis[1000005],vis2[1000005]; queue<point> q; vector<int> x_,y_; int bfs(int x,int y) { point p = {x,y}; q.push(p); vis[p.x*1000+p.y-1] = 1,x_.push_back(p.x),y_.push_back(p.y); int ans = 0; while(!q.empty()) { point tmp = q.front(); q.pop(); ans++; if(ch[tmp.x][tmp.y-1]=='#'||ch[tmp.x-1][tmp.y]=='#'||ch[tmp.x][tmp.y+1]=='#'||ch[tmp.x+1][tmp.y]=='#'||ch[tmp.x][tmp.y]=='#') continue; vis2[tmp.x*1000+tmp.y-1] = 1; for(int i = 0; i<4; i++) { point np = tmp; np.x+=dx[i]; np.y+=dy[i]; if(1<=np.x&&np.x<=n&&1<=np.y&&np.y<=m&&!vis[np.x*1000+np.y-1]&&!vis2[np.x*1000+np.y-1]) q.push(np),vis[np.x*1000+np.y-1] = 1,x_.push_back(np.x),y_.push_back(np.y); } } for(int i = x_.size()-1;i>=0;i--) vis[x_[i]*1000+y_[i]-1] = 0; x_.clear(); y_.clear(); return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; int ans = 0; for(int i = 1; i<=n; i++) for(int j = 1; j<=m; j++) cin >> ch[i][j],ans = ans||(ch[i][j]=='.'); for(int i = 1; i<=n; i++) for(int j = 1; j<=m; j++) if((!(ch[i][j-1]=='#'||ch[i-1][j]=='#'||ch[i][j+1]=='#'||ch[i+1][j]=='#'||ch[i][j]=='#'))&&!vis2[i*1000+j-1]) ans = max(ans,bfs(i,j)); cout << ans; return 0; }
|
F
两颗树状数组,一个求比他小的个数,一个求比他小的个数之和
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| #include<bits/stdc++.h> #define int int64_t
using namespace std;
struct Fenwick { vector<int> tree; int size; explicit Fenwick(int size) : size(size) { tree.resize(size + 1, 0); }
void update(int pos, int value) { while (pos <= size) { tree[pos] += value; pos += pos & -pos; } }
int query(int left, int right) { return query(right) - query(left - 1); }
int query(int pos) { int res = 0; while (pos > 0) { res += tree[pos]; pos -= pos & -pos; } return res; } };
void solve() { int n; cin >> n; vector<int> a(n); vector<int> b(n); map<int, int> map;
for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; }
sort(b.begin(), b.end()); b.erase(unique(b.begin(), b.end()), b.end()); for (int i = 0; i < b.size(); ++i) { map[b[i]] = i + 1; }
Fenwick sum(b.size()); Fenwick count(b.size());
int result = 0;
for (int i = n - 1; i >= 0; --i) { int hz = map[a[i]];
int sum_g = sum.query(hz + 1, b.size()); int cnt_g = count.query(hz + 1, b.size());
result += sum_g - a[i] * cnt_g;
sum.update(hz, a[i]); count.update(hz, 1); }
cout << result << "\n"; }
int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
|