Submission #537628

# Submission time Handle Problem Language Result Execution time Memory
537628 2022-03-15T10:05:08 Z jamezzz Planine (COCI21_planine) C++17
20 / 110
308 ms 45532 KB
#include <bits/stdc++.h>
using namespace std;

#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define INF 1023456789
typedef long long ll;
typedef long double ld;
typedef pair<ld,ld> ii;

int n,h,x[1000005],y[1000005];

struct line{
	ll x1,x2,y1,y2;
	int cross(line &o){//where does this line intersect
		ll x3=o.x1,x4=o.x2,y3=o.y1,y4=o.y2;
		if((x1-x2)*(y3-y4)==(x3-x4)*(y1-y2))return 2;//parallel
		ll num=(y3-y4)*(y1*(x1-x2)-x1*(y1-y2))-(y1-y2)*(y3*(x3-x4)-x3*(y3-y4));
		ll den=(y3-y4)*(x1-x2)-(y1-y2)*(x3-x4);
		//cout<<(ld)num/den<<"\n";
		if(num==h*den)return 0;//intersect exactly
		if((den>0&&num<h*den)||(den<0&&num>h*den))return -1;//intersect below
		if((den>0&&num>h*den)||(den<0&&num<h*den))return 1;//intersect above
	}
	bool cmp(line &o){
		ll a=x2-x1,b=y2-y1,c=o.x2-o.x1,d=o.y2-o.y1;
		if(x1<o.x1){
			if(b*c>d*a)return true;//intersect below
			if(cross(o)<=0)return false;
			else return true;
		}
		else{
			if(b*c<d*a)return false;//intersect below
			if(cross(o)==-1)return true;
			else return false;
		}
	}
	void print(){
		pf("{%lld %lld %lld %lld} ",x1,y1,x2,y2);
	}
};

struct valley{
	int i;
	line l1,l2;
	void print(){
		pf("%d: ",i);
		l1.print();
		l2.print();
		pf("\n");
	}
};

vector<valley> r;

int main(){
	sf("%d%d",&n,&h);
	for(int i=1;i<=n;++i)sf("%d%d",&x[i],&y[i]);
	for(int i=3;i<n;i+=2){
		line l1={x[i],x[i-1],y[i],y[i-1]};
		line l2={x[i],x[i+1],y[i],y[i+1]};
		r.pb({i,l1,l2});
	}
	sort(all(r),[](valley &a,valley &b){
		return a.l2.cmp(b.l2);
	});
	/*
	for(valley &i:r)i.print();
	for(int i=0;i<3;++i){
		for(int j=0;j<3;++j)pf("%d ",r[i].l2.cross(r[j].l2));
		pf("\n");
	}
	for(int i=0;i<3;++i){
		for(int j=0;j<3;++j)pf("%d ",r[i].l2.cmp(r[j].l2));
		pf("\n");
	}
	*/
	int ans=0;
	line pv=r[0].l1;
	pv.x1-=1;pv.x2-=1;
	for(int i=0;i<sz(r);++i){
		//pf("%d:\n",i);
		//pf("%d\n",pv.cross(r[i].l1));
		if(pv.cross(r[i].l1)>=1){
			//pf("add: %d\n",i);
			++ans;pv=r[i].l2;
		}
	}
	pf("%d\n",ans);
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:62:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |  sf("%d%d",&n,&h);
      |    ^
Main.cpp:63:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |  for(int i=1;i<=n;++i)sf("%d%d",&x[i],&y[i]);
      |                         ^
Main.cpp: In member function 'int line::cross(line&)':
Main.cpp:29:2: warning: control reaches end of non-void function [-Wreturn-type]
   29 |  }
      |  ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1072 KB Output is correct
2 Correct 3 ms 1144 KB Output is correct
3 Correct 3 ms 1200 KB Output is correct
4 Correct 28 ms 6248 KB Output is correct
5 Correct 41 ms 6320 KB Output is correct
6 Correct 36 ms 6320 KB Output is correct
7 Correct 264 ms 45532 KB Output is correct
8 Correct 308 ms 45404 KB Output is correct
9 Correct 285 ms 45384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Incorrect 1 ms 468 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1072 KB Output is correct
2 Correct 3 ms 1144 KB Output is correct
3 Correct 3 ms 1200 KB Output is correct
4 Correct 28 ms 6248 KB Output is correct
5 Correct 41 ms 6320 KB Output is correct
6 Correct 36 ms 6320 KB Output is correct
7 Correct 264 ms 45532 KB Output is correct
8 Correct 308 ms 45404 KB Output is correct
9 Correct 285 ms 45384 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Incorrect 1 ms 468 KB Output isn't correct
12 Halted 0 ms 0 KB -