题目链接:

题目

D. Toy Sum

time limit per test:1 second

memory limit per test:256 megabytes

问题描述

Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris.

There are exactly s blocks in Chris's set, each block has a unique number from 1 to s. Chris's teacher picks a subset of blocks X and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset Y from the remaining blocks, that the equality holds:

"Are you kidding me?", asks Chris.

For example, consider a case where s = 8 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1 - 1) + (4 - 1) + (5 - 1) = (8 - 3) + (8 - 6) = 7.

However, now Chris has exactly s = 106 blocks. Given the set X of blocks his teacher chooses, help Chris to find the required set Y!

输入

The first line of input contains a single integer n (1 ≤ n ≤ 5·105), the number of blocks in the set X. The next line contains n distinct space-separated integers x1, x2, ..., xn (1 ≤ xi ≤ 106), the numbers of the blocks in X.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

输出

In the first line of output print a single integer m (1 ≤ m ≤ 106 - n), the number of blocks in the set Y. In the next line output m distinct space-separated integers y1, y2, ..., ym (1 ≤ yi ≤ 106), such that the required equality holds. The sets X and Y should not intersect, i.e. xi ≠ yj for all i, j (1 ≤ i ≤ n; 1 ≤ j ≤ m). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.

样例

input

3

1 4 5

output

2

999993 1000000

input

1

1

output

1

1000000

题意

给你一个大小为n的X集合x1,x2,...,xn,问你能不能选定一个集合大小S(S<=10^6),使得从S包含X,并且从属于S但不属于X的元素组成的集合里,选出一个大小为k的非空子集Y,使得等式sigma(xi-1)==sigma(s-yj),其中1<=i<=n,1<=j<=k。

题解

1、暴搜,在属于S但不属于X的元素组成的集合里暴搜出一个满足答案的解。跑了717ms。(暴搜方案,每个数选或不选)

#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; typedef __int64 LL;
const int maxn=5e5+10;
const int INF=1e6; int arr[maxn];
int n;
map<int,int> mp;
vector<int> ans;
vector<int> re; bool dfs(int cur,LL sum){
if(sum==0) return true;
int pos=lower_bound(re.begin(),re.end(),sum)-re.begin();
if(pos>cur) pos=cur;
if(pos<=0) return false;
ans.push_back(INF-re[pos]);
if(dfs(pos-1,sum-re[pos])) return true;
ans.pop_back();
if(dfs(pos-1,sum)) return true;
return false;
} int main(){
scanf("%d",&n);
LL sum=0;
for(int i=0;i<n;i++){
int x; scanf("%d",&x); mp[x]=1;
sum+=x-1;
}
re.clear();
re.push_back(-1);
for(int i=1;i<=INF;i++){
if(!mp[i]){
re.push_back(INF-i);
}
}
sort(re.begin(),re.end());
dfs(re.size()-1,sum);
if(!mp[INF]) ans.push_back(INF);
printf("%d\n",ans.size());
for(int i=0;i<ans.size()-1;i++) printf("%d ",ans[i]);
printf("%d\n",ans[ans.size()-1]);
return 0;
}

2、找对称的数,对于x,有y=S+1-i,使得x-1=S-y;所以对于xi,如果与它对称的数yi不在X里面,则吧yi放到答案里面,如果在,则这两个数相加为S,我们只要另找一个数不在X里面,并且它对称的数也不在X里面,然后吧这两个数都加到答案里面就可以了。

#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; const int maxn = 1e6 + 10;
const int S = 1e6; int used[maxn]; int main() {
memset(used, 0, sizeof(used));
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x; scanf("%d", &x); used[x] = 1;
}
vector<int> ans;
//cnt统计一个数和与它对称的数都在X里面的数对
int cnt = 0;
for (int i = 1; i <= S; i++) {
if (used[i]) {
int j = S + 1 - i;
if (!used[j]) {
ans.push_back(j);
used[j] = 1;
}
else {
if(i<=j) cnt++;
}
}
}
for (int i = 1; i <= S&&cnt>0; i++) {
if (!used[i]) {
int j = S + 1 - i;
ans.push_back(i);
ans.push_back(j);
used[i] = used[j] = 1;
cnt--;
}
}
sort(ans.begin(), ans.end());
printf("%d\n", ans.size());
for (int i = 0; i < ans.size() - 1; i++) printf("%d ", ans[i]);
printf("%d\n",ans[ans.size()-1]);
return 0;
}

Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜的更多相关文章

  1. Codeforces Round #238 (Div. 2) D. Toy Sum

    D. Toy Sum   time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  2. Codeforces Round #238 (Div. 2) D. Toy Sum(想法题)

     传送门 Description Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to s ...

  3. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  4. Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Descripti ...

  5. Codeforces Round #238 (Div. 1)

    感觉这场题目有种似曾相识感觉,C题还没看,日后补上.一定要坚持做下去. A Unusual Product 题意: 给定一个n*n的01矩阵,3种操作, 1 i 将第i行翻转 2 i 将第i列翻转 3 ...

  6. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  7. Codeforces Round #303 (Div. 2) A. Toy Cars 水题

     A. Toy Cars Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/problem ...

  8. Codeforces Round #344 (Div. 2) E. Product Sum 维护凸壳

    E. Product Sum 题目连接: http://www.codeforces.com/contest/631/problem/E Description Blake is the boss o ...

  9. Codeforces Round #232 (Div. 2) D. On Sum of Fractions

    D. On Sum of Fractions Let's assume that v(n) is the largest prime number, that does not exceed n; u ...

随机推荐

  1. CSS之text-stroke

    啧啧啧( ̄︶ ̄),国庆人太多,所以假期还没结束就提前几天回来了.今天也是挤火车赶回来的,被夹在门里好尴尬啊~~ 回家的这几天在外婆家招待过的好爽啊,又是鱼又是肉,馋的我都不想走了. 然而自己在家只能“ ...

  2. JS数据类型转换

    JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...

  3. OC7_代理的基本概念

    // // Cat.h // OC7_代理的基本概念 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxue ...

  4. PowerDesigner数据库建模工具一缆

    转自:http://blog.csdn.net/shanliwa/archive/2007/10/20/1834117.aspx Sybase PowerDesigner - 一个高端数据建模工具.你 ...

  5. ubuntu下安装git,sublime,nodejs

    用的是VMware10.0版本的虚拟机,很早之前下载的今天就直接用了,安装挺简单记得需要一个序列号.在这里:http://mirrors.163.com/ubuntu-releases/15.04/u ...

  6. web应用中webapp. root重用问题解决方案

      同一个tomcat服务器里面部署两个JavaEE项目,都是用了log4j做日志.并且web.xml里面都监听了日志信息. 启动服务的时候报错. 于是在web.xml添加以下代码:   <di ...

  7. Windows 命令大全

    打开控制面板的方法:输入control,回车即可打开. 以下是“运行”里常见的命令: gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址 ...

  8. c++11:function的用法

    function是函数.函数对象.函数指针.和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针 普通函数 #include <functional> voi ...

  9. Template、ItemsPanel、ItemContainerStyle、ItemTemplate

    先来看一张图(网上下的图,加了几个字) 1.Template是指控件的样式 在WPF中所有继承自contentcontrol类的控件都含有此属性,(继承自FrameworkElementdl类的Tex ...

  10. Mongodb地理空间索引

    1.索引: 建立索引既耗时也费力,还需要消耗很多资源.使用{"bakckground":true}选项可以使这个过程在后台完成,同时正常处理请求.如果不包括background 这 ...