ABC340

C

黑板上给一个数N,一直不停重复到黑板上的数组里面只剩下1:
擦去每个大于1的数n
加入⌊n2⌋和⌈n2⌉\lfloor \frac n 2 \rfloor和 \lceil \frac n 2 \rceil2n2n
每擦一个数n计入分数n
求最后的总分数


用map记录一下每个n的总得分

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;


ll n;
map<ll, ll> h;

ll get(ll x) {
    if (h.count(x)) return h[x];
    if (x == 1) return 0;
    if (x % 2 == 0) {
        return h[x] = get(x / 2) * 2 + x;
    }
    else {
        return h[x] = get(x / 2) + get(x / 2 + 1) + x;
    }
}


int main(){
    //freopen("in.txt", "r", stdin);
    cin >> n;
    ll ans = get(n);
    printf("%lld\n", ans);
    return 0;
}

D

1…N的一个数组,一开始在格子1
对于格子i有两个操作,一个花费AiA_iAi到i+1,第二个花费BiB_iBi到d
求最后到N的最小花费


优先队列

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

int n;
int a[200020], b[200020], x[200020];
ll f[200020];
vector<pair<int, ll>> g[200020];

struct Node {
    ll w;
    int u;
    bool operator < (const Node& rhs) const {
        return w > rhs.w;
    }
};


int main(){
    //freopen("in.txt", "r", stdin);
    cin >> n;
    memset(f, 0x33, sizeof(f));
    f[1] = 0;
    for (int i = 1; i < n; ++i) {
        cin >> a[i] >> b[i] >> x[i];
        g[i].push_back({ x[i], b[i] });
        g[i].push_back({ i + 1, a[i] });
    }
    priority_queue<Node> qu;
    qu.push({ 0, 1 });
    while (qu.size()) {
        auto [w, u] = qu.top();
        qu.pop();
        if (u == n) {
            break;
        }
        for (auto [v, tw]: g[u]) {
            if (w + tw < f[v]) {
                f[v] = w + tw;
                qu.push({ f[v], v });
            }
        }
    }
    printf("%lld\n", f[n]);
    return 0;
}

E

围成一圈的n个格子,每个格子里都放着AiA_iAi个球
m个操作,格子MiM_iMi
将格子里面的球拿出来,按顺序依次放到所有格子里面,每次放一个
求最后每个格子里有几个球


对单点更新(清零)–就是区间更新,单点查询,裸的线段树。BIT+差分也可。

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

int n, m;
const int N = 200020;
ll a[N];
#define lu (u * 2)
#define ru (u * 2 + 1)

struct SegTree {
    struct Node {
        int l, r;
        ll val, add;
    }tr[N << 2];

    void push_down(int u) {
        if (tr[u].add) {
            tr[lu].add += tr[u].add;
            tr[ru].add += tr[u].add;
            tr[u].add = 0;
        }
    }

public:
    void build(int l, int r, int u) {
        tr[u].l = l, tr[u].r = r, tr[u].add = 0;
        if (l == r) {
            tr[u].val = a[l];
            return;
        }
        int mi = (l + r) / 2;
        build(l, mi, lu);
        build(mi + 1, r, ru);
    }

    void update(int l, int r, int u, ll v) {
        if (l <= tr[u].l && tr[u].r <= r) {
            tr[u].add += v;
            return;
        }
        push_down(u);
        int mi = (tr[u].l + tr[u].r) / 2;
        if (mi >= l)
            update(l, r, lu, v);
        if (mi < r) 
            update(l, r, ru, v);
    }

    ll query(int p, int u) {
        if (tr[u].l == tr[u].r) {
            return tr[u].val + tr[u].add;
        }
        push_down(u);
        int mi = (tr[u].l + tr[u].r) / 2;
        if (p <= mi) 
            return query(p, lu);
        else 
            return query(p, ru);
    }
} seg;


