DP->UVALive 4764

本文介绍了一款名为Bingit的游戏,该游戏包含N轮,每轮会开启一种编号从1到100000的新卡片。玩家可以通过bing特定的卡片来获取分数,若连续两次出现相同的卡片则可获得1分,特殊卡片999连续出现可得3分。文章提供了求解最大得分的算法实现,并附带示例输入输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bing it
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

I guess most of you played cards on the trip to Harbin, but I'm sure you have never played the following card game. This card game has Nrounds and 100000 types of cards numbered from 1 to 100000. A new card will be opened when each round begins. You can ``bing" this new card. And if the card you last ``bing" is the same with this new one, you will get 1 point. You can "bing" only one card, but you can change it into a new one. For example, the order of the 4 cards is 1 3 4 3. You can ``bing" 1 in the first round and change it into 3 in the second round. You get no point in the third round, but get 1 point in the last round. Additionally, there is a special card 999. If you ``bing" it and it is opened again, you will get 3 point.

Given the order of N cards, tell me the maximum points you can get.

Input

The input file will contain multiple test cases. Each test case will consist of two lines. The first line of each test case contains one integerN(2$ \le$N$ \le$100000). The second line of each test case contains a sequence of n integers, indicating the order of cards. A single line with the number ``0" marks the end of input; do not process this case.

Output

For each input test case, print the maximum points you can get.

Sample Input

2 
1 1 
5 
1 999 3 3 999 
0

Sample Output

1 
3
 
   
/*This Code is Submitted by OUC_Fighting for Problem 2883 at 2013-10-06 11:20:28*/
#include <iostream>
#include <string.h>

using namespace std;

#define MAXN 100000 + 10

int temp[MAXN], dp[MAXN];
bool vis[MAXN];

void input()
{
    int n;

    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> temp[i];
        }

        dp[temp[0]] = 0;

        int per = temp[0], mx = -1;

        memset(dp, 0, sizeof(dp));
        memset(vis, false, sizeof(vis));
        vis[per] = true;

        for (int i = 1; i < n; i++)
        {
            int next = temp[i];

            if (!vis[next])
            {
                dp[next] = dp[per];
                vis[next] = true;
            }
            else
            {
                if (next == 999)
                {
                    dp[next] = max(dp[per], dp[next] + 3);
                }
                else
                {
                    dp[next] = max(dp[per], dp[next] + 1);
                }
            }

            per = next;

            if (dp[next] > mx)
            {
                mx = dp[next];
            }
        }

        cout << mx << endl;
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    input();
    return 0;
}


