解题报告:HDU_6136:Death Podracing (优先队列+循环链表)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_32570675/article/details/77364296

题目链接

题意:

n个人以不同的速度在环上顺时针或逆时针移动,每次相遇,移除下标小的,问最后只剩下一个人的时间的分数形式


官方题解及思路:


也不是第一次写循环链表的题了,还是写了好久。。

注意维护循环链表时要同时更新左右指针

代码:

#include<bits/stdc++.h>

const int N = 1e5+10;
using namespace std;


struct node{
   double val;
   int d,v,id,w,l;
   node(){val=0;}
   node(double a,int b,int c,int i=0,int _w=0,int _l=0){
      val = a;d = b;v = c;id = i;w = _w;l = _l;
   }bool operator <(const node& a)const {
      return val > a.val;
   }
}A[N];

bool cmp(node a,node b){
   return a.d<b.d;
}

int n,l;
int R[N],L[N];
bool used[N];
priority_queue< node >Q;


node oper(int x,int y){
   if(x==y)return node(0,0,0,0,x,y);
   int d , v , w=A[x].id>A[y].id?x:y;
   if(A[x].v>0){
      if(A[y].v<=0){d = A[y].d - A[x].d;v = A[x].v - A[y].v;}
      else {
         if(A[y].v<A[x].v){d = A[y].d - A[x].d;v = A[x].v - A[y].v;}
         else {d = l + A[x].d - A[y].d;v = A[y].v - A[x].v;}
      }
   }else {//A[x].v<=0
      if(A[y].v>0){d = l + A[x].d - A[y].d;v = A[y].v - A[x].v;}
      else {//A[y].v<=0
         if(A[y].v>A[x].v){d = l + A[x].d - A[y].d;v = A[y].v - A[x].v;}
         else {d = A[y].d - A[x].d;v = A[x].v - A[y].v;}
      }
   }
   return node(1.0*d/v,d,v,0,w,x^y^w);
}

int main()
{
   //freopen("1004.in","r",stdin);
   //freopen("my_1004.out","w",stdout);
   int T;
   scanf("%d",&T);
   while(T--){
      while(!Q.empty())Q.pop();
      scanf("%d%d",&n,&l);
      memset(used,0,sizeof(used));
      for(int i=0;i<n;i++)scanf("%d",&A[i].d),L[i]=i-1,R[i]=i+1,A[i].id=i;
      for(int i=0;i<n;i++)scanf("%d",&A[i].v);R[n-1]=0,L[0]=n-1;
      sort(A,A+n,cmp);
      for(int i=0,j=1;i<n;i++){
         if(j)Q.push(oper(i,j));
         else Q.push(oper(j,i));
         if(++j==n)j=0;
      }node ans ;
      while(!Q.empty()){
         node tmp = Q.top();Q.pop();
         if(tmp.w==tmp.l)break;
         if(used[tmp.w]||used[tmp.l])continue;
         if(tmp.val>ans.val)ans = tmp;
         used[tmp.l]=true;
         int y,x=tmp.w;
         if(L[tmp.w]==tmp.l){
            y = L[x];
            while(used[y])y=L[y];
            L[x] = y;R[y] = x;
         }else if(R[tmp.w]==tmp.l){
            y = R[x];
            while(used[y])y=R[y];
            R[x] = y;L[y] = x;
         }if(x>y)swap(x,y);
         Q.push(oper(x,y));
      }int gcd = __gcd(ans.d,ans.v);
      printf("%d/%d\n",ans.d/gcd,ans.v/gcd);
   }return 0;
}


Death to Binary?

05-04

The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they use Fibonacci base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits (fits?) in the representation are not powers of two, but the elements of the Fibonacci progression (1, 2, 3, 5, 8,... - the progression is defined by F0 = 1, F1 = 2 and the recursive relation Fn = Fn-1 + Fn-2 for n >= 2).nnFor example 1101001Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40.nnYou may observe that every integer can be expressed in this base, but not necessarily in a unique way - for example 40 can be also expressed as 10001001Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001Fib is a canonical Fibonacci representation of 40.nnTo prove that this representation of numbers is superior to the others, ACM have decided to create a computer that will compute in Fibonacci base. Your task is to create a program that takes two numbers in Fibonacci base (not necessarily in the canonical representation) and adds them together.nnInputnnThe input consists of several instances, each of them consisting of a single line. Each line of the input contains two numbers X and Y in Fibonacci base separated by a single space. Each of the numbers has at most 40 digits. The end of input is not marked in any special way.nnOutputnnThe output for each instance should be formated as follows:nnThe first line contains the number X in the canonical representation, possibly padded from left by spaces. The second line starts with a plus sign followed by the number Y in the canonical representation, possibly padded from left by spaces. The third line starts by two spaces followed by a string of minus signs of the same length as the result of the addition. The fourth line starts by two spaces immediately followed by the canonical representation of X + Y. Both X and Y are padded from left by spaces so that the least significant digits of X, Y and X + Y are in the same column of the output. The output for each instance is followed by an empty line.nnSample Inputnn11101 1101n1 1nSample Outputnn 100101n+ 10001n -------n 1001000nn 1n+ 1n --n 10 问答

没有更多推荐了,返回首页