poj 2549 Sumsets 折半枚举
来源:互联网 发布:i5处理器编程够用么 编辑:程序博客网 时间:2024/06/04 19:00
http://poj.org/problem?id=2549
下面是第一次wa代码:
题意:给你n个数字,从中挑出四个数字使得a+b+c=d求d得最大值;
思想:折半枚举,否则n^4的复杂度;不过很多细节地方要注意;还要注意lower_bound在结构体排序的
应用;
#include <iostream>
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int v, x, y, p, d;
bool operator <(const Node& b) const
{
return v<b.v;
}
};
Node nodeb[1000005], nodec[1000005];
int a[1005];
bool cmp(Node a, Node b)
{
return a.v<b.v;
}
int main()
{
int n;
while (~scanf("%d", &n)&&n)
{
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int cnt1 = 0, cnt2 = 0, ans = 0, flag = 0;
for (int i = 1; i <= n-1; i++)
for (int j = i + 1; j <= n; j++)
{
++cnt1;
nodeb[cnt1].v = a[i] + a[j];
nodeb[cnt1].x =i;
nodeb[cnt1].y =j;
};
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (i == j)
continue;
else
{
++cnt2;
nodec[cnt2].v = a[i]- a[j];
nodec[cnt2].x = i;
nodec[cnt2].y = j;
nodec[cnt2].d = a[i];
};
sort(nodec + 1, nodec + cnt2 + 1, cmp);
for (int i = 1; i <= cnt1; i++)
{
int m = nodeb[i].v;
Node temp; temp.v = m;
int k = lower_bound(nodec + 1, nodec + cnt2 + 1, temp) - nodec;
if (m==nodec[k].v)
if (nodec[k].x != nodeb[i].x&&nodec[k].x != nodeb[i].y
&&nodec[k].y != nodeb[i].x&&nodec[k].y != nodeb[i].y)
{
flag = 1;
ans = ans<nodec[k].d ? nodec[k].d : ans;
}
}
if (!flag)
printf("no solution\n");
else
printf("%d\n", ans);
}
return 0;
}
#include <iostream>
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
struct Node{
int v,x,y,p,d;
bool operator <(const Node& b) const
{
return v<b.v;
}
};
Node nodea[1005],nodeb[1005],nodec[1005];;
bool cmp(Node a,Node b)
{
return a.v<b.v;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&nodea[i].v);
nodea[i].p=i;
}
int cnt1=0,cnt2=0,ans=0,flag=0;
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(i==j)
continue;
else
{
nodeb[++cnt1].v=nodea[i].v+nodea[j].v;
nodeb[cnt1].x=nodea[i].p;
nodeb[cnt1].y=nodea[j].p;
};
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
if(i==j)
continue;
else
{
nodec[++cnt2].v=nodea[i].v-nodea[j].v;
nodec[cnt2].x=nodea[i].p;
nodec[cnt2].y=nodea[j].p;
nodec[cnt2].d=nodea[i].v;
};
sort(nodec+1,nodec+cnt2+1,cmp);
for(int i=1;i<=cnt1;i++)
{
int m=nodeb[i].v;
Node temp;temp.v=-m;
int k=lower_bound(nodec+1,nodec+cnt2+1,temp)-nodec;
if(m+nodec[k].v==0)
if(nodec[k].x!=nodeb[i].x&&nodec[k].x!=nodeb[i].y
&&nodec[k].y!=nodeb[i].x&&nodec[k].y!=nodeb[i].y)
{
flag=1;
ans=ans<nodec[k].d?nodec[k].d:ans;
}
}
if(!flag)
printf("no solution\n");
else
printf("%d\n",ans);
}
return 0;
}
阅读全文
0 0
- POJ 2549 Sumsets(折半枚举)
- POJ - 2549 Sumsets(折半枚举)
- poj 2549 Sumsets (折半枚举)
- poj 2549 Sumsets 折半枚举
- poj 2549 Sumsets(折半枚举)
- POJ2549 Sumsets 折半枚举
- poj2549 Sumsets(折半枚举)
- poj 2549 Sumsets (枚举+二分)
- Poj 2549 Sumsets【巧妙枚举】
- 【POJ 2549 Sumsets】+ 技巧枚举
- UVa 10125 Sumsets (折半枚举&二分查找)
- 10125 - Sumsets(折半枚举+二分)
- UVA 10125 Sumsets 折半枚举 + 二分
- POJ 2785 折半枚举
- 【折半枚举 && 二分】POJ
- poj 2549 Sumsets
- poj 2549 Sumsets
- POJ 2549 Sumsets
- poj 2674 Linear world 弹性碰撞 升级的蚂蚁
- poj 2785 4 Values whose Sum is 0
- Codeforces Round #324 (Div. 2) A. Olesya and Rodion 构造数字 思维题
- Codeforces Round #324 (Div. 2) B. Kolya and Tanya 思维题 数论
- Poj 3977 Subset 折半枚举 超大背包
- poj 2549 Sumsets 折半枚举
- poj 3276 Face The Right Way 挑战150 反转
- poj 3279 Fliptile 反转
- poj 3185 The Water Bowls 反转(开关)
- poj 1930 Dead Fraction 循环小数的处理
- poj 3421 X-factor Chains 素数筛选 因子分解
- poj 3292 Semi-prime H-numbers 素数变形+打表+筛选法
- poj 3468 A Simple Problem with Integers 模板题 线段树 懒惰标记
- POJ 3264 Balanced Lineup 线段树 维护区间最大值和最小值 建树