A. Equator(模拟)

找权值的中位数,直接模拟。。

代码写的好丑qwq。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
int a[MAXN];
main() {
#ifdef WIN32
// freopen("a.in", "r", stdin);
#endif
int N = read(), sum = ;
for(int i = ; i <= N; i++) a[i] = read(), sum += a[i];
int now = ;
for(int i = ; i <= N; i++) {
now += a[i];
if(!(sum & )) {
if(now >= sum / ) {
printf("%d", i); return ;
}
} else {
if(now > sum / ) {
printf("%d", i); return ;
}
}
}
}

A

B. Students in Railway Carriage(贪心)

直接贪心放,如果是偶数的话肯定是两种各放一半

如果是奇数的话,应该在都放一半的基础上,把多的放在最后一个

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN = 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
char s[MAXN];
main() {
#ifdef WIN32
// freopen("a.in", "r", stdin);
#endif
int N = read(), A = read(), B = read();
if(A < B) swap(A, B);
scanf("%s", s + );
int last = ;
int ans = ;
s[N + ] = '*';
for(int i = ; i <= N + ; i++) {
if(s[i] == '*') {
int len = i - last - ;
ans += min(len / , A) + min(len / , B);
A -= min(len / , A); B -= min(len / , B);
if((len & ) && A > ) A--, ans++;
if(A < B) swap(A, B);
last = i;
}
} printf("%d", ans);
}

B

C. Make a Square(数论,暴力)

首先把所有平方数预处理出来

然后对于输出的数的各个位分解出来,

枚举所有的平方数,算出最少需要删几个

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
using namespace std;
const int MAXN = 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int po[MAXN], tot = , N, P[MAXN], Pnum = ;
int check(int val) {
int times = ;
for(int i = ; i <= Pnum; i++) {
if(P[i] == val % ) {
val /= , times++;
}
if(val == ) {
return Pnum - times;
}
}
if(val != ) return -;
else return Pnum - times;
}
main() {
#ifdef WIN32
// freopen("a.in", "r", stdin);
#endif
for(int i = ; ; i++) {
po[++tot] = i * i;
if(po[tot] > * 1e9) break;
}
N = read();
int x = N;
while(x) P[++Pnum] = x % , x /= ;
int ans = 1e9;
for(int i = ; i <= tot; i++) {
int num = check(po[i]);
if(num != -) ans = min(ans, num);
}
printf("%I64d", ans == 1e9 ? - : ans);
}

C

D. Merge Equals(堆)

用优先队列维护每个数的位置和权值(权值相同的时候位置小的在前面)

然后每次取出前两个,如果相同就合成完再放回去,否则统计答案

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define int long long
using namespace std;
const int MAXN = 1e6 + ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int N;
struct Node {
int pos, val;
bool operator < (const Node &rhs) const {
return val == rhs.val ? pos > rhs.pos : val > rhs.val;
}
};
priority_queue<Node>q;
int ans[MAXN];
void work() {
while(q.size() >= ) {
Node x = q.top(); q.pop();
Node y = q.top(); q.pop();
if(x.val != y.val) {q.push(y); ans[x.pos] = x.val; continue;}
q.push((Node){y.pos, x.val * });
}
while(q.size() != ) ans[q.top().pos] = q.top().val, q.pop();
}
main() {
#ifdef WIN32
// freopen("a.in", "r", stdin);
#endif
N = read();
for(int i = ; i <= N; i++)
q.push((Node){i, read()});
work();
int num = ;
for(int i = ; i <= N; i++)
if(ans[i] != )
num++;
printf("%I64d\n", num);
for(int i = ; i <= N; i++)
if(ans[i] != )
printf("%I64d ", ans[i]);
}

D

最新文章

  1. Servlet引擎Jetty之入门1
  2. 我的 vim 基本配置
  3. jquery-easyui 树的使用笔记
  4. Flume_使用
  5. Python3基础 列表之间+ 合并,不去除重复项
  6. Servlet学习三——传输文件
  7. 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1
  8. redis2.8--主从机同步流程
  9. hdu 1509 Windows Message Queue
  10. eclipse(STS,myeclipse)老是报ThreadPoolExecutor$Worker.run()
  11. 文件I/O(不带缓冲)之write函数
  12. ##DAY7 UINavigationController
  13. linux进程解析--进程的创建
  14. Filter的注册2
  15. No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android&quot;
  16. MyBatis Generator中文文档
  17. html元素的分类以及特点
  18. 自己动手实现一个MVVM库
  19. Bean的加载过程
  20. 如何编写LVS对Real Server的健康状态检测脚本

热门文章

  1. php7.0升级到php7.1
  2. 【python】字符遍历
  3. win7下登入本機、域的正確方法
  4. HDU 4499 Cannon (暴力搜索)
  5. 《Linux Device Drivers》第八章 分配内存——note
  6. poj 1390 Blocks (记忆化搜索)
  7. php 把一个数组分成有n个元素的二维数组的算法
  8. 【网络流】 HDU 3468 Treasure Hunting
  9. .NET平台下Redis使用(二)【StackExchange.Redis学习】
  10. 【HAOI 2008】 糖果传递