我要好好学英语

今天上午一上午时间都耗在查四级分数上了,考的不是很理想,去年他们考的时候就嘲讽某些人还没陈昱丞考的高,陈昱丞当时也算是超常发挥吧蒙对了好多听力考他们年级第一,结果这回犯自己头上了,还没陈昱丞考的高,哈哈。

以前就吐槽本校的acm题不是英文,现在题是英文了又读不懂题,全是靠队友读题,靠别人组的队友读题(哭)。

因为他们有谷歌翻译,结果他们四级都没过(为什么我如此想笑)

我要提高自己的理解能力还有阅读能力,做一个好孩子,好好学英语,为校争光。

这一道题,因为起点随机,所以我们只需要在根放置捕鼠器就可以了,但是根可能会与某些点形成环,这样这些与根形成环的点就互相等价了,相当于一个点。那么在这些点里面我们只需要找c[i]最小的点放置捕鼠器就可以了。

并且基环树形成环的一定是根的位置形成环的(因为每一个点出度为1),那么找环就可以直接用并查集来做

一旦两个点x,y形成x->y这条边之前已经在一个并查集里面了,那么一定是成环了,并且x一定是根节点

D. Mouse Hunt
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%80% of applicants are girls and majority of them are going to live in the university dormitory for the next 44 (hopefully) years.

The dormitory consists of nn rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number ii costs cici burles. Rooms are numbered from 11 to nn.

Mouse doesn't sit in place all the time, it constantly runs. If it is in room ii in second tt then it will run to room aiai in second t+1t+1 without visiting any other rooms inbetween (i=aii=ai means that mouse won't leave room ii). It's second 00 in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap.

That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from 11 to nn at second 00.

What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

Input

The first line contains as single integers nn (1n21051≤n≤2⋅105) — the number of rooms in the dormitory.

The second line contains nn integers c1,c2,,cnc1,c2,…,cn (1ci1041≤ci≤104) — cici is the cost of setting the trap in room number ii.

The third line contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n) — aiai is the room the mouse will run to the next second after being in room ii.

Output

Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

 
 
 
Examples
input
Copy
5
1 2 3 2 10
1 3 4 3 3
output
Copy
3
input
Copy
4
1 10 2 10
2 4 2 2
output
Copy
10
input
Copy
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
output
Copy
2
#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
int a[MAX], c[MAX], pre[MAX], x[MAX];
int findx(int x){
    return  x==pre[x]?x:pre[x]=find(xpre[])
}
int dfs(int x, int y){
    if(x == y) return c[x];
    return min(dfs(a[x], y), c[x]);
}
int main(){
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &c[i]), pre[i] = i;
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++){
        if(findx(a[i]) == findx(i)){
            x[i] = 1;
            continue;
        }
        pre[findx(a[i])] = findx(i);
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) if(x[i]) ans += dfs(a[i], i);
    printf("%d
", ans);
    return 0;
}

这个题是树,当时一看碰都不敢碰,结果还那么多人过,当然pzr也过了,其实用简单的并查集就可以处理。

#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#define INF 1e9
using namespace std;
const int maxn=20005;
int par[maxn];
    void init(int n){
        for(int i=1;i<=n;i++){
            par[i]=i;
        }
    }
    int find(int x){
        if(par[x]==x) return x;
        else return find(par[x]);
    }
    void sanchu(int x){
        par[x]=x;
    }
    bool same(int x,int y){
    return find(x)==find(y);
    }
    int main()
    {
        int n,m,k,t;
        cin>>t;
        for(int j=1;j<=t;j++){
            cin>>n>>k;
            init(n);
            for(int i=1;i<=n;i++){
                cin>>m;
                if(m!=0) par[i]=m;
            }
            printf("Case #%d:
",j);
            char ch;
            int a,b;
            for(int i=0;i<k;i++){
                cin>>ch;
                if(ch=='C'){
                    cin>>a;
                    sanchu(a);
                }
                else{
                    cin>>a>>b;
                    if(same(a,b)) cout<<"YES"<<endl;
                    else cout<<"NO"<<endl;
                }
            }
        }
    }

然后是F,这道题就是用set存元素,如果没在区间范围就存元素,然后lower_bound算法要求在已经按照非递减顺序排序的数组中找到第一个大于等于给定值key的那个数,upper_bound函数要求在按照非递减顺序排好序的数组中找到第一个大于给定值key的那个数

 

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
set<int>s;
struct node
{
    int num;
    int l;
    int r;
} a[100001];
bool cmp(node x,node y)
{
    if(x.r==y.r)
        return x.l<y.l;
    return x.r<y.r;
}
int main()
{
    int t,n,i,j;
    cin>>t;
    int cnt=0;
    while(t--)
    {
        s.clear();
        cnt++;
        cin>>n;
        for(i=0; i<n; i++)
        {
            cin>>a[i].num>>a[i].l>>a[i].r;
            s.insert(a[i].num);
        }
        sort(a,a+n,cmp);
        set<int>::iterator it;
        for(i=0; i<n; i++)
        {
            it=s.lower_bound(a[i].l);
            if(it!=s.end()&&*it==a[i].num)
                it++;
            if(it==s.end()||*it>a[i].r)
            {
                if(a[i].r==a[i].num)
                    s.insert(-a[i].r);
                else
                     s.insert(a[i].r);
            }
        }
        printf("Case #%d: %d
",cnt,s.size());
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kepa/p/9519750.html