0
点赞
收藏
分享

微信扫一扫

HDU——1029 Ignatius and the Princess IV(STL/动态规划)

原题链接: ​​http://acm.hdu.edu.cn/showproblem.php?pid=1029​​

HDU——1029 Ignatius and the Princess IV(STL/动态规划)_#define
测试样例

Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
Sample Output
3
5
1

题意: 给你一个奇数HDU——1029 Ignatius and the Princess IV(STL/动态规划)_ios_02长度大小的整数序列,你需要找出其中一个元素,它至少出现了HDU——1029 Ignatius and the Princess IV(STL/动态规划)_c代码_03次。

解题思路: 这里我们有两种方法去解决。先说通俗的解法,即是用map容器来解决,我们对每次输入进行记录,并判断它符合要求。对符合要求的值进行存储, 最后输出即可。很好理解。那么还有一种方法就是动态规划。这种方法十分巧妙,我们首先要确定一个事实,就是这个整数序列只能出现一个符合要求的元素。因为它至少出现HDU——1029 Ignatius and the Princess IV(STL/动态规划)_c代码_03次。所以我们则可以设这个元素就为HDU——1029 Ignatius and the Princess IV(STL/动态规划)_#define_05,那么它出现的次数就为HDU——1029 Ignatius and the Princess IV(STL/动态规划)_#define_06。再想一想,在前HDU——1029 Ignatius and the Princess IV(STL/动态规划)_c代码_07个数中,如果没有一个数出现次数超过HDU——1029 Ignatius and the Princess IV(STL/动态规划)_#define_08,那么这个数必定在剩下的HDU——1029 Ignatius and the Princess IV(STL/动态规划)_c代码_09个数中出现。所以,我们可以判断当输入数值不等于我们当时所假设这个元素时,那么这个出现次数就减,若为,则加。这样的目的是为了判断我们在剔除过程中到底有没有一个元素可以出现超过一半。所以我们在HDU——1029 Ignatius and the Princess IV(STL/动态规划)_#define_10时我们需要更换我们的假定值,因为它已经不可能了。OK,具体看代码。

STL AC代码

/*
*
*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n,temp;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>n){
int result;
map<int,int> p;
rep(i,0,n-1){
cin>>temp;
if(++p[temp]>=(n+1)/2){
result=temp;
}
}
cout<<result<<endl;
}
return 0;
}

动态规划AC代码

/*
*
*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n,temp,result,cnt;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>n){
cnt=0;
rep(i,1,n){
cin>>temp;
if(cnt==0){
result=temp;
cnt++;
}
else{
if(temp==result){
cnt++;
}
else{
cnt--;
}
}
}
cout<<result<<endl;
}
return 0;
}


举报

相关推荐

0 条评论