shu3.xml:<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background" tools:context=".ShuHai3Activity"> <!-- Toolbar --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 播放视频区域 --> <!-- 时间线和缩略图区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.55" android:background="@android:color/black" app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.6" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar" app:layout_constraintVertical_bias="0.0"> <!-- 模拟萤石播放加载动画 --> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <!-- 视频播放View,可以替换成具体的视频播放控件 --> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintHeight_percent="0.45" android:fillViewport="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 多个缩略图子项 --> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> <!-- <ImageView--> <!-- android:layout_width="0dp"--> <!-- android:layout_height="wrap_content"--> <!-- app:layout_columnWeight="1"--> <!-- android:layout_margin="4dp"--> <!-- android:adjustViewBounds="true"--> <!-- android:contentDescription="萤石缩略图"--> <!-- android:src="@drawable/default_thumbnail" />--> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> 修改下面shuhaisp.xml布局和上面shu3.xml布局一样,保持shuhaisp.xml依赖不变必须使用android支持库。shuhaisp.xml:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- tools:context=".ShuhaiSp"--> <!-- <!– 顶部工具栏 –>--> <RelativeLayout android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#44bd32"> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_below="@id/toolbar" android:layout_above="@+id/thumbnail_scrollview" android:layout_marginTop="0dp" android:background="@android:color/black"> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentBottom="true" android:fillViewport="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" android:rowCount="0" android:alignmentMode="alignBounds" android:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <ImageView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> --> </android.widget.GridLayout> </LinearLayout> </ScrollView> </RelativeLayout>
最新发布
06-25
<think>我们有两个布局文件:shu3.xml和shuhaisp.xml。目标是将shuhaisp.xml修改成和shu3.xml一样的布局,但必须使用Android支持库(即不能使用AndroidX)。同时,保持shuhaisp.xml中已有的依赖不变。分析shu3.xml布局特点:1.根布局是ConstraintLayout。2.包含一个Toolbar(RelativeLayout),内部有一个返回按钮。3.视频播放区域(FrameLayout)位于Toolbar下方,并且上方约束在Toolbar底部,下方约束在缩略图区域顶部。同时,其高度是父布局高度的55%(通过app:layout_constraintHeight_percent="0.55"实现)。4.缩略图区域(ScrollView)位于视频播放区域下方,高度为父布局的45%,并且底部约束在父布局底部。5.缩略图区域内部是一个GridLayout(来自android.support.v7.widget.GridLayout),设置为2列。而shuhaisp.xml当前布局:1.根布局是RelativeLayout。2.同样有Toolbar(RelativeLayout)、视频播放区域(FrameLayout)和缩略图区域(ScrollView)。3.视频播放区域和缩略图区域使用RelativeLayout的相对定位(layout_below和layout_above)来放置。要求修改shuhaisp.xml,使用ConstraintLayout并实现与shu3.xml相同的布局结构。修改步骤:1.将根布局改为android.support.constraint.ConstraintLayout,并添加必要的命名空间。2.调整子视图的约束条件,使用ConstraintLayout的约束属性。3.视频播放区域的高度设置为父布局的55%,缩略图区域高度设置为45%。4.将GridLayout改为支持库版本(android.support.v7.widget.GridLayout)。注意:由于原shuhaisp.xml中使用了android.widget.GridLayout,现在需要替换为android.support.v7.widget.GridLayout。同时,保留原布局中的其他属性(如背景、id等)。修改后的布局代码应该如下:</think>要将 `shuhaisp.xml` 布局修改为与 `shu3.xml` 相同的结构,同时保持 Android 支持库依赖不变,我们需要做以下关键修改: ### 主要修改点: 1. **根布局**:将 `RelativeLayout` 改为 `android.support.constraint.ConstraintLayout` 2. **约束设置**:使用百分比约束控制视频区域和缩略图区域的高度比例 3. **GridLayout**:替换为支持库版本的 `android.support.v7.widget.GridLayout` 4. **布局参数**:添加约束布局的定位属性和百分比高度控制 ### 修改后的完整代码: ```xml <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_background"> <!-- 修改点1:根布局改为ConstraintLayout --> <!-- 顶部工具栏 --> <RelativeLayout android:id="@+id/toolbar" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <!-- 添加约束定位 --> <Button android:id="@+id/back_button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginBottom="8dp" android:background="@android:color/transparent" android:contentDescription="返回上一页" android:foreground="?android:attr/selectableItemBackground" android:text="返回" android:textColor="@android:color/white" android:textSize="19dp" /> </RelativeLayout> <!-- 视频播放区域 --> <FrameLayout android:id="@+id/video_container" android:layout_width="0dp" android:layout_height="0dp" android:background="@android:color/black" app:layout_constraintHeight_percent="0.55" <!-- 关键:高度占比55% --> app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/toolbar"> <ProgressBar android:id="@+id/video_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="视频播放区域" android:textColor="@android:color/white" android:visibility="visible" /> </FrameLayout> <!-- 缩略图区域 --> <ScrollView android:id="@+id/thumbnail_scrollview" android:layout_width="0dp" android:layout_height="0dp" android:fillViewport="true" app:layout_constraintHeight_percent="0.45" <!-- 关键:高度占比45% --> app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/video_container"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 修改点:使用支持库的GridLayout --> <android.support.v7.widget.GridLayout android:id="@+id/gridLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:rowCount="0" app:alignmentMode="alignBounds" app:useDefaultMargins="true" android:padding="8dp"> <!-- 缩略图示例 --> <!-- <ImageView android:layout_width="0dp" android:layout_height="wrap_content" app:layout_columnWeight="1" android:layout_margin="4dp" android:adjustViewBounds="true" android:contentDescription="缩略图" android:src="@drawable/default_thumbnail" /> --> </android.support.v7.widget.GridLayout> </LinearLayout> </ScrollView> </android.support.constraint.ConstraintLayout> ``` ### 关键修改说明: 1. **根布局转换**: ```xml <!-- 修改前 --> <RelativeLayout ...> ... </RelativeLayout> <!-- 修改后 --> <android.support.constraint.ConstraintLayout ...> ... </ConstraintLayout> ``` 2. **百分比高度控制**: ```xml <!-- 视频区域占55% --> app:layout_constraintHeight_percent="0.55" <!-- 缩略图区域占45% --> app:layout_constraintHeight_percent="0.45" ``` 3. **约束关系设置**: ```xml <!-- 视频区域约束 --> app:layout_constraintBottom_toTopOf="@id/thumbnail_scrollview" app:layout_constraintTop_toBottomOf="@id/toolbar" <!-- 缩略图区域约束 --> app:layout_constraintTop_toBottomOf="@id/video_container" app:layout_constraintBottom_toBottomOf="parent" ``` 4. **GridLayout替换**: ```xml <!-- 修改前 --> <android.widget.GridLayout ...> ... </android.widget.GridLayout> <!-- 修改后 --> <android.support.v7.widget.GridLayout ...> ... </android.support.v7.widget.GridLayout> ``` ### 布局效果说明: 1. **层级结构**: ``` ConstraintLayout (根布局) ├── RelativeLayout (工具栏) ├── FrameLayout (视频容器,占55%高度) └── ScrollView (缩略图区域,占45%高度) └── GridLayout (支持库版本) ``` 2. **比例控制**: - 视频区域:屏幕高度的55% - 缩略图区域:屏幕高度的45% - 使用`layout_constraintHeight_percent`精确控制比例 3. **兼容性**: - 完全使用Android支持库组件 - 兼容旧版Android设备 ### 相关问题: 1. 如何在ConstraintLayout中实现百分比高度布局? 2. Android支持库的GridLayout与标准GridLayout有何区别? 3. 为什么在视频播放区域使用FrameLayout作为容器? 4. 如何优化缩略图GridLayout的加载性能?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值