int main(){
    //freopen("in.txt", "r", stdin);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    seg.build(1, n, 1);
    for (int i = 0; i < m; ++i) {
        int t;
        cin >> t;
        t++;
        ll c = seg.query(t, 1);
        seg.update(t, t, 1, -c);
        seg.update(1, n, 1, c / n);
        ll r = c % n;
        if (r) {
            if (t + r <= n) {
                seg.update(t + 1, t + r, 1, 1);
            }
            else {
                if (t < n) {
                    seg.update(t + 1, n, 1, 1);
                }
                seg.update(1, r - (n - t), 1, 1);
            }
        }
        
    }
    for (int i = 1; i <= n; ++i) {
        ll c = seg.query(i, 1);
        printf("%lld ", c);
    }
    printf("\n");
    return 0;
}

F

给(0,0)点和(X,Y)点,求任意(A,B)点使得三角形面积为1


三角形面积公式
x1y2+x2y3+x3y1−x1y3−x2y1−x3y2=Sx_1y_2+x_2y_3+x_3y_1-x_1y_3-x_2y_1-x_3y_2=Sx1y2+x2y3+x3y1x1y3x2y1x3y2=S
化简为ay−bx=2ay-bx=2aybx=2
扩展欧几里得做就好了

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#include <algorithm>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

ll X, Y;
// ax - by = 2

ll gcd(ll a, ll b) {
    if (a < b) swap(a, b);
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll x0, y0;
    ll d = exgcd(b, a % b, x0, y0);
    // b x0 + a%b y0 = d
    // b x0 + (a - (a / b)b) y0 = d
    // b x0 + a y0 - (a / b) b y0 = d
    // b(x0 - (a/b) y0) + a y0 = d
    // by + ax = d
    // y = x0 - (a/b)y0, x = y0
    y = x0 - (a / b) * y0, x = y0;
    return d;
}


