社区讨论

为什么fread会把输入数据重复读呀

P6033[NOIP 2004 提高组] 合并果子 加强版参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@lo7ipj0u
此快照首次捕获于
2023/10/27 02:28
2 年前
此快照最后确认于
2023/10/27 02:28
2 年前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;


namespace io {
  char buf[1 << 21 + 1], *p1 = buf, *p2 = buf;
  inline char gc(void) {
    if (p1 == p2)
      p2 = buf + fread(buf, 1, 1 << 21, stdin), p1 = buf;
    return *p1++;
  }
  template<typename T>
  T iread(void) {
    T res(0), f(1);
    char ch;
    while (!isdigit(ch = gc()))
      f = ch == '-' ? 1 : -1;
    res = ch & 15;
    while (isdigit(ch = gc()))
      res = (res << 1) + (res << 3) + (ch & 15);
    return f * res;
  }

  template<typename T>
  T uread(void) {
    T res(0);
    char ch;
    while (!isdigit(ch = gc()));
    res = ch & 15;
    while (isdigit(ch = gc()))
      res = (res << 1) + (res << 3) + (ch & 15);
    return res;
  }

  size_t gsl(char *const &s) {
    size_t len(0);
    char c;
    while ((c = gc()) == '\n' || c == '\r');
    s[len++] = c;
    while ((c = gc()) != '\n' && c != '\r')
      s[len++] = c;
    s[len] = '\0';
    return len;
  }

  size_t gs(char *const &s){
    size_t len(0);
    char c;
    while (isblank(c = gc()) || c == '\n');
    s[len++] = c;
    while (!isblank(c = gc()) && c != '\n')
      s[len++] = c;
    s[len] = '\0';
    return len;
  }

} using namespace io;

int tmp[100005];
queue<long long> q1, q2;

int main() {
  int n(uread<int>());
  long long ans(0);
  for (int i = 1; i <= n; ++i)
      ++tmp[uread<int>()];
  for (int i = 1; i <= 100000; ++i) 
    while (tmp[i]) {
      --tmp[i], q1.push(i);
    }
  for (int i = 1; i < n; ++i) {
    long long a, b;
    if ((q1.front() < q2.front() && !q1.empty()) || q2.empty())
      a = q1.front(), q1.pop();
    else
      a = q2.front(), q2.pop();
    if ((q1.front() < q2.front() && !q1.empty()) || q2.empty())
      b = q1.front(), q1.pop();
    else
      b = q2.front(), q2.pop();
    ans += a + b, q2.push(a + b);
  }
  printf("%lld\n", ans);
}
subtask1的第四个点完全过不去,经IDE调试发现输入被读了两遍

回复

0 条回复,欢迎继续交流。

正在加载回复...