8-30(高斯,樹狀數組,分類討論,stl)

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;    
}

今天還正好趁數值分析學了高斯消元,模板貼在這
這裏寫圖片描述
這裏寫圖片描述

明天下午正好沒有課,
把紫書上第五章最後三道習題補完
今天比賽看能寫的都補上
練習幾道高斯消元的模板題

嗯, 又是元氣滿滿的一天呢~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章