码迷,mamicode.com
首页 > 其他好文 > 详细

8.22 今日头条笔试

时间:2017-08-23 22:58:43      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:oid   指针   max   学习   count   opened   lambda表达式   test   close   

这次套路深啊,怎么还有改错题!。

上来看题,每个题目的输入数据都很大,果断上scanf,printf, 千万不能用cin,cout

1. 右上角的点,我的思路是先对每一行去重(题目好像说没有重的点耶!),每一行只有最右边的点才是候选点,然后对这些候选点按照y坐标,从大到小访问,要求相应的x是严格递增的,最后打印输出。

技术分享
 1 #include<bits/stdc++.h>
 2 #define pb push_back
 3 typedef long long ll;
 4 using namespace std;
 5 typedef pair<int, int> pii;
 6 const int maxn = 1e3 + 10;
 7 int n;
 8 void solve() {
 9     map<int, int> ma;
10     scanf("%d", &n);
11     int x, y;
12     for (int i = 0; i < n; i++) {
13         scanf("%d%d", &x, &y);
14         if(ma.count(y)) {
15             ma[y] = max(ma[y], x);
16         } else ma[y] = x;
17     }
18     vector<pii> res;
19     auto it = ma.rbegin();
20     x = it->second - 1;
21     //cout << it->first <<endl;
22     while(it != ma.rend()) {
23         if(it->second > x) {
24             res.pb({it->second, it->first});
25             x = it->second;
26         }
27         it++;
28     }
29     for (pii t : res) {
30      printf("%d %d\n", t.first, t.second);
31     }
32 }
33 
34 int main() {
35     freopen("test.in", "r", stdin);
36     //freopen("test.out", "w", stdout);
37     solve();
38     return 0;
39 }
View Code

2. 我认为是分治, 首先考虑数组的最小值,哪些区间可以取到取小值,取到最小值,使得结果最大,当然是和最大,那就是全部的数,接下来这个最小值就可以不考虑了,分别对左半和右半重复上面处理。

#include<bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 5e5 + 10;

int n;
ll a[maxn];
ll s[maxn];
ll f(int x, int y) {
    return s[y] - s[x - 1];
}
ll res = 0;
void work(int x, int y) {
    if(x > y) return;
    if(x == y) {
        res = max(res, a[x] * a[x]);
        return;
    }
    int p = x;
    for (int i = x; i <= y; i++) {
        if(a[i] < a[p]) {
            p = i;
        }
    }
    res = max(res, a[p] * f(x, y));
    work(x, p - 1);
    work(p + 1, y);
}
void solve() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf("%lld", &a[i]);
        s[i] = s[i - 1] + a[i];
    }
    work(1, n);
    printf("%lld\n", res);
}

int main() {
    freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    solve();
    return 0;
}

3. 题目描述的挺复杂,就是多个角度的优先队列,然后就试了试刚学习的函数类(仿函数,函数对象,函数指针),以及lambda表达式,写了一下。

最后过了80%, 我的是按时间模拟的,题目的范围是【1,3000】,我的for循环是1-3000,突然想到有些任务可能到3000的时候一直没有程序员空闲,所以for循环至少要到6000才行,这样才能ac吧。

感觉模拟到6000也不行。应该是任务队列判空为止才行。

 1 #include<bits/stdc++.h>
 2 #define pb push_back
 3 typedef long long ll;
 4 using namespace std;
 5 typedef pair<int, int> pii;
 6 const int maxn = 3e3 + 10;
 7 struct node {
 8     int id, p, s, pr, t;
 9 };
10 int n, m, p;
11 node a[maxn];
12 //这里扩大范围
13 int f[maxn * 2];
14 int res[maxn];
15 class cmp {
16 public:
17     bool operator()(const node&x, const node&y) {
18         if(x.pr == y.pr) {
19             if(x.t == y.t) {
20                 return x.s < y.s;
21             } else {
22                 return x.t < y.t;
23             }
24         } else {
25             return x.pr > y.pr;
26         }
27     }
28 };
29 set<node, cmp> data[maxn];
30 void solve() {
31     int x, y, x1, y1;
32     scanf("%d%d%d", &n, &m, &p);
33     for (int i = 1; i <= p; i++) {
34         scanf("%d%d%d%d", &x, &y, &x1, &y1);
35         a[i] = {i, x, y, x1, y1};
36     }
37     sort(a + 1, a + p + 1, [](const node&x, const node&y)->bool{return x.s < y.s;});
38     int cur = 0;
39     f[1] = m;
40     set<int> se;
41     x = 1;
42     int cnt = 0;
43     //这里应该模拟到6000, 保证所有的任务都有机会执行,或者更大。
44     for (int i = 1; i <= 3000; i++) {
45         cur += f[i];
46         while(x <= p) {
47             //cout << x << endl;
48             if(a[x].s == i) {
49                 data[a[x].p ].insert(a[x]);
50                 //cout << "asd " << a[x].id << endl;
51                 x++;
52                 cnt++;
53             } else break;
54         }
55         if(cur <= 0 || cnt == 0) continue;
56         int t = cur;
57 
58         for (int j = 0; j < t; j++) {
59             int id = -1;
60             int ti = 0;
61             for (int k = 1; k <= n; k++) {
62                 if(data[k].size() == 0)
63                     continue;
64                 else {
65                    // cout << k << " asd " << (data[k].begin())->t << endl;
66                     if(id == -1 || (data[k].begin())->t < ti) {
67                     id = k;
68                     ti = (data[k].begin())->t;
69                 }}
70             }
71             //cout << id << " " << ti << endl;
72             cur--;
73             cnt--;
74             node nd = *data[id].begin();
75             data[id].erase(data[id].begin());
76             res[nd.id] = i + nd.t;
77             f[i + nd.t]++;
78 
79             if(cnt <= 0 || cur <= 0) break;
80         }
81 
82     }
83     for (int i = 1; i <= p; i++)
84         printf("%d\n", res[i]);
85 }
86 
87 int main() {
88     freopen("test.in", "r", stdin);
89     //freopen("test.out", "w", stdout);
90     solve();
91     return 0;
92 }

 

8.22 今日头条笔试

标签:oid   指针   max   学习   count   opened   lambda表达式   test   close   

原文地址:http://www.cnblogs.com/y119777/p/7420563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!