首页 > 其他分享 >POJ 2886(线段树+单点修改+约瑟夫环)

POJ 2886(线段树+单点修改+约瑟夫环)

时间:2023-02-03 11:01:56浏览次数:53  
标签:2886 int lo sum num POJ child 单点 include


Description

N children are sitting in a circle to play a game.

The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from the K-th child, who tells all the others the integer on his card and jumps out of the circle. The integer on his card tells the next child to jump out. Let A denote the integer. If A is positive, the next child will be the A-th child to the left. If Ais negative, the next child will be the (−A)-th child to the right.

The game lasts until all children have jumped out of the circle. During the game, the p-th child jumping out will get F(p) candies where F(p) is the number of positive integers that perfectly divide p. Who gets the most candies?

Input

There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 500,000) and K (1 ≤ K ≤ N) on the first line. The next N lines contains the names of the children (consisting of at most 10 letters) and the integers (non-zero with magnitudes within 108) on their cards in increasing order of the children’s numbers, a name and an integer separated by a single space in a line with no leading or trailing spaces.

Output

Output one line for each test case containing the name of the luckiest child and the number of candies he/she gets. If ties occur, always choose the child who jumps out of the circle first.

Sample Input

4 2 Tom 2 Jack 4 Mary -1 Sam 1

Sample Output

Sam 3

n个孩子按顺时针排列,每个人手上都有一张牌,牌上有一个数字,从第k个孩子开始出队,出队的孩子卡上数字是val,则从他开始顺时针第val人是下一个出队的,负数则逆时针数那个第val个人,第P个出队的会得到的糖果数是p的因子个数,输出得到最多糖果的人和他的糖果数,如果有多个,则输出最先出队的人。

  其实本题就是模拟每轮游戏,然后在每一轮游戏中先算出当前需要出队的第j个孩子,然后用线段树找到还在队中的第j个还是得原始编号。

        首先建立一棵线段树,其每个叶节点都是1(代表每个孩子都在圈中,如果第i个孩子不在圈中,那么就令第i个叶子(孩子的编号不是i而是i管理的区间l或r)sum=0)。

分析题中的例子:

4 2

Tom 2

Jack 4

Mary -1

Sam 1

       假设我们第一个出去的孩子是第2个孩子,那么我们把第二个叶节点的值置为0,该步只需要通过update找到sum值正好为2的那个区间的右端点(且该右端点的sum值也为1)即可。然后让这个sum变为0,表示这个孩子出了圈。并且我们读到了JACK的val值为4,当前圈剩余3个人,所以我们向后找4%3=1个人,如果[3,n]内的sum和>=1,那么直接在JACK右边即[3,n]区间找即可。如果sum的和<1,那么就在JACK左边[1,1]内找1-sum的那个位置的1.

       依次类推即可。线段树的每个叶节点维护name,val,sum三个值,非叶节点只需要维护sum值即可。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<set>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
#define MAX_N 500100
int s[MAX_N];
int bit[MAX_N];
int id[MAX_N];
char name[MAX_N][12];
int N,K;

void init()
{
int i,j,k,m;
for(i=0; i<MAX_N; i++)
s[i]=1;
for(i=2; i<=MAX_N; i++)
{
if(s[i]==1)
{
for(j=i; j<=MAX_N; j+=i)
{
k=0;
for(m=j; m%i==0; m/=i,k++);
s[j]*=k+1;
}
}
}
}

void add(int i,int x)
{
while(i<=N)
{
bit[i]+=x;
i+=i&-i;
}
}
int query(int i)
{
int sum=0;
while(i>0)
{
sum+=bit[i];
i-=i&-i;
}
return sum;
}

int find(int x)
{
int l,r,m;
l=1;
r=N;
while(l<r)
{
m=(l+r)/2;
if(query(m)<x)
l=m+1;
else
r=m;
}
return l;
}

int main(void)
{
int i,num,max,pos,p,lo;
init();
while(~scanf("%d%d",&N,&K))
{
num=N;
for(i=1; i<=N; i++)
scanf("%s%d",name[i],&id[i]);
for(i=1; i<=N; i++)
add(i,1);
max=-1;
for(i=2; i<=N; i++)
if(max<s[i])
{
max=s[i];
pos=i;
}
lo=K;

while(--pos)
{
add(lo,-1);
num--;
if(id[lo]>0)
K=((K-2+id[lo])%num+num)%num+1;
else
K=((K-1+id[lo])%num+num)%num+1;
lo=find(K);
}
printf("%s %d\n",name[lo],max);
}
return 0;
}

 

标签:2886,int,lo,sum,num,POJ,child,单点,include
From: https://blog.51cto.com/u_15952369/6035590

相关文章

  • POJ 1456 Supermarket (贪心+并查集优化)
    DescriptionAsupermarkethasasetProdofproductsonsale.Itearnsaprofitpxforeachproductx∈Prodsoldbyadeadlinedxthatismeasuredasanintegral......
  • POJ 3468(树状数组+区间修改)
    题目描述给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一:1、“Clrd”,表示把A[l],A[l+1],…,A[r]都加上d。2、“Qlr”,表示询问数列中第l~r个数的和......
  • SPOJ375--Query on a tree(树链剖分)
    Description:Youaregivenatree(anacyclicundirectedconnectedgraph)with N nodes,andedgesnumbered1,2,3...N-1.Wewillaskyoutoperfromsomeins......
  • POJ3468 A Simple Problem with Integers(SplayTree做法)
    DescriptionYouhave N integers, A1, A2,..., AN.Youneedtodealwithtwokindsofoperations.Onetypeofoperationistoaddsomegivennumbertoea......
  • POJ2761 Feed the dogs(Treap)
    DescriptionWindlovesprettydogsverymuch,andshehasnpetdogs.SoJiajiahastofeedthedogseverydayforWind.JiajialovesWind,butnotthedogs,......
  • Java里什么是POJO
    POJO(PlainOrdinaryJavaObject)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。POJO和JavaBean是我们常见的两个关键字,一般容易混淆,POJO全称是Pl......
  • 单点登录(SSO)的设计与实现
    本文转载自 https://ken.io/note/sso-design-implement 一、前言1、SSO说明SSO英文全称SingleSignOn,单点登录。SSO是在多个应用系统中,用户只需要登录一次就可以访......
  • POJ-1328-Radar Installation
    RadarInstallationTimeLimit:2000/1000ms(Java/Other)   MemoryLimit:20000/10000K(Java/Other)TotalSubmission(s):47   AcceptedSubmission(s):25......
  • POJ-2406-Power Strings
    PowerStringsTimeLimit:6000/3000ms(Java/Other)   MemoryLimit:131072/65536K(Java/Other)TotalSubmission(s):96   AcceptedSubmission(s):34Probl......
  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......