int main(){
    //freopen("in.txt", "r", stdin);
    cin >> X >> Y;
    ll lx = abs(X), ly = abs(Y);
    ll d = gcd(lx, ly);
    
    if (d > 2) {
        printf("-1\n");
        return 0;
    }
    ll x, y;
    exgcd(X, Y, y, x);
    y = -y;
    if (d == 1) {
        printf("%lld %lld\n", x * 2, y * 2);
    }
    else {
        printf("%lld %lld\n", x, y);
    }
    return 0;
}
以下是一个使用施耐德 M340 PLC 实现水泵手动和自动控制功能及程序优化的代码示例,采用 Structured Text(ST)语言编写。 ```st PROGRAM Main VAR // 输入信号 Button_On: BOOL; // 手动全开按钮 Button_Off: BOOL; // 手动全关按钮 Low_Water_Level: BOOL; // 超低水位信号 Float_Switch_1: BOOL; // 1 号浮球信号 Float_Switch_2: BOOL; // 2 号浮球信号 Float_Switch_3: BOOL; // 3 号浮球信号 // 输出信号 Pump_A: BOOL; // A 泵控制信号 Pump_B: BOOL; // B 泵控制信号 Pump_C: BOOL; // C 泵控制信号 High_Water_Level: BOOL; // 超高水位信号 Alarm_Signal: BOOL; // 报警信号 Fault_Signal: BOOL; // 故障信号 // 内部变量 Manual_Mode: BOOL; // 手动模式标志 Auto_Mode: BOOL; // 自动模式标志 Pump_Counter_A: INT := 0; // A 泵启动次数计数器 Pump_Counter_B: INT := 0; // B 泵启动次数计数器 Pump_Counter_C: INT := 0; // C 泵启动次数计数器 Pump_Timer_A: TON; // A 泵启动时间定时器 Pump_Timer_B: TON; // B 泵启动时间定时器 Pump_Timer_C: TON; // C 泵启动时间定时器 Rotation_Counter: INT := 0; // 轮询计数器 END_VAR // 手动控制逻辑 Manual_Mode := Button_On OR Button_Off; IF Manual_Mode THEN IF Button_On THEN Pump_A := TRUE; Pump_B := TRUE; Pump_C := TRUE; ELSIF Button_Off THEN Pump_A := FALSE; Pump_B := FALSE; Pump_C := FALSE; END_IF; END_IF; // 自动控制逻辑 Auto_Mode := NOT Manual_Mode AND NOT Low_Water_Level; IF Auto_Mode THEN IF Float_Switch_1 AND NOT Float_Switch_2 AND NOT Float_Switch_3 THEN // 1 号浮球动作,ABC 泵轮询启动 CASE Rotation_Counter OF 0: Pump_A := TRUE; Pump_B := FALSE; Pump_C := FALSE; Rotation_Counter := 1; 1: Pump_A := FALSE; Pump_B := TRUE; Pump_C := FALSE; Rotation_Counter := 2; 2: Pump_A := FALSE; Pump_B := FALSE; Pump_C := TRUE; Rotation_Counter := 0; END_CASE; ELSIF Float_Switch_1 AND Float_Switch_2 AND NOT Float_Switch_3 THEN // 1、2 号浮球动作,两台一组轮询启动 CASE Rotation_Counter OF 0: Pump_A := TRUE; Pump_B := TRUE; Pump_C := FALSE; Rotation_Counter := 1; 1: Pump_A := FALSE; Pump_B := TRUE; Pump_C := TRUE; Rotation_Counter := 2; 2: Pump_A := TRUE; Pump_B := FALSE; Pump_C := TRUE; Rotation_Counter := 0; END_CASE; ELSIF Float_Switch_1 AND Float_Switch_2 AND Float_Switch_3 THEN // 1、2、3 号浮球动作,三台同时启动 Pump_A := TRUE; Pump_B := TRUE; Pump_C := TRUE; High_Water_Level := TRUE; END_IF; END_IF; // 超低水位信号处理 IF Low_Water_Level THEN Pump_A := FALSE; Pump_B := FALSE; Pump_C := FALSE; END_IF; // 水泵启动次数和时间计时 IF Pump_A AND NOT Pump_Timer_A.Q THEN Pump_Counter_A := Pump_Counter_A + 1; Pump_Timer_A(IN := TRUE, PT := T#1M); ELSIF NOT Pump_A THEN Pump_Timer_A(IN := FALSE); END_IF; IF Pump_B AND NOT Pump_Timer_B.Q THEN Pump_Counter_B := Pump_Counter_B + 1; Pump_Timer_B(IN := TRUE, PT := T#1M); ELSIF NOT Pump_B THEN Pump_Timer_B(IN := FALSE); END_IF; IF Pump_C AND NOT Pump_Timer_C.Q THEN Pump_Counter_C := Pump_Counter_C + 1; Pump_Timer_C(IN := TRUE, PT := T#1M); ELSIF NOT Pump_C THEN Pump_Timer_C(IN := FALSE); END_IF; // 报警信号处理 Alarm_Signal := (Pump_Counter_A > 3) OR (Pump_Counter_B > 3) OR (Pump_Counter_C > 3) OR (Pump_Timer_A.Q) OR (Pump_Timer_B.Q) OR (Pump_Timer_C.Q); // 故障信号处理 Fault_Signal := Low_Water_Level AND (Float_Switch_1 OR Float_Switch_2 OR Float_Switch_3); END_PROGRAM ``` ### 代码说明 1. **输入信号**:包括手动控制按钮、超低水位信号和浮球信号。 2. **输出信号**:包括水泵控制信号、超高水位信号、报警信号和故障信号。 3. **手动控制**:通过手动按钮控制水泵的全开和全关。 4. **自动控制**:根据浮球信号的不同组合,实现水泵的轮询启动和同时启动。 5. **超低水位处理**:当检测到超低水位信号时,所有水泵停止运行。 6. **启动次数和时间计时**:对每台水泵的启动次数和时间进行计时,超过设定值时输出报警信号。 7. **故障信号**:当超低水位信号和其他浮球信号同时存在时,输出故障信号。 ### 注意事项 - 代码中的定时器时间单位为分钟,可以根据实际需求进行调整。 - 轮询计数器用于实现水泵的轮询启动,确保每台水泵都有机会启动。 - 报警信号和故障信号可以连接到相应的报警设备,提醒操作人员注意。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值