8-30(高斯,树状数组,分类讨论,stl)

来源:互联网 发布:java与xml 编辑:程序博客网 时间:2024/06/15 04:41

uva 1592
自己代码:

#include <bits/stdc++.h>using namespace std;const int maxnrow = 100;char row[maxnrow];const int maxn = 10010;const int maxm = 15;vector<int> vec[maxn];map<string, int> mark;int tot;int ID(string s){    if(!mark.count(s))  mark[s] = ++tot;    return mark[s];}int main(){    int n, m;    while(cin >> n >> m){        for(int i = 0; i < n; i++) vec[i].clear();        mark.clear();        tot = 0;        char ch = getchar();        for(int i = 0; i < n; i++){            gets(row);            char word[maxnrow];            int s = strlen(row);            for(int t = 0, j = 0; j <= s; j++, t++)            {                if(row[j] == ',' || row[j] == '\0') {                    word[t] = '\0';                    string str = word;                    vec[i].push_back(ID(str));                    t = -1;                }                else word[t] = row[j];            }        }        bool flag = true;        int r[2], c[2];        for(int i = 0; i < n; i++){            for(int j = i+1; j < n; j++){                int cnt = 0;                for(int k = 0; k < m; k++)                    if(vec[i][k] == vec[j][k]){                        r[0] = i, r[1] = j, c[cnt] = k, cnt++;                        if(cnt == 2) {                            flag = false;                            break;                        }                    }                if(!flag) break;            }            if(!flag) break;        }        if(flag)            printf("YES\n");        else            printf("NO\n%d %d\n%d %d\n", r[0]+1, r[1]+1, c[0]+1, c[1]+1);    }    return 0;}

刘哥哥的版本:

// UVa1592 Database// Rujia Liu// 本程序只是为了演示STL各种用法,效率较低。实践中一般用C字符串和哈希表来实现。#include<iostream>#include<cstdio>#include<vector>#include<string>#include<map>#include<sstream>using namespace std;typedef pair<int,int> PII;const int maxr = 10000 + 5;const int maxc = 10 + 5;int m, n, db[maxr][maxc], cnt;map<string, int> id;int ID(const string& s) {  if(!id.count(s)) {    id[s] = ++cnt;  }  return id[s];}void find() {  for(int c1 = 0; c1 < m; c1++)    for(int c2 = c1+1; c2 < m; c2++) {      map<PII, int> d;      for(int i = 0; i < n; i++) {        PII p = make_pair(db[i][c1], db[i][c2]);        if(d.count(p)) {          printf("NO\n");          printf("%d %d\n", d[p]+1, i+1);          printf("%d %d\n", c1+1, c2+1);          return;        }        d[p] = i;      }    }  printf("YES\n");}int main() {  string s;  while(getline(cin, s)) {    stringstream ss(s);    if(!(ss >> n >> m)) break;    cnt = 0;    id.clear();    for(int i = 0; i < n; i++) {      getline(cin, s);      int lastpos = -1;      for(int j = 0; j < m; j++) {        int p = s.find(',', lastpos+1);        if(p == string::npos) p = s.length();        db[i][j] = ID(s.substr(lastpos+1, p - lastpos - 1));        lastpos = p;      }    }    find();  }  return 0;}

hdu 5857

//这题主要是分类讨论,过程的处理, 代码写的不够好看看有没有可以改进的地方#include <bits/stdc++.h>using namespace std;const int maxn = 100010;double a[maxn];int main(){    //freopen("1.txt", "r", stdin);    int n, m;    int t;    cin >> t;    while(t--){        cin >> n >> m;        for(int i = 1; i <= n; i++)            scanf("%lf", &a[i]);        sort(a+1, a+n+1);        for(int i = 0; i < m; i++){            int l1, r1, l2, r2;            scanf("%d%d%d%d", &l1, &r1, &l2, &r2);            if(l1>l2) swap(l1, l2);            if(r1>r2) swap(r2, r1);            int cnt = r1-l1+r2-l2+2;            bool flag = true;            if(cnt % 2 == 1) flag = false;            cnt = (cnt-1)/2;            int tmp = 0;            double ans;            if(l2 > r1){                if(r1-l1+1<=cnt)                       tmp = l2 + cnt -(r1 - l1 + 1);                else if(flag && r1-l1==cnt) //注意这里的等于号特判, 若中位数取左段最右值, 右段最左值时需要单独处理                    ans = (a[r1] + a[l2])/2.0;                else                    tmp = l1 + cnt;            }            else{                tmp = 0;                if(l2-l1>cnt) tmp = l1+cnt;                else{                    tmp = 0;                    cnt -= l2-l1;                    if(2*(r1-l2+1)>cnt){                        if(cnt%2==1)                            if(flag == false) ans = a[l2+cnt/2];                            else ans = (a[l2+cnt/2]+a[l2+1+cnt/2])/2.0;                        else                            if(flag == false) ans = a[l2+cnt/2];                            else ans = a[l2+cnt/2];                    }                    else{                        cnt -= 2*(r1-l2+1);                        tmp = r1+1 + cnt;                    }                }            }            if(tmp == 0) printf("%.1lf\n", ans);            else{                if(flag) ans = (a[tmp]+a[tmp+1])/2.0;                  //注意这里开始a都设的int型,没注意相加会溢出,WA了一发                else ans = a[tmp];                printf("%.1lf\n", ans);            }        }    }    return 0;}

