16.7.18

[STL]set转载

维护集合信息,集合内各元素均不相同,支持插入、删除、查询等操作,单次操作复杂度为O(logN)。
set能根据待定的排序准则自动将元素排序。默认比较方式是less。
构造set集合主要目的是为了快速检索,不可直接去修改键值。

常用操作:

1.元素插入:insert()

set<int> S;
S.insert(13); //插入元素

2.按序遍历:用迭代器遍历

set<int> S;
set<int>::iterator it;
for (it = S.begin(); it != S.end(); ++it) // 不支持it+=3这样的操作
cout << *it << " "; // 递增输出,如1 3 4 6

3.反向遍历:利用反向迭代器reverse_iterator

    set < int > s;
    set< int >::reverse_iterator rit;
    for(rit = s.rbegin(); rit != s.rend(); rit++)

4.元素删除:erase()

        set< int > s;
        s.erase(2);        //删除键值为2的元素
        s.clear();         //清空集合元素

5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素的后一个位置。

            set<int> s;
            set<int>::iterator it;
            it=s.find(5);              //查找键值为5的元素
            if(it!=s.end())            //找到
                cout<<*it<<endl;
            else                       //未找到
                cout<<"未找到";

6.自定义比较函数

(1)元素不是结构体, 可自定义比较函数myComp,重载“()”操作符:

        struct myComp
        {
            bool operator()(const your_type &a,const your_type &b)
            {
                return a.data > b.data;
            }
        }
        set<int,myComp>s;
        set<int,myComp>::iterator it;

(2)如果元素是结构体,可以直接将比较函数写在结构体内。

        struct Info
        {
            string name;
            float score;
            //重载“<”操作符,自定义排序规则
            bool operator < (const Info &a) const
            {
                //按score从大到小排列
                return a.score<score;
            }
        }
        set<Info> s;
        set<Info>::iterator it;

7.返回某个值元素的个数count()

8.lower_bound() 和 upper_bound()

set<int> S;
set<int>::iterator it;
it = S.lower_bound(13); // 返回第1个小于等于13的元素的迭代器
if (it != S.begin())
--it; // 返回第1个小于13的元素的迭代器
it = S.upper_bound(13); // 返回第1个大于13的元素的迭代器
if (it != S.begin())
--it; // 返回第1个小于等于13的元素的迭代器

queue
通常用于spfa中,可以避免写循环队列(即一个点可能入队多次)。

    // 最短路,spfa
    int dis[N];
    bool vis[N];
    vector<pair<int, int> > edge[N]; // 邻接表
    queue<int> que;
    void bfs(int start) {
        for (int i = 1; i < N; ++i)
        dis[i] = INF, vis[i] = false;
        dis[start] = 0, vis[start] = true, que.push(start);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            for (int i = 0; i < edge[u].size(); ++i) {
                int v = edge[u][i].first, d = edge[u][i].second;
                if (dis[v] > dis[u] + d) {
                    dis[v] = dis[u] + d;
                    if (!vis[v])
                    vis[v] = true, que.push(v); // 进队列
                }
            }
            vis[u] = false; // 出队列
        }
    }   

vector
存数据很方便,并且常用于数据离散化。

vector<int> e[N]; // 邻接表

for (int i = 0; i < m; ++i) { // 假设有m条边
    int u, v;
    scanf("%d%d", &u, &v);
    e[u].push_back(v); // push_back()将元素添加到容器尾部
    e[v].push_back(u);
}

for (int i = 0; i < e[u].size(); ++i) { // 遍历与u有连边的点
    int v = e[u][i];
    cout << v << " ";
}

    //邻接表的数组实现
    int et, head[N]; // head[]需要赋初值-1
    struct Edge {
        int t, d, nxt;
    } e[M * 2]; // M为边数,无向边需要*2

    void addEdge(int s, int t, int d) { // s -> t, 权值为d
    e[et].t = t, e[et].d = d, e[et].nxt = head[s], head[s] = et++;
    // 若为有向边,则这句去掉
    e[et].t = s, e[et].d = d, e[et].nxt = head[t], head[t] = et++;

    for (int i = head[u]; i != -1; i = e[i].nxt) { // 遍历与u有连边的点
        int v = e[i].t, d = e[i].d;
        cout << v << " ";
    }

//今日代码
2016-07-18 personal training V2

A. Dubstep

(从字符串中删去多个指定字符串)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#define LL long long
using namespace std;

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    string s;
    cin >> s;
    for(int i = 0; i < s.length() ; i++)
    {
        if(s[i] == 'W' && s[i+1] == 'U' && s[i+2] == 'B')
        {
            s[i] = '0';
            s[i+1] = '0';
            s[i+2] = '0';
            i+=2;
        }
    }

     int i = 0;
    while(s[i++] == '0');

    int j = s.length()-1;
    while(s[j--] == '0');


    for(i = i-1;i<= j+1;i++)
    {
        if(s[i]!='0') cout << s[i];
        else
        {
            cout <<" ";
            while(s[i++]=='0');
            i=i-2;

        }
    }

    return 0;   
}

D. Prizes, Prizes, more Prizes

(贪心。每次都从中尽量减去prize中最大的值,一次可能减去多次。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<map>
#include<algorithm>
#include<cmath>
#include<cstring>
#define LL __int64
using namespace std;

LL arr[55];
LL prize[10];
LL get[10];

bool compare(LL a,LL b)
{
    return a > b;
}

int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    cin >> n;
    for(int i = 0 ; i < n; i++)
        scanf("%I64d",&arr[i]);

    for(int i = 0; i < 5; i++)
        scanf("%I64d",&prize[i]);


    LL sum = 0;
    for(int i = 0; i < n; i++)
    {
        sum+=arr[i];
        for(int j = 4; j >= 0; j--)
            if(sum >= prize[j])
            {
                get[j]+=sum/prize[j];
                sum %= prize[j];
            }
    }

    cout << get[0];
    for(int i = 1; i < 5; i++)
        printf(" %I64d",get[i]);
    printf("\n");
    printf("%I64d\n",sum);

    return 0;   
}

