codechef营养题 第二弹
第二弾が始まる!
codechef problems 第二弹
一、Backup Functions
题面
One unavoidable problem with running a restaurant is that occasionally a menu item cannot be prepared. This can be caused by a variety of reasons such as missing ingredients or malfunctioning equipment.
There is an additional problem with such situations. A customer who has spent many minutes deciding between menu items can get quite annoyed when they place the order and the server tells them the item is not available. To mitigate this effect, the Chef declares that all servers must have what he calls a "backup function". This is a function f from menu items to menu items. For each menu item x, f(x) is an alternative menu item that the server may suggest to the customer in the event the customer requests menu item x when x is not available.
Of course, if a given item is not available then some other items make better suggestions than others. So, for some pairs of items x and y the Chef has determined a numeric value describing the effectiveness of the assignment f(x) = y. Higher values indicate the proposed substitute is similar to the original and lower values indicate the proposed substitute is not very similar to the original. Such effectiveness values are symmetric meaning that if the effectiveness of assignment f(x) = y is v, then the effectiveness of the assignment f(y) = x is also v.
You will be given a list of pairs of menu items. Each such pair will come with an associated effectiveness value. You are to compute a backup function f from these pairs. However, there is one additional constraint. For personal reasons, the Chef is opposed to using two items as backups for each other. Thus, for any two menu items x and y, it cannot be that f(x) = y and f(y) = x. Your goal is to compute a backup function of maximum possible quality. The quality of the backup function is simply defined as the sum of the effectiveness values of the assignments f(a) for each item a.
Input
The first line contains a single integer T ≤ 30 indicating the number of test cases.
Each test case begins with two integers N and M where 3 ≤ N ≤ 10,000 and 3 ≤ M ≤ 50,000 where N is the number of items in the menu. The menu items will be numbered 1 through N.
M lines follow, each containing three integers a,b, and v. Here 1 ≤ a < b ≤ N and |v| ≤ 100,000. Such a triple indicates that f(a) = b or f(b) = a (but not both) may be used in a backup function. Setting either f(a) = b or f(b) = a has effectiveness v. Each pair 1 ≤ a < b ≤ N will occur at most once in the input.
Test cases will be separated by a single blank line (including a blank line preceding the first test case).
Output
The output for each test case consists of a single line containing the maximum possible quality of a backup function f that does not assign any pair of items to each other. To be explicit, the assignment f(a) = b has effectiveness v where v is the value associated with the a,b pair in the input. Then the quality of a backup function is just the sum of the effectiveness values of the assignment for each item. If no backup function can be constructed from the given input pairs, then simply output "impossible" (note the lowercase 'i').
We may only assign f(a) = b or f(b) = a if a,b is an input pair. Furthermore, a backup function defines a single item f(a) for every item a between 1 and n. Note that f(a) = a is not valid since f(a) is supposed to be an alternative item if menu item a is not available (and all input pairs a,b have a < b anyway). Finally, based on the Chef's additional constraint we cannot have a backup function with both f(a) = b and f(b) = a for any input pair a,b.
Example
Input:
- - -
Output:
impossible
Note, one possible solution for the first test case is f(1) = 2, f(2) = 3, and f(3) = 1. The second is impossible since the only possiblity for f(5) is 6 and the only possiblity for f(6) is 5 and both cannot be used in a backup function. Finally, a possible solution for the last test case is f(1) = 2, f(2) = 3, f(3) = 1, and f(4) = 2.
Description
给定N道菜,M个替换关系
替换关系描述为三元组(a,b,c),表示a和b两道菜可以相互作为备选菜,当a或b没法做时,b或a可以作为它的备用菜
定义函数f(x)=y,代表菜x的备选菜是y
特别地,不允许f(x)=y and f(y)=x
Solution
并查集,把关系排序后能加就加
#include <stdio.h>
#include <algorithm>
#define N 10010
#define M 50010
#define Buf 1<<22
#define RG register
#define Blue() ((S==T&&(T=(S=B)+fread(B,1,Buf,stdin),S==T))?0:*S++)
char B[Buf],*S=B,*T=B;
template<class Type>inline void Rin(RG Type &x)
{
x=;RG int c=Blue();RG bool b=false;
for(;c<||c>;c=Blue())
if(c==)b=true;
for(;c>&&c<;c=Blue())
x=(x<<)+(x<<)+c-;
x=b?-x:x;
}
bool r[N];
int k,n,m,f[N],a,c,tx,ty;
struct L
{
int u,v,w;
bool operator < (const L &o) const
{
return w<o.w;
}
}e[M];
inline int fd(RG int x)
{
for(;x!=f[x];x=f[x]=f[f[x]]);
return x;
}
#define FO(x) {freopen(#x".in","r",stdin);}
int main(){
FO(cc backup func);
Rin(k);
while(k--)
{
Rin(n),Rin(m);
for(RG int i=;i<=m;i++)
Rin(e[i].u),Rin(e[i].v),Rin(e[i].w);
std::sort(e+,e++m);
for(RG int i=;i<=n;i++)
f[i]=i,r[i]=;
a=c=;
while(c<n&&m)
{
tx=fd(e[m].u),ty=fd(e[m].v);
if(tx!=ty)
{
if(r[tx]|r[ty])
++c,
f[tx]=ty,
r[ty]&=r[tx],
a+=e[m].w;
}
else if(r[ty])
++c,
r[ty]=,
a+=e[m].w;
m--;
}
c==n?printf("%d\n",a):puts("impossible");
}
return ;
}
二、magic hull
题面
You are given a set of N two-dimensional non-zero vectors (X1, Y1), (X2, Y2), ..., (XN, YN) with integer coordinates. You can choose no more than 6 vectors from this set to form a non-strictly convex polygon that have sides equal to these vectors (see paragraph below for more detailed explanation). Non-strictly convex polygon is the polygon that can have flat angles (angles of 180 degrees). You can select each vector several times if needed. Your goal is to maximize the area of this polygon. It is guaranteed that at least one such convex polygon can be constructed.
How exactly we construct a polygon from the given sequence of vectors? Suppose you've chosen the sequence of vectors (U1, V1), (U2, V2), ..., (UK, VK), where 3 ≤ K ≤ 6. Each vector here should be equal to one of the given N vectors but some vectors in this sequence can coincide. At first, you take some point (A1, B1) at the plane as the first vertex of your polygon. Then you put your first vector (U1, V1) to this point to obtain the second vertex (A2, B2) = (A1 + U1, B1 + V1). Next you take the second vector and put it to the second vertex to obtain the third vertex and so on. Finally, at the last step you put vector (UK, VK) to the Kthvertex (AK, BK) to obtain the point (AK + 1, BK + 1) = (AK + UK, BK + VK) that should coincide with the first vertex (A1, B1), otherwise we should reject this sequence of vectors. Hence in the end we obtain a polygon with vertexes (A1, B1), (A2, B2), ..., (AK, BK). If this polygon is simple (without self-intersections) and non-strictly convex, we should consider its area as the candidate for the answer. By self-intersection we mean that either some non-consecutive sides (considered with their ends) have common point or two consecutive sides have more than one common point.
Input
The first line of the input file contains an integer N, the number of the given vectors. Each of the following N lines contains two space separated integers Xi, Yi, coordinates of the corresponding vector.
Output
In the only line of the output file print the maximal possible area of the convex polygon that can be constructed by the rules described in the problem statement with exactly one digit after the decimal point.
Constraints
3 ≤ N ≤ 150
|Xi|, |Yi| ≤ 1000000
All vectors are non-zero: |Xi| + |Yi| > 0
There exists at least one sequence of at most 6 vectors from this set that forms a convex polygon.
Example
Input:
-
-
- -
Output:
2.0
Explanation
The only non-strictly convex polygon you can construct from these vectors by the rules described in the problem statement is the isosceles trapezoid that has height of length 1 and bases of length 1 and 3. It has area 2.0.
Description
给你一堆向量,求k个向量组成凸包的最大面积
其中k<=6
Solution
O(n^3)建凸壳
满足和向量互为相反向量的就可以组合成一个凸包
二分+排序
最差时间复杂度(n^3 * log n)
目前codechef上rank1最快,归功于简单短小的变量名
#include <math.h>
#include <stdio.h>
#include <algorithm>
#define L long long
#define N 155
#define U 1<<22
#define dmax(a,b) ((a)>(b)?(a):(b))
#define dabs(a) ((a)<(0)?(-a):(a))
#define F()((I==J&&(J=(I=B)+fread(B,1,U,stdin),I==J))?0:*I++)
char B[U],*I=B,*J=B;
template<class T>inline void Rin(T &x)
{
x=;int c=F();bool b=;
for(;c<||c>;c=F())
if(c==)b=;
for(;c>&&c<;c=F())
x=(x<<)+(x<<)+c-;
x=b?-x:x;
}
struct VV
{
int x,y;
VV(int _=,int __=)
{
x=_;
y=__;
}
bool operator < (VV o) const
{
return x<o.x||x==o.x&&y<o.y;
}
bool operator == (VV o) const
{
return x==o.x&&y==o.y;
}
L operator ^ (VV o) const
{
return (L)x*o.y-(L)y*o.x;
}
VV operator + (VV o) const
{
return VV(x+o.x,y+o.y);
}
VV operator - () const
{
return VV(-x,-y);
}
int p()
{
return y>||y==&&x>;
}
}V[N];
struct PP
{
VV f;
L s;
PP(VV _=,L __=)
{
f=_;
s=__;
}
bool operator < (PP o) const
{
return f<o.f||f==o.f&&s<o.s;
}
bool operator == (PP o) const
{
return f==o.f&&s==o.s;
}
}P[N*N*N/];
bool A(VV a,VV b)
{
int p=a.p();
int q=b.p();
if(p!=q)
{
return p>q;
}
return (a^b)>;
}
int n,_t,_k;
L ans;
#define FO(x) {freopen(#x".in","r",stdin);}
int main()
{
FO(cc magic hull);
Rin(n);
for(int i=;i<n;i++)
{
Rin(V[i].x);
Rin(V[i].y);
}
std::sort(V,V+n,A);
//按角度大小排序,不用atan2等库函数防止浮点误差,本题没有这样的特殊要求;
for(int i=;i<n;i++)
{
for(int j=i;j<n;j++)
{
L m=V[i]^V[j];
if(m<)
{
break;
}
m=dabs(m);
P[_t++]=PP(V[i]+V[j],m);
//P数组用于存储和向量及面积,多边形面积并直接叉积就行;
for(int k=j;k<n;k++)
{
if((V[j]^V[k])<)
{
break;
}
VV t=V[i]+V[j]+V[k];
if((V[i]^t)>=)
{
P[_t++]=PP(t,m+dabs(V[k]^t));
}
}
}
}
std::sort(P,P+_t);
for(int i=;i<_t;)
{
int j=i;
for(;j<_t&&P[i].f==P[j].f;j++)
;
P[_k++]=P[j-];
i=j;
}
for(int i=;i<_k;i++)
{
VV t=-P[i].f;
int j=std::lower_bound(P,P+_k,PP(t,-1LL))-P;
if(j<_k&&P[j].f==t)
{
ans=dmax(ans,P[i].s+P[j].s);
}
}
printf("%lld.%lld\n",ans/,ans%*);
return ;
}
codechef营养题 第二弹的更多相关文章
- codechef 营养题 第一弹
第一弾が始まる! 定期更新しない! 来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5 ...
- codechef营养题 第三弹
第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...
- 关于『进击的Markdown』:第二弹
关于『进击的Markdown』:第二弹 建议缩放90%食用 众里寻他千百度,蓦然回首,Markdown却在灯火灿烂处 MarkdownYYDS! 各位早上好! 我果然鸽稿了 Markdown 语法 ...
- 浅谈Hybrid技术的设计与实现第二弹
前言 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 浅谈Hybrid技术的设计与实现第三弹——落地篇 接上文:浅谈Hybrid技术的设计与实现(阅读本文前,建议阅读这个先) ...
- 前端学习 第二弹: JavaScript中的一些函数与对象(1)
前端学习 第二弹: JavaScript中的一些函数与对象(1) 1.apply与call函数 每个函数都包含两个非继承而来的方法:apply()和call(). 他们的用途相同,都是在特定的作用域中 ...
- 青瓷引擎之纯JavaScript打造HTML5游戏第二弹——《跳跃的方块》Part 10(排行榜界面&界面管理)
继上一次介绍了<神奇的六边形>的完整游戏开发流程后(可点击这里查看),这次将为大家介绍另外一款魔性游戏<跳跃的方块>的完整开发流程. (点击图片可进入游戏体验) 因内容太多,为 ...
- typecho流程原理和插件机制浅析(第二弹)
typecho流程原理和插件机制浅析(第二弹) 兜兜 393 2014年04月02日 发布 推荐 1 推荐 收藏 14 收藏,3.7k 浏览 上一次说了 Typecho 大致的流程,今天简单说一下插件 ...
- LCA问题第二弹
LCA问题第二弹 上次用二分的方法给大家分享了对 LCA 问题的处理,各位应该还能回忆起来上次的方法是由子节点向根节点(自下而上)的处理,平时我们遇到的很多问题都是正向思维处理困难而逆向思维处理比较容 ...
- 线段树+RMQ问题第二弹
线段树+RMQ问题第二弹 上篇文章讲到了基于Sparse Table 解决 RMQ 问题,不知道大家还有没有印象,今天我们会从线段树的方法对 RMQ 问题再一次讨论. 正式介绍今天解决 RMQ 问题的 ...
随机推荐
- C#面向过程之局部变量、成员变量、变量作用域、可变参数
局部变量与成员变量: 局部变量:定义在方法里面的变量就叫做局部变量:没有默认的初始值,使用变量之前必须给它赋值成员变量:定义在类下面的变量叫做成员变量:如果是数值类型默认初始值为0 如果是引用类型默 ...
- 【FFmpeg】FFmpeg常用基本命令(转载)
转自:http://www.cnblogs.com/dwdxdy/p/3240167.html 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an outp ...
- 【第三届强网杯】write up
一,鲲or鳗orGame 从浏览器里拿到game.gb 用 VisualBoyAdvance ,搜索内存变量,然后改变量就完事了,但是改变量他们说直接利用添加代码功能有bug,所以要在工具里的内存查看 ...
- C++小项目-吃豆子游戏
GMap.h #pragma once //保证头文件只被编译一次 #include "stdafx.h" #define MAP_LEN 19 //逻辑地图大小 (逻辑地图由行. ...
- [ZJOI2005]沼泽鳄鱼
题目描述 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎然,引来不少游客. 为了让游玩更有情趣,人们在池塘的中央建设了几座石墩和石桥 ...
- 395 Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子串
找到给定字符串(由小写字符组成)中的最长子串 T , 要求 T 中的每一字符出现次数都不少于 k .输出 T 的长度.示例 1:输入:s = "aaabb", k = 3输出:3最 ...
- 窗体基础WINFORM
winform 1.窗体: 造窗体界面: 窗体设计界面: 窗体类名不能重复! 属性: acceptbutton:回车是默认点击按钮 cancelbutton:按esc按键式默认的按钮 backcolo ...
- LN : leetcode 538 Convert BST to Greater Tree
lc 538 Convert BST to Greater Tree 538 Convert BST to Greater Tree Given a Binary Search Tree (BST), ...
- Android APK瘦身之webp图片
webp格式是谷歌推出的一种有损压缩格式,这种图片格式相比png或者jpg格式的图片损失的质量几乎可以忽略不计,但是压缩后图片的体积却比png或者jpg要小很多.亲测一个100kb的png图片经过we ...
- android中用Intent传数据,如果用传递的是一个类,就将类实现Parcelable接口
Parcelable,内存单位,跨进程使用,或者intent传递对象的时候使用.android中用Intent传数据,如果用传递的是一个对象,就将对象实现Parcelable接口,而不是将对象序列化. ...