1002:
Title:
Given n n intervals, we need to go through these intervals one by one. Notice that they are in turn. I didn't see them at first, and I ignored them.
Explanation:
Find the leftmost and rightmost position of each movement, and judge the relationship between the area to be reached and the current left-right position.
Examples:
The first left is 0, the right is 1000000, ans=0, because it can be placed at will in this interval.
Adding the first interval [1,10], we find that this interval intersects the current interval, and the ans does not need to update the operation, at this time only need to update the interval to [1,10].
Add a second interval [20,30], no intersection with the current interval, update the ans, and update the interval to [20,20]
Why update to [20,20]? Because you have completed the minimum steps and the final coordinate of the class in the target interval can only be 20. Of course, if the final interval is [19,30], the new interval you updated is [19,20].
Here's the code.
#include <bits/stdc++.h> using namespace std; #define ll long long const int maxn=1001; struct road{ int l,r; bool operator<(const road& a)const { if(l!=a.l)return l<a.l; return r<a.r; } }r[maxn]; int ans; int n; void dfs(int i,int leftpos,int rightpos){ if(i==n+1)return; if(r[i].l<=leftpos&&r[i].r>=leftpos||r[i].l<=rightpos&&r[i].r>=rightpos||r[i].l>=leftpos&&r[i].l<=rightpos||r[i].r>=leftpos&&r[i].r<=rightpos){ leftpos=max(leftpos,r[i].l); rightpos=min(rightpos,r[i].r); dfs(i+1,leftpos,rightpos); } else{ int lastpos=r[i].l>rightpos?r[i].l:r[i].r; int len=min(abs(r[i].l-rightpos),abs(r[i].r-leftpos)); ans+=len-len/2; if(len%2==0)dfs(i+1,lastpos,lastpos); else{ if(r[i].l!=r[i].r){ if(r[i].l>rightpos)dfs(i+1,lastpos,lastpos+1); else dfs(i+1,lastpos-1,lastpos); } else dfs(i+1,lastpos,lastpos); } } } int main(){ int t; scanf("%d",&t); while(t--){ int a,b; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&r[i].l,&r[i].r); } ans=0; int leftpos=0,rightpos=1000000; dfs(1,leftpos,rightpos); printf("%d\n",ans); } return 0; }
Just make a list of five questions for one rule-finding question.
The law is one formula for every six numbers.
scanf("%lld",&n); ll x=ceil(1.0*n/6); n%=6; if(n==0){ printf("%lld\n",3*x); } else if(n==1){ printf("%lld\n",4*x-3); } else if(n==2){ printf("%lld\n",3*x-2); } else if(n==3){ printf("%lld\n",x-1); } else if(n==4){ printf("%lld\n",6*x-3); } else if(n==5){ printf("%lld\n",x-1); }