Google CodeJam Round 2 2013

Official Analysis: https://code.google.com/codejam/contest/2442487/dashboard#s=a


C++ code Problem A (small data)

#include <fstream>
#include <vector>
#include <utility>
#include <algorithm>
#include <stack>

using namespace std;

int main() {
        ifstream fin("A-small-attempt0.in");
        ofstream fout("A-small-attempt0.out");
        int T;
        fin >> T;
        for (int casenum = 1; casenum <= T; ++casenum) {
                int N, M;
                fin >> N >> M;
                int cost = 0;
                vector<pair<int,int>> vec;
                for (int i = 0; i < M; ++i) {
                        int o, e, p;
                        fin >> o >> e >> p;
                        int n = e - o;
                        cost += (n == 0 ? 0 : n * N - n * (n - 1) / 2) * p;
                        vec.push_back(make_pair(o, -p));
                        vec.push_back(make_pair(e, p));
                }
                sort(vec.begin(), vec.end());
                int n = 2 * M;
                stack<pair<int,int>> stk;
                int total = 0;
                for (int i = 0; i < n; ++i) {
                        if (vec[i].second < 0)
                                stk.push(make_pair(vec[i].first, -vec[i].second));
                        else {
                                int t = vec[i].second;
                                while (t > 0) {
                                        int num = stk.top().second;
                                        if (t >= num) {
                                                t -= num;
                                                int k = vec[i].first - stk.top().first;
                                                total += (k == 0 ? 0 : k * N - k * (k - 1) / 2) * num;
                                                stk.pop();
                                        } else {
                                                int j = stk.top().first;
                                                stk.pop();
                                                int k = vec[i].first - j;
                                                total += (k == 0 ? 0 : k * N - k * (k - 1) / 2) * t;
                                                num -= t;
                                                stk.push(make_pair(j, num));
                                                t = 0;
                                        }
                                }
                        }
                }
                int ans = (cost - total) % 1000002013;
                fout << "Case #" << casenum << ": " << ans <<'\n';
        }
        fin.close();
        fout.close();
        //system("pause");
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值