uva 10125和集 sumset(set)
来源:互联网 发布:ps 淘宝 广告 编辑:程序博客网 时间:2024/05/29 08:17
给定数字集合,找出最大的d,使得a+b+c=d且abc均在数字集合中,集合元素数量不超过1000
思路是把等式变换为d-c=a+b,这样只要枚举d,c,验证d-c是否在任意两元素之和所在的集合中,注意abcd不能重复,所以需要一些额外的判重条件
#include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long using namespace std; const int maxn = 1000 + 5;const int INF = 0x3f3f3f3f;//freopen("input.txt", "r", stdin);multiset<int> su;set<int> sumset;int S[maxn];int n;void init() {su.clear();for(int i = 0; i < n; i++) { scanf("%d", &S[i]); sumset.insert(S[i]); } for(int i = 0; i < n-1; i++) for(int j = i + 1; j < n; j++) { su.insert(S[i]+S[j]); }}void solve() {int ans = -INF;for(int i = 0; i < n-1; i++)for(int j = i + 1; j < n; j++) {int cnt = 0;if(sumset.count(-S[j])) cnt++; if(sumset.count(S[i]-2*S[j])) cnt++;if(!S[j]) cnt--;if(su.count(S[i]-S[j])>cnt) ans = max(ans, S[i]);cnt = 0;if(sumset.count(-S[i])) cnt++; if(sumset.count(S[j]-2*S[i])) cnt++;if(!S[i]) cnt--;if(su.count(S[j]-S[i])>cnt) ans = max(ans, S[j]);}if(ans != -INF) printf("%d\n", ans);else puts("no solution");}int main() {//freopen("input.txt", "r", stdin);while(scanf("%d", &n) == 1 && n) {init();solve();}return 0;}
0 0
- uva 10125和集 sumset(set)
- UVA - 10391 Compound Words 迭代器和set
- Sumset 递推 poj2299
- UVA - 489 Hangman Judge(set应用)
- UVA 1391 - Astronauts(2-SET)
- UVA 11294 - Wedding(Two-Set)
- UVA - 11136Hoax or what(set)
- UVa 11572(滑动窗口+set)
- UVA 1595 找对称轴(set)
- UVA 156 Ananagrams(stl,map,set)
- UVA 11020 Efficient Solutions(STL,set)
- UVA 156 Ananagrams(map、set)
- UVA - 1595 Symmetry set
- UVA 10815 set
- UVa 10815 set
- UVA 1595 Symmetry (set)
- UVA-136 Set
- UVA 1493 Draw a Mess(并查集+set)
- 解放双手——Android自动化测试
- Unity3D占用内存太大的解决方法
- list
- Mac创建无线热点
- 派生类的访问控制-“三看”原则-派生类类成员访问级别设置的原则
- uva 10125和集 sumset(set)
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第一课:游戏原理与机制
- VMware与VirtualBox相互转换
- 超级干货!深聊Material Design复杂响应式设计
- android 开发
- Vim技巧之重复
- SpringMVC通过Redis实现缓存主页
- 一起学习CC3200系列教程之TCP ECHO 服务端 selcet
- 浏览器内核种类