7.19



题目链接


H:GRE阅读题

#include<cstdio>
#include<iostream>
using namespace std;
int num[] = { 5, 21, 12, 2, 1, 4, 6, 1, 4, 4, 1, 0,1,1 };
int main()
{
    int n;cin >> n;
    cout << num[n] << endl;
    return 0;
}

Uva11054
题意;直线上有n个村庄,每个村庄一个a[i],如果大于0,则为买酒,如果小于0,则为卖酒,k个单位的就从一个村庄搬到另一个村庄需要k个劳动力,计算最少需要多少劳动力满足供需平衡

解法:考虑最左边的村庄,如果需要买酒,就一定需要劳动力从村庄2给他运酒,那么就需要劳动力a[i],那么对于第二个村庄的需求就变成了a[1]+a[2],一次类推,绝对值的解释放到了代码里面。

//想想a[i]一直为正的时候就好想了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;

int main() {
    int n;
    while (cin >> n&&n) {
        ll ans = 0, a, last=0;
        for (int i = 0; i < n; i++) {
            cin >> a;
            //假如第一个村庄需求为负,传过来之后需要加绝对值,
            //之后再读入数据,如果为正,两个抵消,还多出来的部分表示还需要下一家支持
            //也就需要那么多的劳动力
            ans += (abs(last));
            last += a;//表示当前需求
        }
        cout << ans << endl;
    }
    return 0;
}

Uva11572
题意:输入一个子序列A,长度为n,找到一个尽量长的子序列Al和Ar,使得该序列中没有重复的元素

解法:滑窗问题,先一直推右边,当右边不能推的时候,就一直删左边的元素,知道把那个重复的元素删掉

#include<cstdio>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
ll a[maxn];
set<ll>s;

int main() {
    int n, T;
    cin >> T;
    while (T--) {
        s.clear();
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }
        int l = 1, r = 1, ans = 0;
        while (r <= n) {
            while (r<=n&&s.find(a[r]) == s.end()) { s.insert(a[r++]); }
            ans = max(r - l, ans);
            s.erase(a[l++]);
        }
        cout << ans << endl;
    }
    return 0;

}
原文地址:https://www.cnblogs.com/romaLzhih/p/9489806.html