3月13日练习记录

补题

CCA的搬运

Title

在一个竖直的洞里有 n 个有重量的球,需要进行 m 次操作,每次操作需要将其中一个球拿出来然后放在最上面 。
取出一个小球放在最上面需要消耗的体力为它上面的小球的重量之和 。
现在给定每次操作需要取的小球的编号,要求出一种初始的放球方案使得消耗的总体力最少 。

Input

第一行两个正整数 n 和 m,意义如题所示 。
第二行 n 个正整数,分别表示 n 个球的重量 。
第三行 m 个正整数,分别表示 m 次操作取出小球的编号 。

Output

一个整数表示消耗的总体力的最小值 。

Answer

#include<iostream>
#include<stack>
using namespace std;
int n,m,p;
const int N = 2010;
int a[N];
stack<int> now,temp;
int main(){
    cin >> n >> m;
    int ans = 0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=m;i++){
        scanf("%d",&p);
        int sum = 0;
        while(!now.empty()){
            int cur = now.top();
            now.pop();
            if(cur == p)break;
            sum+=a[cur];
            temp.push(cur);
        }
        while(!temp.empty()){
            int cur = temp.top();
            temp.pop();
            now.push(cur);
        }
        ans+=sum;
        now.push(p);
    }
    cout << ans << endl;
}

Hint

这道题是贪心的思想,即先出现的石头默认放在最上面。用一个栈存储目前的序列,每取一次就进行一次计数,然后用另一个栈进行过渡,把取出来的再放回去。

并查集

Wireless Network

Title

An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap computers, but an unexpected aftershock attacked, all computers in the network were all broken. The computers are repaired one by one, and the network gradually began to work again. Because of the hardware restricts, each computer can only directly communicate with the computers that are not farther than d meters from it. But every computer can be regarded as the intermediary of the communication between two other computers, that is to say computer A and computer B can communicate if computer A and computer B can communicate directly or there is a computer C that can communicate with both A and B.

In the process of repairing the network, workers can take two kinds of operations at every moment, repairing a computer, or testing if two computers can communicate. Your job is to answer all the testing operations.

Input

The first line contains two integers N and d (1 <= N <= 1001, 0 <= d <= 20000). Here N is the number of computers, which are numbered from 1 to N, and D is the maximum distance two computers can communicate directly. In the next N lines, each contains two integers xi, yi (0 <= xi, yi <= 10000), which is the coordinate of N computers. From the (N+1)-th line to the end of input, there are operations, which are carried out one by one. Each line contains an operation in one of following two formats:

  1. “O p” (1 <= p <= N), which means repairing computer p.
  2. “S p q” (1 <= p, q <= N), which means testing whether computer p and q can communicate.

The input will not exceed 300000 lines.

Output

For each Testing operation, print “SUCCESS” if the two computers can communicate, or “FAIL” if not.

Answer

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int fa[1005];
bool vis[1005];
int n,d;
struct Node{
    int x,y;
}a[1005];
void init(){
    for(int i=1;i<=n;i++){
        fa[i]=i;
    }
}
int find(int x){
    if(fa[x]==x){
        return x;
    }
    else{
        return fa[x]=find(fa[x]);
    }
}
void unit(int x,int y){
    x=find(x);
    y=find(y);
    if(x!=y)
        fa[x]=y;
    else 
        return ;
}
int main(){
    memset(vis,false,sizeof(vis));
    char op;
    cin >> n >> d;
    init();
    for(int i=1;i<=n;i++){
        scanf("%d %d",&a[i].x,&a[i].y);
    }
    while(~scanf(" %c",&op)){
        if(op=='O'){
            int id;
            scanf("%d",&id);
            vis[id]=true;
            for(int i=1;i<=n;i++){
                if(vis[i]==true){   
                    int distance=(a[id].x-a[i].x)*(a[id].x-a[i].x)+(a[id].y-a[i].y)*(a[id].y-a[i].y);
                    if(distance <= d*d){
                        unit(id,i);
                    }
                }
            }
        }
        if(op=='S'){
            int c,d;
            scanf("%d %d",&c,&d);
            if(find(d)==find(c))
                printf("SUCCESS\n");
            else
                printf("FAIL\n");
        }
    }
    return 0;
}

Hint

并查集的简单应用,注意要考虑距离与是否修好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值