【NOIP2018pj】题解

【NOIP2018pj】题解

\(T1\)

题面

洛谷

题解

好像并没有什么好说的。。。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring> 
#include <cmath>
#include <algorithm>
#include <string> 
using namespace std;
string s;
int main () {
    getline(cin, s);
    int ans = 0; 
    for (int i = 0, l = s.size(); i < l; i++) {
        if (isdigit(s[i]) || ('a' <= s[i] && s[i] <= 'z') || ('A' <= s[i] && s[i] <= 'Z')) ans++; 
    }
    printf("%d\n", ans); 
    return 0; 
} 

\(T2\)

题面

洛谷

题解

先把每一边贡献算出来再扫一遍即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring> 
#include<cmath>
#include<algorithm>
using namespace std;
inline int gi() {
    register int data = 0, w = 1; 
    register char ch = 0;
    while (ch != '-' && !isdigit(ch)) ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); 
    return data * w; 
}
#define MAX_N 100005
int N, a[MAX_N]; 
int m, p1, s1, s2; 
typedef long long ll;
ll Left = 0, Right = 0, dlt = 0; 
int ans = 0; 
int main () {
    N = gi(); 
    for (int i = 1; i <= N; i++) a[i] = gi(); 
    m = gi(), p1 = gi(), s1 = gi(), s2 = gi(); 
    a[p1] += s1; 
    for (int i = 1; i < m; i++) Left += 1ll * a[i] * (m - i); 
    for (int i= m + 1; i <= N; i++) Right += 1ll * a[i] * (i - m);
    ans = m; dlt = abs(Left - Right);
    for (int i = 1; i < m; i++) {
        ll res = abs(Left + 1ll * s2 * (m - i) - Right);
        if (res < dlt) ans = i, dlt = res;
        else if (res == dlt) ans = min(i, ans); 
    }
    for (int i = m + 1; i <= N; i++) {
        ll res = abs(Right + 1ll * s2 * (i - m) - Left);
        if (res < dlt) ans = i, dlt = res;
        else if (res == dlt) ans = min(i, ans); 
    }
    printf("%d\n", ans); 
    return 0; 
} 

\(T3\)

这里

\(T4\)

这里

转载于:https://www.cnblogs.com/heyujun/p/10050610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值