uva 5862
自己最终还是T了的代码, 待补全

#include <bits/stdc++.h>using namespace std;const int maxn = 100010;struct vert{    int x, h1, h2;};vert v[maxn];bool operator < (const vert& a, const vert& b){    return a.x < b.x;}struct hori{    int x, h;};hori h[maxn*2];bool operator < (const hori& a, const hori& b){    return a.x < b.x;}int tree[maxn*2];void add(int x, int c, int n){    while(x <= n){        tree[x] += c;        x += x&-x;    }}long long sum(int x){    long long ans = 0;    while(x){        ans += tree[x];        x -= x&-x;    }    return ans;}vector<int> vec;map<int, int> id, mark;int main(){   // freopen("1.txt", "r", stdin);    int t;    scanf("%d", &t);    while(t--){        int n;        int nv = 0, nh = 0;        vec.clear();        id.clear();        mark.clear();        scanf("%d", &n);        for(int i = 0; i < n; i++){            int x1, y1, x2, y2;            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);            if(x1 == x2){                vec.push_back(y1);                vec.push_back(y2);                v[nv].x = x1;                if(y1 > y2) swap(y1, y2);                v[nv].h1 = y1;                v[nv].h2 = y2;                nv++;            }            if(y1 == y2){                vec.push_back(y1);                h[nh].x = x1, h[nh].h = y1;                nh ++;                h[nh].x = x2, h[nh].h = y1;                nh ++;            }        }        sort(vec.begin(), vec.end());        sort(h, h+nh);        sort(v, v+nv);        int maxh = 0;        for(int i = 0; i < vec.size(); i++)            if(!mark.count(vec[i])) id[vec[i]] = ++maxh;        memset(tree, 0, sizeof(tree));        long long ans = 0;        for(int i = 0, j = 0; i < nv; i++){            while(h[j].x <= v[i].x){                if(mark.count(id[h[j].h])){                    mark.erase(id[h[j].h]);                    add(id[h[j].h], -1, maxh);                }                else{                    mark[id[h[j].h]] = 1;                    add(id[h[j].h], 1, maxh);                }                j++;            }            ans += sum(id[v[i].h1])-sum(id[v[i].h2]);            //printf("  %lld %d %d\n", ans, v[i].h1, v[i].h2);        }        printf("%lld\n", ans);    }    return 0;}

别人的代码:
参考 http://www.cnblogs.com/thecoollight/p/5788283.html

#include <bits/stdc++.h>using namespace std;const int maxn = 101000;#define lowbit(x) (x&(-x))struct Node{    int type,x,y,y1;    bool operator < (const Node & R)const{        return (x == R.x ? type < R.type : x < R.x);    }}node[maxn*2];int Maxn;int cy[maxn*2];int bi[maxn*2];void add(int add,int n){    for (int i = add; i <= Maxn; i += lowbit(i))        bi[i] += n;}int sum(int n){    int ret = 0;        for (int i = n; i > 0; i -= lowbit(i))        ret += bi[i];    return ret;}map <int,int>mp;int main(){    int t;    scanf("%d",&t);    while (t--){        mp.clear();        memset(bi,0,sizeof bi);        int n;        scanf("%d",&n);        int cnode,ccy;        cnode = ccy = 0;        int x1,x2,y1,y2;        for (int i = 1; i <= n; i++){            scanf("%d %d %d %d",&x1,&y1,&x2,&y2);            if (x1 == x2){                if (y1 > y2)    swap(y1,y2);                    node[++cnode]={1,x1,y1,y2};                cy[++ccy] = y1;                cy[++ccy] = y2;            }else {                if (x1 > x2)    swap(x1,x2);                node[++cnode]={0,x1,y1,1};                node[++cnode]={0,x2+1,y2,-1};                cy[++ccy] = y1;            }        }        sort(cy+1,cy+ccy+1);        int acl = 0;        for (int i = 1; i <= ccy; i++){            if (!mp[cy[i]]) mp[cy[i]] = ++ acl;            }        Maxn = acl;        sort(node+1,node+cnode+1);        long long ans = 0;        for (int i = 1; i <= cnode; i++){            if (node[i].type){                ans += (sum(mp[node[i].y1]) - sum(mp[node[i].y]-1));            }else {                add(mp[node[i].y],node[i].y1);                }            }        printf("%lld\n",ans);    }    return 0;    }

今天还正好趁数值分析学了高斯消元,模板贴在这
这里写图片描述
这里写图片描述

明天下午正好没有课,
把紫书上第五章最后三道习题补完
今天比赛看能写的都补上
练习几道高斯消元的模板题

嗯, 又是元气满满的一天呢~

原创粉丝点击