Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Knapsack DP is harder than FFT.
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
- #define ff first
- #define ss second
- #define pb emplace_back
- #define AI(x) begin(x),end(x)
- template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
- template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
- // 建議把 N 最大值這種常數設成一個 const
- const int N = 1e5 + 225;
- ll num[N];
- ll bit[N];
- ll suffix(ll x){
- ll sum = 0;
- for(ll i = x; i > 0; i -= i & -i)
- sum += bit[i];
- return sum;
- }
- // update() 沒有要回傳,
- // 記得用 void !
- void update(ll y){
- // 反正都開滿了就跑滿也沒有差
- // 不用特別維護 maxx
- for(ll i = y; i < N; i += i & -i)
- ++bit[i];
- }
- signed main(){
- ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- ll n, numOfTest = 1;
- // 這裡用 && 比較保險
- // 因為逗號只會回傳後面那一個
- // 有時候沒有輸入成功還是會繼續執行喔
- while(cin >> n && n){
- fill(bit, bit + N, 0);
- // num 因為有點大宣告在外面
- for(ll i = 0; i < n; ++i){
- // 盡量不要混用 cin cout 和 scanf printf
- // 哪天出事了會死不瞑目
- cin >> num[i];
- }
- map<ll, ll> mp;
- set<ll> st;
- ll rank = 1;
- for(ll i = 0; i < n; ++i) st.insert(num[i]);
- // 下面這行也可以指定型態喔
- // 不一定要用 auto
- for(ll x: st) mp[x] = rank++;
- for(ll i = 0; i < n; ++i) num[i] = mp[num[i]];
- ll ans = 0;
- for(ll i = 0; i < n; ++i){
- ans += i - suffix(num[i]);
- update(num[i]);
- }
- // 注意輸出格式!
- cout << "Case #" << numOfTest++ << ": " << ans << '\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement