A Birthday Cake
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
double r;
map<string,bool>mp;
struct point {
double x,y;
}p[51];
struct line{
point a,b;
}l[16];
double xmult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool side(point p1,line l)
{
return xmult(l.a,p1,l.b)>0;
}
point intersection(point u1,point u2,point v1,point v2)
{
point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
double distance(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
void intersection_line_circle(point c,double r,point l1,point l2,point& p1,point& p2)
{
point p=c;
double t;
p.x+=l1.y-l2.y;
p.y+=l2.x-l1.x;
p=intersection(p,c,l1,l2);
t=sqrt(r*r-distance(p,c)*distance(p,c))/distance(l1,l2);
p1.x=p.x+(l2.x-l1.x)*t;
p1.y=p.y+(l2.y-l1.y)*t;
p2.x=p.x-(l2.x-l1.x)*t;
p2.y=p.y-(l2.y-l1.y)*t;
}
bool isIntersected(point s1,point e1, point s2,point e2)
{
return (max(s1.x,e1.x) >= min(s2.x,e2.x)) &&
(max(s2.x,e2.x) >= min(s1.x,e1.x)) &&
(max(s1.y,e1.y) >= min(s2.y,e2.y)) &&
(max(s2.y,e2.y) >= min(s1.y,e1.y)) &&
(xmult(s1,s2,e1)*xmult(s1,e1,e2)>0) &&
(xmult(s2,s1,e2)*xmult(s2,e2,e1)>0);
}
int main(){
cin>>n>>m>>r;
for(ll i=1;i<=n;i++){
cin>>p[i].x>>p[i].y;
}
for(ll i=1;i<=m;i++){
double a,b,c;
cin>>a>>b>>c;
point p0,p1;
if(b!=0){
p0={0,-c/b};
p1={1,-(c+a)/b};
}
else{
p0={-c/a,0};
p1={-(c+b)/a,1};
}
intersection_line_circle({0,0},r,p0,p1,l[i].a,l[i].b);
}
bool flag=true;
for(ll i=1;i<=n;i++){
string s;
for(ll j=1;j<=m;j++){
if(side(p[i],l[j])){
s+='0';
}
else{
s+='1';
}
}
if(mp[s]){
flag=false;
}
else{
mp[s]=true;
}
}
ll cnt=m+1;
for(ll i=1;i<=m;i++){
for(ll j=1;j<i;j++){
if(isIntersected(l[i].a,l[i].b,l[j].a,l[j].b)){
cnt++;
}
}
}
if(cnt!=n)flag=false;
if(flag)
puts("yes");
else
puts("no");
}
B Bumped!
#include<bits/stdc++.h>
using namespace std;
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
const ll maxn=5e4+10;
ll n,m,s,t,f;
struct qnode{
ll v,dis,level;
qnode(ll v=0,ll dis=0,ll level=0):v(v),dis(dis),level(level) {}
bool operator<(const qnode &r)const
{
return dis>r.dis;
}
};
struct Edge{
ll v,w;
Edge(ll v=0,ll w=0):v(v),w(w) {}
};
vector<Edge> ve[maxn];
ll d[maxn][2];
bool vis[maxn][2];
void addEdge(ll u,ll v,ll w)
{
ve[u].push_back(Edge(v,w));
}
void dijkstra(ll start)
{
mem(vis,false);
mem(d,inf);
priority_queue<qnode> q;
d[start][0]=0;
q.push(qnode(start,0,0));
qnode tmp;
while(!q.empty())
{
tmp=q.top();
q.pop();
ll u=tmp.v;
ll lv=tmp.level;
if(vis[u][lv])continue;
vis[u][lv]=true;
for(ll i=0; i<ve[u].size(); i++)
{
ll v=ve[u][i].v;
ll w=ve[u][i].w;
if(w!=0){
if(w+d[u][lv]<d[v][lv])
{
d[v][lv]=w+d[u][lv];
q.push(qnode(v,d[v][lv],lv));
}
}
else if(w==0){
if(lv==0&&d[u][lv]<d[v][lv+1])
{
d[v][lv+1]=d[u][lv];
q.push(qnode(v,d[v][lv+1],lv+1));
}
}
}
}
}
int main()
{
scanf("%lld%lld%lld%lld%lld",&n,&m,&f,&s,&t);
for(ll i=0; i<m; i++)
{
ll u,v,w;scanf("%lld%lld%lld",&u,&v,&w);
addEdge(u,v,w); addEdge(v,u,w);
}
for(ll i=0;i<f;i++){
ll u,v;
scanf("%lld%lld",&u,&v);
addEdge(u,v,0);
}
dijkstra(s);
printf("%lld\n",min(d[t][0],d[t][1]));
return 0;
}
C Canonical Coin Systems
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=2e6+10;
ll a[1001];
ll dp[maxn];
ll ans1[maxn];
const ll INF=0x3f3f3f3f;
int main(){
ll n;
memset(dp,INF,sizeof(dp));
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
bool flag=true;
dp[0]=0;
for(ll i=1;i<=2*a[n];i++){
for(ll j=1;j<=n&&i-a[j]>=0;j++){
dp[i]=min(dp[i-a[j]]+1,dp[i]);
ans1[i]=ans1[i-a[j]]+1;
}
if(dp[i]!=ans1[i]){
flag=false;
break;
}
}
if(flag)puts("canonical");
else puts("non-canonical");
}
D Cat and Mice
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
double x[20],y[20],s[20],m[20];
double dp[1<<15][20];
double INF=1e20;
ll n;
ll numbersof1(ll n){
int count = 0;
while(n != 0){
++count;
n = (n-1) & n;
}
return count;
}
double DIS(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double TIME(double dis,double v){
return dis/v;
}
bool check(double v){
for(ll i=0;i<(1<<n);i++){
for(ll j=0;j<=n;j++){
dp[i][j]=INF;
}
}
dp[0][0]=0;
for(ll i=1;i<(1<<n);i++){
ll cnt=numbersof1(i);
for(ll j=1;j<=n;j++){
if(i&(1<<(j-1))){
ll pre=i^(1<<(j-1));
for(ll k=0;k<=n;k++){
if(cnt!=1&&k==0)continue;
if(k!=0&&(pre&(1<<(k-1))==0))continue;
if(dp[pre][k]==INF)continue;
double curv=m[cnt-1]*v;
double curdis=DIS(x[j],y[j],x[k],y[k]);
double curtime=TIME(curdis,curv);
if(dp[pre][k]+curtime>s[j])continue;
dp[i][j]=min(dp[i][j],dp[pre][k]+curtime);
}
}
}
}
for(ll i=1;i<=n;i++){
if(dp[(1<<n)-1][i]!=INF)
return true;
}
return false;
}
int main(){
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
scanf("%lf%lf%lf",&x[i],&y[i],&s[i]);
}
m[0]=1;
scanf("%lf",&m[1]);
for(int i=2;i<20;i++){
m[i]=m[i-1]*m[1];
}
double l=0,r=1e20;
while(r-l>1e-6){
double mid=(l+r)/2;
if(check(mid))
r=mid;
else
l=mid;
}
printf("%.12f\n",r);
}
E Company Picnic
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e3+10;
ll head[maxn];
ll cnt,n,root;
map<string,ll>mp;
map<ll,string>mp2;
struct edge{
ll v,next;
}e[maxn];
void add(ll u,ll v){
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
double a[maxn];
pair<ll,double>dp[maxn][2];
void DP(ll u){
for(ll i=head[u];~i;i=e[i].next){
ll v=e[i].v;
DP(v);
dp[u][0].first+=dp[v][1].first;
dp[u][0].second+=dp[v][1].second;
}
dp[u][1]=dp[u][0];
for(ll i=head[u];~i;i=e[i].next){
ll v=e[i].v;
pair<ll,double>cur;
cur.first=dp[u][0].first-dp[v][1].first+dp[v][0].first+1;
cur.second=dp[u][0].second-dp[v][1].second+dp[v][0].second+min(a[v],a[u]);
if(cur>dp[u][1])
dp[u][1]=cur;
}
}
int main(){
memset(head,-1,sizeof(head));
cin>>n;
ll num=0;
for(ll i=1;i<=n;i++){
string U,V;
double w;
cin>>V>>w>>U;
ll u,v;
if(!mp[V]){
mp[V]=++num;
mp2[num]=V;
}
v=mp[V];
a[v]=w;
if(U=="CEO"){
root=v;
}
else{
if(!mp[U]){
mp[U]=++num;
mp2[num]=U;
}
u=mp[U];
add(u,v);
}
}
DP(root);
ll ans1=dp[root][1].first;
double ans2=dp[root][1].second/ans1;
printf("%lld %.12f\n",ans1,ans2);
}
F GlitchBot
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string,ll>mp;
map<ll,string>mp2;
ll d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main(){
ll X,Y,n;
string s[51];
scanf("%lld%lld%lld",&X,&Y,&n);
mp["Forward"]=1;
mp2[1]="Forward";
mp["Left"]=2;
mp2[2]="Left";
mp["Right"]=3;
mp2[3]="Right";
for(ll i=1;i<=n;i++){
cin>>s[i];
}
ll ans1;
string ans2;
for(ll i=1;i<=n;i++){
string S=s[i];
bool FLAG=false;
for(ll j=1;j<=3;j++){
if(j==mp[S])continue;
s[i]=mp2[j];
ll curx=0,cury=0,curd=0;
bool flag=true;
for(ll k=1;k<=n;k++){
if(mp[s[k]]==1){
curx+=d[curd][0];
cury+=d[curd][1];
}
else if(mp[s[k]]==2){
curd=(curd+3)%4;
}
else
curd=(curd+1)%4;
if(curx<-50||curx>50||cury<-50||cury>50){
flag=false;
}
}
if(flag&&curx==X&&cury==Y){
ans1=i,ans2=mp2[j];
FLAG=true;
break;
}
}
if(FLAG)break;
s[i]=S;
}
cout<<ans1<<' '<<ans2<<endl;
}
G Greeting Card
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map< pair<ll,ll>,ll>mp;
pair<ll,ll>pr[100010];
int main(){
ll n;
scanf("%lld",&n);
for(ll i=1;i<=n;i++){
ll x,y;
scanf("%lld%lld",&x,&y);
mp[make_pair(x,y)]=i;
pr[i].first=x;
pr[i].second=y;
}
ll ans=0;
for(ll i=1;i<=n;i++){
ll x=pr[i].first;
ll y=pr[i].second;
if(mp[make_pair(x+2018,y+0)]){
ans++;
}
if(mp[make_pair(x+0,y+2018)]){
ans++;
}
if(mp[make_pair(x+1118,y+1680)]){
ans++;
}
if(mp[make_pair(x+1680,y+1118)]){
ans++;
}
if(mp[make_pair(x-2018,y+0)]){
ans++;
}
if(mp[make_pair(x+0,y-2018)]){
ans++;
}
if(mp[make_pair(x-1118,y+1680)]){
ans++;
}
if(mp[make_pair(x-1680,y+1118)]){
ans++;
}
if(mp[make_pair(x+1118,y-1680)]){
ans++;
}
if(mp[make_pair(x+1680,y-1118)]){
ans++;
}
if(mp[make_pair(x-1118,y-1680)]){
ans++;
}
if(mp[make_pair(x-1680,y-1118)]!=0){
ans++;
}
}
cout<<ans/2<<endl;
}
H Imperfect GPS
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+10;
ll n;
ll t;
ll x[1001],y[1001],s[1001];
double X[maxn],Y[maxn];
double dis(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main(){
cin>>n>>t;
double dis1=0,dis2=0;
for(ll i=0;i<n;i++){
cin>>x[i]>>y[i]>>s[i];
if(i==0)continue;
dis1+=dis(1.0*x[i],1.0*y[i],1.0*x[i-1],1.0*y[i-1]);
}
X[0]=x[0]*1.0,Y[0]=y[0]*1.0;
for(ll i=1;i<n;i++){
for(ll j=s[i-1]+1;j<s[i];j++){
X[j]=X[s[i-1]]+1.0*(x[i]-x[i-1])*(j-s[i-1])/(s[i]-s[i-1]);
Y[j]=Y[s[i-1]]+1.0*(y[i]-y[i-1])*(j-s[i-1])/(s[i]-s[i-1]);
}
X[s[i]]=x[i]*1.0,Y[s[i]]=y[i]*1.0;
}
ll TIME;
for(TIME=0;TIME+t<=s[n-1];TIME+=t){
dis2+=dis(X[TIME],Y[TIME],X[TIME+t],Y[TIME+t]);
}
dis2+=dis(X[TIME],Y[TIME],X[s[n-1]],Y[s[n-1]]);
double ans=(dis1-dis2)/dis1*100;
printf("%.12f\n",ans);
}
I Odd Gnome
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[1010];
int main(){
ll t;
scanf("%lld",&t);
while(t--){
ll n;
scanf("%lld",&n);
ll ans=0;
for(ll i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(ll i=2;i<=n;i++){
if(a[i]!=a[i-1]+1){
ans=i;
break;
}
}
printf("%lld\n",ans);
}
}
J Progressive Scramble
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<char,ll>mp;
map<ll,char>Mp;
ll a[1001];
int main(){
mp[' ']=0;
Mp[0]=' ';
for(ll i=1;i<=26;i++){
mp['a'+i-1]=i;
Mp[i]='a'+i-1;
}
ll n;
scanf("%lld",&n);
getchar();
while(n--){
string s;
getline(cin,s);
ll sz=s.size();
char op=s[0];
s=s.substr(2,sz-2);
sz-=2;
if(op=='e'){
for(ll i=0;i<sz;i++){
a[i]=mp[s[i]];
}
for(ll i=1;i<sz;i++){
a[i]+=a[i-1];
a[i]%=27;
}
for(ll i=0;i<sz;i++){
s[i]=Mp[a[i]];
}
cout<<s<<endl;
}
else{
for(ll i=0;i<sz;i++){
a[i]=mp[s[i]];
}
for(ll i=sz-1;i>=1;i--){
a[i]-=a[i-1];
a[i]=(a[i]+27)%27;
}
for(ll i=0;i<sz;i++){
s[i]=Mp[a[i]];
}
cout<<s<<endl;
}
}
}
K Space Probe
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e4+10;
const ll maxm=1e7+10;
ll a[maxn];
ll b[maxn],e[maxn];
pair<ll,ll>se[maxm];
pair<ll,ll>st[maxm];
int main(){
ll n,k,t1,t2;
cin>>n>>k>>t1>>t2;
for(ll i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(ll i=1;i<=k;i++){
scanf("%lld%lld",&b[i],&e[i]);
}
ll cnt=0;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=k;j++){
ll s=b[j]-a[i];
ll t=e[j]-a[i];
s=max(s,t1);
t=min(t,t2);
if(s>=t)continue;
se[cnt++]=make_pair(s,t);
}
}
sort(se,se+cnt);
ll sz=unique(se,se+cnt)-se;
ll top=0;
for(ll i=0;i<sz;i++){
if(top&&se[i].first<=st[top].second){
se[i].first=st[top].first;
se[i].second=max(se[i].second,st[top].second);
top--;
}
st[++top]=se[i];
}
ll len=0;
while(top){
pair<ll,ll>x=st[top--];
len+=x.second-x.first;
}
double ans=1.0*(t2-t1-len)/(1.0*(t2-t1));
printf("%.12f\n",ans);
}
L Suspension Bridges
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const double INF=1e20;
int main(){
double d,s;
scanf("%lf%lf",&d,&s);
d/=2.0;
double l=0,r=INF;
while(r-l>1e-6){
double mid=(l+r)/2;
if(mid+s*1.0<mid*coshl(d/mid)){
l=mid;
}
else{
r=mid;
}
}
double ans=l*2*sinhl(d/l);
printf("%.8f\n",ans);
}
M Umbral Decoding
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e8+10;
map<ll,vector<pair<ll,ll> > >mp;
ll n,k;
inline bool in(ll x,ll y){
return x>=0&&x<=n&&y>=0&&y<=n;
}
inline bool check(ll x1,ll y1,ll x2,ll y2,ll b){
ll DX=max(x1,x2)-min(x1,x2);
ll DY=max(y1,y2)-min(y1,y2);
return DX*DX*DX+DY*DY*DY<=b;
}
int main(){
cin>>n>>k;
ll cnt=0;
for(ll i=1;i<=k;i++){
ll x,y;
ll b;
scanf("%lld%lld%lld",&x,&y,&b);
for(ll dx=0;dx*dx*dx<=b;dx++){
ll maxy=0;
for(ll dy=0;dx*dx*dx+dy*dy*dy<=b;dy++){
maxy=max(maxy,dy);
}
if(x+dx<=n)mp[x+dx].push_back(make_pair(max(y-maxy,(ll)0),min(y+maxy,n)));
if(dx&&x-dx>=0)mp[x-dx].push_back(make_pair(max(y-maxy,(ll)0),min(y+maxy,n)));
}
}
ll ans=(n+1)*(n+1);
for(auto it:mp){
stack<pair<ll,ll> >st;
vector<pair<ll,ll> >v=it.second;
sort(v.begin(),v.end());
for(ll i=0;i<v.size();i++){
if((!st.empty())&&v[i].first<=st.top().second){
v[i].first=st.top().first;
v[i].second=max(v[i].second,st.top().second);
st.pop();
}
st.push(v[i]);
}
while(!st.empty()){
pair<ll,ll>x=st.top();
ans-=x.second-x.first+1;
st.pop();
}
}
cout<<ans<<endl;
}