为什么fread会把输入数据重复读呀
查看原帖
为什么fread会把输入数据重复读呀
177878
金庆涵楼主2022/11/18 21:53
#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调试发现输入被读了两遍

2022/11/18 21:53
加载中...