F. System of Equations

(枚举。a,b都不为负数,一定小于m,n中的最小值。)

#include<iostream> 
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#define LL long long 
using namespace std;


int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n,m;
    int a,b;

    cin >> n >> m;
    int x = min(n,m);

    int cnt = 0;
    for(a = 0; a <= x; a++)
        for(b = 0; b <=x ;b++)
        {
            if(a*a+b == n && a+b * b ==m) cnt++;
        }

    cout << cnt << endl;

    return 0;   
}

G. Hometask

(从给定的一串数字中拼凑出最大的可以被2,3,5整除的数。能被2,5同时整除则尾数一定是0,所以所给数字不存在0时直接输出-1。能被3整除则各个位上数字和也能被3整除。若能从中找到一个数字%3的值和sum%3值相等,则删去这个数字就得到答案。若找不到,删去两个最小的%3不为零的数则为答案,因为比如sum%3=1,此时存在%3不为0的数则一定%3=2,删去两个即可;sum%3=2时同理。)

#include<cstdio>
#include<iostream> 
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<cstring>
#define LL long long 
using namespace std;

int cnt[100086];
int num; 
int sum;

void check()
{
    if(sum == 0) cnt[0] = 1; //避免多个0
    for(int i= 9; i >= 0; i--)
        while(cnt[i]--) printf("%d",i);
    printf("\n");
}
int main()
{
    freopen("xx.in","r",stdin);
    freopen("xx.out","w",stdout);

    int n;
    scanf("%d",&n);

    for(int i = 0; i < n; i++)
    {
        scanf("%d",&num);
        sum+=num;
        cnt[num]++;     
    }   


    if(cnt[0] == 0) //no 0 
    {
        cout << -1<< endl;
        return 0;
    }
    else if(sum%3 == 0) //no delete 
    {
        check();
        return 0;
    }
    else //delete
    {
        for(int i = 0; i <= 9; i++) 
            if(cnt[i] && i%3 == sum%3) //delete1
            {
                sum-=i;
                cnt[i]--;
                break;
            }

        if(sum%3 == 0) 
        {
            check();
            return 0;
        }

            //delete2
            int t=2;
            while(t--)
            {
                for(int i = 0; i <=9 && sum%3; i++)
                    if(cnt[i] && i%3)
                    {
                        sum-=i;
                        cnt[i]--;
                    }
            }

        if(sum%3) printf("-1\n");
        else check();

        return 0; 
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1985年1月至2005年12月,原油现货交易价格如下。数据为:26.41 26.73 28.29 27.63 27.84 26.87 27.12 28.08 29.08 30.38 29.75 26.3 18.83 13.26 10.42 13.34 14.3 12.78 11.15 15.9 14.77 15.27 15 17.94 18.75 16.6 18.83 18.73 19.38 20.29 21.37 19.73 19.59 19.96 18.51 16.7 16.94 16.01 17.08 17.99 17.51 15.16 16.31 15.18 13.37 13.58 15.32 17.24 17.03 18.15 20.19 20.42 19.9 20.27 18.31 18.83 20.13 19.94 19.89 21.82 22.68 21.54 20.28 18.54 17.4 17.07 20.69 27.32 39.51 35.23 28.85 28.44 21.54 19.16 19.63 20.96 21.13 20.56 21.68 22.26 22.23 23.37 21.48 19.12 18.9 18.68 19.44 20.85 22.11 21.6 21.87 21.48 21.71 20.62 19.89 19.5 20.26 20.6 20.44 20.53 20.02 18.85 17.88 18.29 18.79 16.92 15.43 14.17 15.19 14.48 14.79 16.9 18.31 19.37 20.3 17.56 18.39 18.19 18.05 17.76 18.39 18.49 19.17 20.38 18.89 17.4 17.56 17.84 17.54 17.64 18.18 19.55 17.74 19.54 21.47 21.2 19.76 20.92 20.42 22.25 24.38 23.35 23.75 25.92 24.15 20.3 20.41 20.21 20.88 19.8 20.14 19.61 21.18 21.08 19.15 17.64 17.21 15.44 15.61 15.39 13.95 14.18 14.3 13.34 16.14 14.42 11.22 11.28 12.75 12.27 16.16 18.23 16.84 18.37 20.53 21.9 24.51 21.75 24.59 25.6 28.27 30.43 27.31 25.74 29.01 32.5 27.43 33.12 30.84 33.48 33.82 27.8 28.66 27.39 27.09 27.86 28.37 28.2 26.1 27.2 23.36 21.07 19.37 19.84 19.2 21.48 26.12 27.36 25.02 26.8 27.21 28.99 30.52 26.86 26.79 30.45 33.56 37.05 31.02 26.13 29.32 30.06 30.61 31.78 28.89 28.77 29.95 32.89 33.26 35.56 36.13 37.74 39.41 35.76 43.5 41.8 49.55 51.49 49.98 42.76 47.1 51.93 55.07 50.41 51.48 56.84 60.34 69.31 66.37 60.6 56.41 59.88 请回答:(1)研究1985-2005年原油现货价格的走势,对原油价格拟合 ARIMA模型。(2)研究原油现货价格的波动特征。如果存在条件异异方差,则拟合适当的条件异方差模型。 (3)预测2006-2007年月原油现货价格的走势及 95%的置信区间。
最新发布
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值