Codeforces_474B算法
来源:互联网 发布:阿里云登录 编辑:程序博客网 时间:2024/06/11 04:46
#include <iostream>#include <algorithm>#include <string>#include <math.h>#include <vector>#include <bitset>using namespace std;struct ints{int number;int id;int group;};bool operator < (const ints& va1, const ints & va2){ return va1.number < va2.number;}class Comparers{public: bool operator () (const ints & va1, const ints & va2) { return va1.id < va2.id; }};int main(){int n, a, sums[100001], m;cin >> n;cin >> a;sums[0] = a;for (int i = 1; i < n; i++){cin >> a;sums[i] = sums[i - 1] + a;}cin >> m;ints t;vector<ints> ww;for (int i = 0; i < m; i++){cin >> t.number;t.id = i;t.group = 0;ww.push_back(t);}sort(ww.begin(), ww.end());int k = 0;for (int j = 0; j < m; j++){if (ww[j].number <= sums[k])ww[j].group = k + 1;else{while (ww[j].number > sums[k])k++;ww[j].group = k + 1;}}sort(ww.begin(), ww.end(), Comparers());for (int i = 0; i < m; i++){cout << ww[i].group << endl;}}
终于突破了474B,这道题一开始最简单的思路会导致m*n的时间复杂度,因此会卡死在test32上,一直time out。所以只能换思路。
换思路时,卡死在了struct的sort上。需要对struct排序咋办呢?参考了一下文章 http://blog.csdn.net/byrsongqq/article/details/5683597 及 http://www.jb51.net/article/36021.htm
搞定,比较爽。一次过。
关键是又突破了自我
0 0
- Codeforces_474B算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- 算法
- Python轻量级库web.py
- 杭州--4.13
- Two Sum(Java)
- (8)Java设计模式-代理模式(Proxy)
- STL与泛型编程-学习2-GeekBand
- Codeforces_474B算法
- Codeforces Round #308 (Div. 2) C.Vanya and Scales
- 用成员函数设计点类求两点之间的距离
- 用类求三角形的周长与面积
- swift 中使用扩展实现代理,让程序猿省心,让代码简洁
- MIT 6.828 学习笔记1 阅读boot.S
- LeetCode *** 300. Longest Increasing Subsequence
- Android快速开发系列 10个常用工具类
- 第3周 C语言及程序设计初步例程-40 穷举法解题