题目链接:https://ac.nowcoder.com/acm/contest/5600
题解链接:https://ac.nowcoder.com/discuss/428377?type=101&order=0&pos=15&page=1
分情况讨论
#include
using namespace std;
const int N = 2e5 + 5;
typedef long long ll;
int a[N];
int main(void)
{
int n, x;
ll ans = 0;
scanf("%d%d", &n, &x);
for (int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
if (n <= x){
for (int i = 0; i < n; i++)
ans += a[i];
}
else{
sort(a, a + n);
ans += x * a[n - x];
for (int i = n - x; i < n; i++){
ans += a[i] - a[n - x];
}
}
printf("%lld\n", ans);
return 0;
}
用字符数组来模拟栈
#include
using namespace std;
const int N = 3e5 + 5;
char s[N], st[N];
int main(void)
{
int idx = 0;
scanf("%s", s);
int n = strlen(s);
st[0] = s[0];
for (int i = 1; i < n; i++){
if (st[idx] != s[i]){
st[++idx] = s[i];
}
else{
idx--;
}
}
if (idx != -1){
for (int i = 0; i <= idx; i++)
printf("%c", st[i]);
puts("");
}
else{
printf("0\n");
}
return 0;
}
水题
#include
using namespace std;
const int N = 2e5 + 5;
int main(void)
{
double sum = 0, t;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n - m; i++){
scanf("%lf", &t);
sum += t;
}
printf("%.5lf %.5lf\n", (sum + m) / n, (sum + 5 * m) / n);
return 0;
}
水题
#include
using namespace std;
char s[1005];
int a[30];
int main(void)
{
int maxv = -1, idx;
while (scanf("%s", s) != EOF){
int n = strlen(s);
for (int i = 0; i < n; i++){
a[s[i] - 'a']++;
}
}
for (int i = 0; i < 26; i++){
if (maxv < a[i]){
maxv = max(maxv, a[i]);
idx = i;
}
}
printf("%c\n", 'a' + idx);
return 0;
}
不得不说,此题在本菜鸡能力范围之外
因为double的精度问题,会导致死循环,需要使用long double
并且我的devcpp输出long double类型的答案结果异常,可能是不支持吧
#include
using namespace std;
typedef long double ld;
ld a, b, c;
ld f(ld x)
{
ld ans = 1;
for (int i = 0; i < a; i++)
ans *= x;
ans += b * log(x) - c;
return ans;
}
int main(void)
{
scanf("%Lf%Lf%Lf", &a, &b, &c);
ld eps = 1e-8;
ld l = 1, r = c;
while (r - l >= eps){
ld mid = (l + r) / 2;
if (f(l) * f (mid) <= 0)
r = mid;
else if (f(mid) * f(r) <= 0)
l = mid;
}
printf("%.7Lf\n", l);
return 0;
}
用三个数组,x, y, xy 分别存储每一行的和,每一列的和,每一个位置的和
最后要求的每个位置的和即为:x[i] + y[j] - xy[i][j]
因为没有注意到 n * m < 1e6的条件,一直段错误,没想到会在这里挖坑QAQ
#include
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
ll x[N], y[N], xy[N];
int main(void)
{
ll t;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
scanf("%lld", &t);
x[i] += t, y[j] += t, xy[i * m + j] += t;
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
printf("%lld ", x[i] + y[j] - xy[i * m + j]);
}
puts("");
}
return 0;
}