Nothing for Nothing( 十八 )

題目:Nested Segments

思路:把橫座標從小到大排序,縱座標進行離散化,然後從右往左遍歷,用樹狀數組去維護。
代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
struct Node{
       int x,y, id;
}seg[maxn];
int ans[maxn], c[maxn], n;
int lowbit(int x){
return x & (-x);
}
void add(int x, int v){
     while(x <= n){
        c[x] += v;
        x += lowbit(x);
     }
}
int ask(int x){
     int ans = 0;
     while(x){
        ans += c[x];
        x -= lowbit(x);
     }
     return ans;
}
bool cmp1(Node A, Node B){
    return A.y < B.y;
}
bool cmp2(Node A, Node B){
     return A.x < B.x;
}
int main(){
    int m;
    scanf("%d", &m);
    memset(c, 0, sizeof c);
    for(int i = 0; i < m; i++){
        scanf("%d %d", &seg[i].x, &seg[i].y);
        seg[i].id = i;
    }
    sort(seg, seg+m, cmp1);
    for(int i = 0; i < m; i++){
        seg[i].y = i+1;
    }

    n = m;
    sort(seg, seg+m, cmp2);
    for(int i = m-1; i >= 0; i--){
        ans[seg[i].id] = ask(seg[i].y);
        add(seg[i].y, 1);
    }
    for(int i = 0; i < m; i++){
        printf("%d\n", ans[i]);
    }

return 0;
}

題目:Igor In the Museum

思路:就是一個BFS沒什麼可說的。
代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1050;
 
char mp[maxn][maxn];
bool vis[maxn][maxn];
int res[maxn][maxn], rev[100100];
 
int n, m, k;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct Node
{
    int x, y;
} tmp1, tmp2;
int cnt = 0;
void BFS(int x, int y)
{
    int xx, yy;
    int ans = 0;
    cnt++;
    queue<Node>q;
    res[x][y] = cnt;
    tmp1.x = x;
    tmp1.y = y;
    q.push(tmp1);
    while(q.size())
    {
        tmp2 = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            xx = tmp2.x + dx[i];
            yy = tmp2.y + dy[i];
            if(xx >= 0 && xx < n && yy >= 0 && yy < m && mp[xx][yy] == '*')
            {
                ans ++;
            }
        }
        for(int i = 0; i < 4; i++)
        {
            xx = tmp2.x+dx[i];
            yy = tmp2.y+dy[i];
            if(xx >= 0 && xx < n && yy >= 0 && yy < m && res[xx][yy] == 0 && mp[xx][yy] == '.')
            {
                tmp1.x = xx;
                tmp1.y = yy;
                res[xx][yy] = cnt;
                q.push(tmp1);
            }
        }
    }
    rev[cnt] = ans;
}
int main()
{
    scanf("%d %d %d", &n, &m, &k);
    cnt = 0;
    memset(res, 0, sizeof res);
    for(int i = 0; i < n; i++)
    {
        scanf("%s", mp[i]);
    }
    while(k--)
    {
        int s, t;
        scanf("%d %d", &s, &t);
        s--, t--;
        if(!res[s][t])BFS(s,t);
        printf("%d\n",rev[res[s][t]]);
    }
 
    return 0;
}

題目:King’s Path

思路:本題也是一個BFS搜索,但是由於範圍過大,所以我們就用mp來標記。
代碼:

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10;
map<pair<int, int>, int>mp;


int d[8][2]={1,1,1,-1,-1,1,-1,-1,0,1,1,0,-1,0,0,-1};
int a0, b0, a1, b1, n;
int BFS(){
    int ans = 0;
    int x, y, xx, yy;
    queue<pair<int, int> >q;
    q.push(make_pair(a0, b0));
    mp[make_pair(a0, b0)] = 0;
    while(q.size()){
        x = q.front().first;
        y = q.front().second;
        q.pop();
        if(x == a1 && y == b1)return mp[make_pair(x, y)];
        for(int i = 0; i < 8; i++){
            xx =  x + d[i][0];
            yy =  y + d[i][1];
            if(mp[make_pair(xx, yy)] == -1 ){
                q.push(make_pair(xx, yy));
                mp[make_pair(xx,yy)] = mp[make_pair(x, y)] +1;
            }
        }

    }
    return -1;

}
int main(){
    scanf("%d %d %d %d", &a0, &b0, &a1, &b1);
    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        int r, a, b;
        scanf("%d %d %d", &r, &a, &b);
        for(int i = a; i <= b; i++){
            mp[make_pair(r, i)] = -1;
        }
    }
    if(!mp.count(make_pair(a1,b1))) cout<<"-1"<<endl;
        else cout<<BFS()<<endl;
return 0;
}


題目:Extract Numbers

代碼:

a = input().replace(';' , ',').split(',')
x = []
y = []
for i in a:
    if i.isdigit() and (i == '0' or i[0] != '0'):
        x.append(i)
    else:
        y.append(i)
for i in x,y:
    print('"{}"'.format(','.join(i))if i else '-')
 

題目:Vanya and Label

代碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
#define LL long long
map<char, int>mp;
int cot[100];
char a[maxn];
const int mod  = 1e9+7;
int main()
{
    for(int i = 0; i <= 9; i++)mp[i+'0'] = i;
    for(int i = 10; i <= 35; i++)mp[i+'A'-10] = i;
    for(int i = 36; i <= 61; i++)mp[i+'a'-36] = i;
    mp['-'] = 62;
    mp['_'] = 63;
    for(auto &v:mp)
    {
        for(auto &x:mp)
        {
            for(auto &y:mp)
            {
                if((x.second & y.second) == v.second)
                {
                    cot[v.second]++;
                }
            }
        }
    }
 
    LL ans = 1;
    scanf("%s", a);
    int len = strlen(a);
    for(int i = 0; i < len; i++){
        ans = (ans * (cot[mp[a[i]]])) % mod;
    }
    printf("%lld\n",ans);
 
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章