HDU 5860 Death Sequence (DP 递推)
2017-09-15 14:02
393 查看
题意:n个人排成一行,从第一个人开始,每隔k个人报数(1, k+1, 2*k+1...),报到数的人被杀死,剩下的人重新排成
一行重复上述操作。q次询问,每次询问第qi个死的人编号。
思路:将编号变为0~n-1,若某轮中某人的编号为i,如果i被k整除则被杀,否则在下轮中编号为i-i/k-1,这是一个子问题。定义一个结构体记录递推一遍就可以得到每个人是第几回合第几个被杀的。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e6+5;
struct node
{
int x, y, pos;
bool operator <(const node &a) const
{
if(x == a.x) return y < a.y;
else return x <a.x;
}
}a[maxn];
int ans[maxn];
int main(void)
{
int n, k, q, _;
cin >> _;
while(_--)
{
scanf("%d%d%d", &n, &k, &q);
int p = 1;
for(int i = 0; i < n; i++)
{
if(i%k == 0)
{
a[i].x = 1;
a[i].y = p++;
a[i].pos = i;
}
else
{
a[i].x = a[i-(i/k+1)].x+1;
a[i].y = a[i-(i/k+1)].y;
a[i].pos = i;
}
}
sort(a, a+n);
while(q--)
{
int x;
scanf("%d", &x);
printf("%d\n", a[x-1].pos+1);
}
}
return 0;
}
一行重复上述操作。q次询问,每次询问第qi个死的人编号。
思路:将编号变为0~n-1,若某轮中某人的编号为i,如果i被k整除则被杀,否则在下轮中编号为i-i/k-1,这是一个子问题。定义一个结构体记录递推一遍就可以得到每个人是第几回合第几个被杀的。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e6+5;
struct node
{
int x, y, pos;
bool operator <(const node &a) const
{
if(x == a.x) return y < a.y;
else return x <a.x;
}
}a[maxn];
int ans[maxn];
int main(void)
{
int n, k, q, _;
cin >> _;
while(_--)
{
scanf("%d%d%d", &n, &k, &q);
int p = 1;
for(int i = 0; i < n; i++)
{
if(i%k == 0)
{
a[i].x = 1;
a[i].y = p++;
a[i].pos = i;
}
else
{
a[i].x = a[i-(i/k+1)].x+1;
a[i].y = a[i-(i/k+1)].y;
a[i].pos = i;
}
}
sort(a, a+n);
while(q--)
{
int x;
scanf("%d", &x);
printf("%d\n", a[x-1].pos+1);
}
}
return 0;
}
相关文章推荐
- HDU 5860 Death Sequence(dp)
- hdu-5860 Death Sequence(递推)
- HDU 5860 Death Sequence(递推)
- HDU 5860 Death Sequence(递推)
- HDU 5860 Death Sequence(递推)
- hdu 5860 Death Sequence(2016 Multi-University Training Contest 10——递推)
- HDU 5860 Death Sequence (递推 + 约瑟夫环 + 思维)——2016 Multi-University Training Contest 10
- HDU 5860 Death Sequence (递推)
- HDU 2050 折线分割平面(动态规划(DP) 递推)
- 【hdu 5945】Fxx and game(递推|dp+单调队列)
- hdu 1723 DP/递推
- HDU 3831 DICS 递推dp
- hdu 2050 折线分割平面(递推,dp)
- hdu 一卡通大冒险 递推 或是 dp
- HDU 5860 Death Sequence(DP+约瑟夫问题·改)
- hdu1978(递推dp)
- HDU 2084 数塔 (DP递推)
- HDU 4734--基础数位dp(递推)
- HDU 5860 Death Sequence
- HDU 6143 Killer Names【dp递推】【好题】【思维题】【阅读题】