1 条题解

  • 4
    @ 2022-10-27 20:53:11

    题解

    这道题是一道模拟,直接模拟上楼过程即可。但是这里要有一个优化:0<M100,0<x1060 < M \le 100, 0 < x \le 10^6

    观察一下就能发现,在寻找第 xx 个有楼梯的房间时,一定是绕了这一层不知道几圈了。所以我们自然而然想到了取模。

    我们与处理一下某一层有多少有楼梯的房间,到时候模一下,然后再模拟。

    这样说可能有点不太清楚,我们结合代码片段说一下:

    // 输入部分
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    	for (int j = 0; j < m; j++)
    	{
            // 输入是否有楼梯和x
            scanf("%d%d", &stair[i][j], &x[i][j]);
    		if (stair[i][j]) // 有楼梯
    			cnt[i]++; // 这一层楼梯数++
            // 通过这样来统计每一层的楼梯数量
    	}
    scanf("%d", &s);
    
    // 模拟时取模减小循环和计算答案部分
    ans = (x[i][s] + ans) % 20123; // 计算答案,注意这里不能取模
    int t = x[i][s] % cnt[i]; // 这里t是真实数的次数
    

    大概能理解了吧。不!

    接下整篇代码也就没什么大的思路了,注意一下细节。

    #include <bits/stdc++.h>
    using namespace std;
    int N, M, s, ans = 0; // s: 当前位置
    bool stair[10005][105]; // 房间是否有楼梯
    int cnt[10005], x[10005][105];
    // 房间有楼梯房间数量和房间里的x
    int main()
    {
        scanf("%d%d", &N, &M);
    	for (int i = 1; i <= N; i++)
    		for (int j = 0; j < M; j++)
    		{
                scanf("%d%d", &stair[i][j], &x[i][j]);
    			if (stair[i][j]) // 统计每一层的楼梯房间数量
    				cnt[i]++;
    		}
    	scanf("%d", &s); // 从s进入
    	for (int i = 1; i <= N; i++)
    	{
    		ans = (x[i][s] + ans) % 20123; // 统计答案
            int t = x[i][s] % cnt[i]; // 这里用于节省多余循环
    		if (t == 0) // 特判取模模成0了(其实表示要绕整圈)
    			t = cnt[i];
    		if (stair[i][s]) // 这里特判本房间是否有楼梯,就--
    			t--;
            // 注意这两个特判不能调换,因为如果两个特判都满足了,t = 0,t--变成-1就无法识别,错误了
    		while (t != 0) // 模拟数房间
    		{
    			s++; // 向后走一个
    			if (s == M) s = 0; // 到达最后一个归0
    			if (stair[i][s]) t--; // 有楼梯数量--
    		}
            // 其实这里上楼了,就直接上到i++了,而且s正好也代表现在房间
    	}
    	printf("%d\n", ans);
    	return 0;
    }
    

    ACAC CodeCode

    #include <bits/stdc++.h>
    using namespace std;
    int N, M, s, ans = 0;
    bool stair[10005][105];
    int cnt[10005], x[10005][105];
    int main()
    {
        scanf("%d%d", &N, &M);
    	for (int i = 1; i <= N; i++)
    		for (int j = 0; j < M; j++)
    		{
                scanf("%d%d", &stair[i][j], &x[i][j]);
    			if (stair[i][j])
    				cnt[i]++;
    		}
    	scanf("%d", &s);
    	for (int i = 1; i <= N; i++)
    	{
    		ans = (x[i][s] + ans) % 20123;
            int t = x[i][s] % cnt[i];
    		if (t == 0)
    			t = cnt[i];
    		if (stair[i][s])
    			t--;
    		while (t != 0)
    		{
    			s++;
    			if (s == M) s = 0;
    			if (stair[i][s]) t--;
    		}
    	}
    	printf("%d\n", ans);
    	return 0;
    }
    
    • 1

    [普及~提高][NOIP2012 普及组] 寻宝

    信息

    ID
    1496
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    83
    已通过
    49
    上传者