STL 全排列

The Little Girl who Picks Mushrooms

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1805    Accepted Submission(s): 579

Problem Description

It‘s yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity
of 2012 grams. Alice has finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five
mountains, she want to bring as much mushrooms as possible home to cook a delicious soup.

Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice‘s way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral
kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.

Somewhere in the forest of magic near Alice‘s house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what‘s the maximum possible amount
of mushrooms Alice has? Remember that our common sense doesn‘t always hold in Gensokyo. People in Gensokyo believe that 1 kilogram is equal to 1024 grams.

Input

There are about 8192 test cases. Proceed to the end of file.

The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.

Output

For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).

Sample Input

1
9
4
512 512 512 512
5
100 200 300 400 500
5
208 308 508 708 1108

Sample Output

1024
1024
0
792

Hint

In the second sample, if Alice doesn‘t pick any mushrooms from the 5-th mountain. She can give (512+512+0) =1024 grams of mushrooms to Sunny, Lunar and
Star. Marisa won‘t steal any mushrooms from her as she has exactly 1 kilogram of mushrooms in total.
In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms.
In the last sample:
1.Giving Sunny, Lunar and Star: (208+308+508)=1024
2.Stolen by Marisa: ((708+1108)-1024)=792

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

#include <ctype.h>

#include <string.h>

#include <string>

#include <queue>

#include <stack>

#include <deque>

#include <vector>

#include <set>

#include <map>

#include <algorithm>

#include <iostream>

#include <limits.h>

using namespace std;

int Max(int a,int b){

return a>b?a:b;

}

int main(){

int num[10],a[10];

int n,i;

while(~scanf("%d",&n)){

int sum = 0;

for(i=0;i<5;i++){

num[i] = i;

}

for(i=0;i<n;i++){

scanf("%d",&a[i]);

sum+=a[i];

}

if(n <= 3){

printf("1024\n");

continue;

}

if(n == 4){

int cot = 0;

do{

int ans = 0;

if(num[0] == 4){

ans = sum-a[num[1]]-a[num[2]];

}

else if(num[1] == 4){

ans = sum-a[num[0]]-a[num[2]];

}

else if(num[2] == 4){

ans = sum-a[num[0]]-a[num[1]];

}

else{

ans+=a[num[0]]+a[num[1]]+a[num[2]];

if(ans%1024 == 0){

ans = 1024;

}

else{

ans = 0;

}

}

while(ans > 1024){

ans-=1024;

}

cot = Max(cot,ans);

}while(next_permutation(num,num+5));

printf("%d\n",cot);

}

if(n == 5){

int cot = 0;

do{

int ans = 0;

int x = 0;

x+=a[num[0]]+a[num[1]]+a[num[2]];

if(x%1024 == 0){

ans = sum-x;

}

//else{

//    ans = 0;

//}

while(ans > 1024){

ans-=1024;

}

cot = Max(cot,ans);

}while(next_permutation(num,num+5));

printf("%d\n",cot);

}

}

return 0;

}

时间: 2024-08-25 00:24:05

STL 全排列的相关文章

hdu 1027 Ignatius and the Princess II (STL 全排列)

题目链接今天学了 全排列函数 之后,再回过头来看这一题,发现这时对于这样的题 就是一个字 秒 .主要函数有两个 next_permutation 和 prev_permutation这两个一个是向后找 一个是向前找,next的是往后,prev的是向前找.有的人可能不太明白我这里只的向前和向后的意思. 向前 就是 往 字典序小 的 方向 找 ,反之 就是向前. 举个例子把 :假设数组a[n],i<=m,next_permutation(a+i,a+m)就表示对a[i]到a[m]进行操作,每操作一次

STL全排列算法next_permutation和prev_permutation

全排列的问题取决于如何找到"下一个",然后就用同样的方法找到全部的排列 全排列这个问题其实和我们数数:11,12,13,14,15,16,17,18,19,20,21,一样的规律,只不过他的变化只要那固定的几个数自每次都出现,这就导致我们传统的连续整数会在那里丢失一部分:你如何对剩下的这些全排列中的数字看成是"连续的"一列数字,对于"第一个""最小的数字"找到它的"下一个",然后用同样的方法找到再"

NYOJ 366 STL 全排列

D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡出了个题目想难倒匡匡(小L很D吧),有一个数n(0<n<10),写出1到n的全排列,这时匡匡有点囧了,,,聪明的你能帮匡匡解围吗? 输入 第一行输入一个数N(0<N<10),表示有N组测试数据.后面的N行输入多组输入数据,每组输入数据都是一个整数x(0<x<10) 输出 按

C++ STL 全排列函数详解

一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a,b,c的全排列一共有3!= 6 种 分别是{a, b, c}.{a, c, b}.{b, a, c}.{b, c, a}.{c, a, b}.{c, b, a}. 二.常用操作 1.头文件 #include <algorithm> 2.使用方法 这里先说两个概念:"下一个排列组合&qu

c++STL全排列

包含在c++<algorithm>库中的next_permutation(arr,arr+n)函数可以实现arr中元素的全排列 但是要求arr中元素事先已经按字典序排列好 具体使用方法如下: #include <iostream> #include <algorithm> using namespace std; int arr[5]={1,3,2,3,4}; int main() { sort(arr,arr+5); while(next_permutation(ar

ACM题目————STL + 全排列

今天碰到一个函数,感觉挺好用的,全排列函数 next_permutation! 他可以遍历全排列哦! 话不多说,直接上题. 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长.现在需要你写一个程序来验证擅长排列的小明到底对不对. 输入 第一行输入整数N(1<N<10)表示多少组测试数据, 每组测试数据第一行两个整数 n m (1<n<9,0<

(六)全排列permutation

参考: C++ STL 全排列函数详解 算法思路: (1)n个元素的全排列=(n-1个元素的全排列)+(另一个元素作为前缀): (2)出口:如果只有一个元素的全排列,则说明已经排完,则输出数组: (3)不断将每个元素放作第一个元素,然后将这个元素作为前缀,并将其余元素继续全排列,等到出口,出口出去后还需要还原数组: 使用STL的std::next_permutation函数 void PermutationTest() { string s = "abc"; cout <<

排列组合递归和非递归算法总结篇

#include <iostream> #include <string> #include <math.h> #include <vector> #include <algorithm> using namespace std; //method1 bool flag[5] ; int arr[5] = {1,2,3,4,5}; int len = sizeof(arr)/sizeof(int); void Comb(int n,int cou

第七章素数环

在网上搜索了一下我的第一种方法应该是减枝的策略,回溯也是减枝的一种 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; const int maxn=20; int ans[maxn]; int n; bool prime(int num) { if(num==2 || num==1) return true; for(int i=2;i<num;i++)