[HDU]week4

1263.水果

#include<iostream>
#include<string>
#include<map>
using namespace std;
 //map<string,map<string,int>>这种比较新奇,解决了三个变量的键值对应
struct MyStruct
{
    
    
	map <string, int>MyStructma;  //存放水果名以及该种水果的数量
};
int main()
{
    
    
	map <string, MyStruct>ma;    //地名   
	map <string, MyStruct>::iterator it;
	map <string, int>::iterator MyStructmait;
	string fruit,place;
	int count;
    int n,t;
	cin>>t;
	while(t--)
	{
    
    
		ma.clear();
		cin>>n;
		while(n--)
		{
    
    
			cin>>fruit>>place>>count;
			ma[place].MyStructma[fruit] += count;
		}
		for (it = ma.begin(); it != ma.end(); it++)
		{
    
    
			cout<<it->first<<endl;
			for (MyStructmait = it->second.MyStructma.begin(); MyStructmait != it->second.MyStructma.end(); MyStructmait++)
			{
    
    
				
				cout<<"   |----"<<MyStructmait->first<<"("<<MyStructmait->second<<")"<<endl;
			}
		}
		if(t != 0)cout<<endl;
	}
	return 0;
}

双重map:参考
错误:

#include <iostream>
#include <string>
#include <map>
using namespace std;

int main()
{
    
    
    int n,m,i;
    cin>>n;
    while(n--){
    
    
        cin>>m;
        string fruit,city;
        int num;
        map<string, int> str1;//fruit,num
        map<string,string> str2;//fruit,city
        set<string> cstr;
        for(i=0;i<m;i++){
    
    
            cin>>fruit>>city>>num;
            str1[fruit]+=num;
            cstr.insert(city);
            str2[fruit]=city;
        }
        for (auto iter = cstr.begin(); iter != cstr.end(); ++iter) {
    
    
            cout <<*iter<<endl;
            for (auto it = str2.begin(); it != str2.end(); ++it){
    
    
                if(it->second == *iter){
    
    
                    cout <<"   |----" <<it->first<<"("<<str1[it->first]<<")"<<endl;
                }
            }
        }
        if(n!=0)    cout<<endl;
    }
    return 0;
}

1075.What Are You Talking About

#include<string>
#include<iterator>
#include<map>
#include<iostream>
using namespace std;
int main()
{
    
    
	string a,b;
	map<string,string> val;
	getline(cin, a);
	while(cin>>a){
    
    
	    if(a=="END") break;
	    else {
    
    
	        cin>>b;
	        val[b]=a;
	    }
	}
	getline(cin, a);
	getline(cin, a);
	string ss="";
	while(1)
	{
    
    
	    getline(cin, a);//a=mar
	    if(a=="END") break;
	    else
	        ss=ss+a+"\n";
	}
	string temp="";
	for(int i=0;i<ss.length();i++){
    
    //mar的长度
	    if('a'<=ss[i]&&ss[i]<='z'){
    
    
	        temp+=ss[i];//可以翻译的mar存在temp中
	    }else{
    
    
	        map<string,string>::iterator it=val.find(temp);
	        if(it!=val.end())
	            cout<<it->second;
	        else 
	        	cout<<temp;
	    temp="";
	    cout<<ss[i];
	    }
	}
}
  • getline
    参考
  • iterator,迭代器遍历
    迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型。参考
  • map.find()
    参考

1006.Let the Balloon Rise

#include<bits/stdc++.h>
using namespace std;
map<string, int> count1;
int main()
{
    
    
	int n;
	int max = 0;
	string pop;
	while (cin>>n&&n) {
    
    
		while (n--) {
    
    
			string color;
			cin >> color;
			count1[color]++;
			if (count1[color] > max) {
    
    
				max = count1[color];
				pop = color;
			}
		}
		cout << pop << endl;
		max = 0;
	}
}

set

set容器中只能存储键,是单纯的键的集合,其中键是不能重复的。
set支持大部分的map的操作,但是set不支持下标的操作,而且没有定义mapped_type类型。
按照个数升序排列。

#include <cstdio>
#include <set>

using namespace std;

int main()
{
    
    
    int n,i;
    while(scanf("%d", &n)==1 && n!=0){
    
    
      string s;
      set<string> str;
      for(i=0;i<n;i++){
    
    
        cin >> s;
        str.insert(s);
      }
      cout << *str.rbegin()<<endl;
    }
    return 0;
}

1029.Ignatius and the Princess IV

#include<stdio.h>
#include<vector>
#include<algorithm>
#define N 1010000
using namespace std;
int p[N],f[N];
int main(void)
{
    
    
	int n,i;
	vector<int>a;//定义数组 
	while(scanf("%d",&n)==1){
    
    
		for(i=1;i<=n;i++){
    
    
			scanf("%d",&p[i]);
			a.push_back(p[i]);//尾部插入数组
		}
		for(i=1;i<=n;i++){
    
    
			//count函数用来统计字符串中某个字符的个数
			f[i]=count(a.begin(),a.end(),p[i]);//起始地址,结束地址,需要查找的字符
			if(f[i]>=(n+1)/2){
    
    
				printf("%d\n",p[i]);
				break;
			}
		 } 
		 a.clear();
	}
	return 0;
}

#include<bits/stdc++.h>

using namespace std;

int i,n,cnt,anss,x;

int main()
{
    
    
    while(scanf("%d",&n)!=EOF)
    {
    
    
        cnt=0;
        for(i=1;i<=n;i++)
        {
    
    
            cin>>x;
            if(cnt==0)
            {
    
    
                cnt++;
                anss=x;
            }
            else
            {
    
    
                if(x==anss)
                cnt++;
                else
                cnt--;
            }        
        }
        cout<<anss<<endl;
    }
}

1412.{A} + {B}

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
int main()
{
    
    
	int n,m;
	while(cin>>n>>m){
    
    
		set<int> q;
		int t,i;
		for(i=0;i<n;i++){
    
    
			scanf("%d",&t);
			q.insert(t);
		}
		for(i=0;i<m;i++){
    
    
			scanf("%d",&t);
			q.insert(t);
		}
		/*set<int>::iterator it;
		for (it = q.begin(); it != q.end(); it++)
    		cout << *it <<" ";
		*/
		//输出set,格外注意输出格式
		int f=0;
		for(auto i:q){
    
    
			if(f==0)
				printf("%d",i);
			else
				printf(" %d",i);
			f=1;
		}
		cout<<"\n"; 
	}
	return 0;
}

4286.Data Handler

方法1

参考

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int maxn = 500000+10;
#define REP(_,a,b) for(int _ = (a); _ <= (b); _++)
int n,m,dir;
deque<int> dqi,lft,rgt;
int Lp,Rp;
int num[maxn];
void init() {
    
    
    dqi.clear();
    lft.clear();
    rgt.clear();
    dir = 1;
}
void input() {
    
    
    scanf("%d",&n);
    REP(i,1,n) scanf("%d",&num[i]);
    scanf("%d%d",&Lp,&Rp);
    REP(i,1,Lp-1 ) lft.push_back(num[i]);
    REP(i,Lp,Rp) dqi.push_back(num[i]);
    REP(i,Rp+1,n ) rgt.push_back(num[i]);
    scanf("%d",&m);
}
void Reverse() {
    
    
    dir = 1-dir;
}
void Insert() {
    
    
    char op[20];
    int x;
    scanf("%s%d",op,&x);
    if( (op[0]=='R' && dir) || (op[0] =='L' && !dir) ) {
    
    
        dqi.push_back(x);
    } else {
    
    
        dqi.push_front(x);
    }
}
void Delete() {
    
    
    char op[20];
    scanf("%s",op);
    if( (op[0]=='R' && dir) || (op[0] =='L' && !dir) ) {
    
    
        dqi.pop_back();
    } else {
    
    
        dqi.pop_front();
    }
}
void MoveLeft() {
    
    
    char op[20];
    scanf("%s",op);
     if( op[0]=='L' && dir ){
    
    
        int x = lft.back();
        lft.pop_back();
        dqi.push_front(x);
    }else if( op[0]=='R' && !dir ){
    
    
        int x = dqi.front();
        dqi.pop_front();
        rgt.push_front(x);
    }
    else if(op[0] == 'R' && dir){
    
    
        int x = dqi.back();
        dqi.pop_back();
        rgt.push_front(x);
    } else {
    
    
        int x = lft.back();
        lft.pop_back();
        dqi.push_back(x);
    }
}
void MoveRight() {
    
    
    char op[20];
    scanf("%s",op);
    if( (op[0]=='L' && dir)) {
    
    
        int x = dqi.front();
        dqi.pop_front();
        lft.push_back(x);
    }
    else if(op[0]=='R' && dir) {
    
    
        int x = rgt.front();
        rgt.pop_front();
        dqi.push_back(x);
 
    }
    else if(op[0]=='L' && !dir){
    
    
        int x = dqi.back();
        dqi.pop_back();
        lft.push_back(x);
    }else{
    
    
        int x = rgt.front();
        rgt.pop_front();
        dqi.push_front(x);
    }
}
void output() {
    
    
    bool flag = false;
    for(deque<int>::iterator it = lft.begin(); it != lft.end(); it++) {
    
    
        if(!flag) {
    
    
            printf("%d",*it);
            flag = true;
        } else {
    
    
            printf(" %d",*it);
        }
    }
 
    if(!dir) reverse(dqi.begin(),dqi.end());
    for(deque<int>::iterator it = dqi.begin(); it != dqi.end(); it++) {
    
    
        if(!flag) {
    
    
            printf("%d",*it);
            flag = true;
        } else {
    
    
            printf(" %d",*it);
        }
    }
    for(deque<int>::iterator it = rgt.begin(); it != rgt.end(); it++) {
    
    
        if(!flag) {
    
    
            printf("%d",*it);
            flag = true;
        } else {
    
    
            printf(" %d",*it);
        }
    }
    puts("");
}
void solve() {
    
    
    char cmd[20];
    while(m--) {
    
    
        scanf("%s",cmd);
        if(cmd[0]=='R') Reverse();
        else if(cmd[0]=='I') Insert();
        else if(cmd[0] == 'D') Delete();
        else if(cmd[0]=='M' && cmd[4]=='R') MoveRight();
        else MoveLeft();
    }
    output();
}
int main(){
    
    
 
    int ncase;
    cin >> ncase;
    while(ncase--) {
    
    
        init();
        input();
        solve();
    }
    return 0;
}

方法2

参考

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#define PI acos(-1.0)
#define M 1000005  //10^6
#define eps 1e-8
#define LL long long
#define moo 1000000007
#define INF -999999999
#define LL long long
using namespace std;
struct aaa
{
    
    
    int val;
    int le;
    int ri; 
}aa[M];     //双向链表
int stle[M];//左栈
int stri[M];//右栈
int b[M];   //初始读入数组
char a[10];
char d[10];
int judge()
{
    
    
    if(strcmp(a,"MoveLeft")==0)
        return 1;
    if(strcmp(a,"MoveRight")==0)
        return 2;
    if(strcmp(a,"Insert")==0)
        return 3;
    if(strcmp(a,"Delete")==0)
        return 4;
    if(strcmp(a,"Reverse")==0)
        return 5;
}
int main()
{
    
    
    int m,n,T;
    while(scanf("%d",&T)!=EOF)
    {
    
    
        while(T--)
        {
    
    
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
    
    
                scanf("%d",&b[i]);
            }
            int lee,rii;
            scanf("%d%d",&lee,&rii);
            int hele=0,heri=0;
            for(int i=1;i<lee;i++)
            {
    
    
                hele++;
                stle[hele]=b[i];
            }
            for(int i=n;i>rii;i--)
            {
    
    
                heri++;
                stri[heri]=b[i];
            }
            int now=rii-lee;
            for(int i=lee;i<=rii;i++)
            {
    
    
                aa[i-lee].le=i-lee-1;
                aa[i-lee].ri=(i==rii?-1:i-lee+1);
                aa[i-lee].val=b[i];
            }
            rii=rii-lee;
            lee=0;
            
            //以上是把三部分分别储存好的操作。
            
            scanf("%d",&m);
            int flag=0;
            while(m--)
            {
    
    
                scanf("%s",a);
                int tt=judge();
                if(tt==5)
                {
    
    
                    flag++;
                    continue;
                }
                if(tt==1)
                {
    
    
                    scanf("%s",d);
                    if(strcmp(d,"L")==0)
                    {
    
    
                        aa[++now].val=stle[hele--];
                        if(flag%2==0)
                        {
    
    
                            aa[now].le=-1;
                            aa[lee].le=now;
                            aa[now].ri=lee;
                            lee=now;
                        }
                        else
                        {
    
    
                            aa[now].ri=-1;
                            aa[rii].ri=now;
                            aa[now].le=rii;
                            rii=now;
                        }
                    }
                    else
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            stri[++heri]=aa[rii].val;
                            aa[aa[rii].le].ri=-1;
                            rii=aa[rii].le;
                        }
                        else
                        {
    
    
                            stri[++heri]=aa[lee].val;
                            aa[aa[lee].ri].le=-1;
                            lee=aa[lee].ri;
                        }
                    }
                    continue;
                }
                if(tt==2)
                {
    
    
                    scanf("%s",d);
                    if(strcmp(d,"R")==0)
                    {
    
    
                        aa[++now].val=stri[heri--];
                        if(flag%2==0)
                        {
    
    
                            aa[now].ri=-1;
                            aa[rii].ri=now;
                            aa[now].le=rii;
                            rii=now;
                        }
                        else
                        {
    
    
                            aa[now].le=-1;
                            aa[lee].le=now;
                            aa[now].ri=lee;
                            lee=now;
                        }
                    }
                    else
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            stle[++hele]=aa[lee].val;
                            aa[aa[lee].ri].le=-1;
                            lee=aa[lee].ri;
                        }
                        else
                        {
    
    
                            stle[++hele]=aa[rii].val;
                            aa[aa[rii].le].ri=-1;
                            rii=aa[rii].le;
                        }
                    }
                    continue;
                }
 
                if(tt==3)
                {
    
    
                    scanf("%s",d);
                    int kkk;
                    scanf("%d",&kkk);
                    now++;
                    aa[now].val=kkk;
                    if(strcmp(d,"L")==0)
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            aa[now].ri=lee;
                            aa[now].le=-1;
                            aa[lee].le=now;
                            lee=now;
                        }
                        else
                        {
    
    
                            aa[now].le=rii;
                            aa[now].ri=-1;
                            aa[rii].ri=now;
                            rii=now;
                        }
                    }
                    else
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            aa[now].le=rii;
                            aa[now].ri=-1;
                            aa[rii].ri=now;
                            rii=now;
                        }
                        else
                        {
    
    
                            aa[now].ri=lee;
                            aa[now].le=-1;
                            aa[lee].le=now;
                            lee=now;
                        }
                    }
                    continue;
                }
                if(tt==4)
                {
    
    
                    scanf("%s",d);
                    if(strcmp(d,"L")==0)
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            lee=aa[lee].ri;
                            aa[lee].le=-1;
                        }
                        else
                        {
    
    
                            rii=aa[rii].le;
                            aa[rii].ri=-1;
                        }
                    }
                    else
                    {
    
    
                        if(flag%2==0)
                        {
    
    
                            rii=aa[rii].le;
                            aa[rii].ri=-1;
                        }
                        else
                        {
    
    
                            lee=aa[lee].ri;
                            aa[lee].le=-1;
                        }
                    }
                }
            }
            //以上是分别处理7种不同操作
            
            
            //下面是输出部分,因为flag的原因,在输出链表部分的时候也得判断正反
            int fuck=0;
            for(int i=1;i<=hele;i++)
            {
    
    
                if(fuck==0)
                {
    
    
                    cout<<stle[i];
                    fuck=1;
                }
                else
                    cout<<" "<<stle[i];
            }
            if(flag%2==0)
            {
    
    
                while(lee!=-1)
                {
    
    
                    if(fuck==0)
                    {
    
    
                        cout<<aa[lee].val;
                        fuck=1;
                    }
                    else
                        cout<<" "<<aa[lee].val;
                    lee=aa[lee].ri;
                }
            }
            else
            {
    
    
                while(rii!=-1)
                {
    
    
                    if(fuck==0)
                    {
    
    
                        cout<<aa[rii].val;
                        fuck=1;
                    }
                    else
                        cout<<" "<<aa[rii].val;
                    rii=aa[rii].le;
                }
            }
            for(int i=heri;i>0;i--)
            {
    
    
                if(fuck==0)
                {
    
    
                    cout<<stri[i];
                    fuck=1;
                }
                else
                    cout<<" "<<stri[i];
            }
            cout<<endl;
        }
    }
}

1051.Wooden Sticks

参考

#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
struct sticks{
    
    
    int l;
    int w;
    int flag;
}s[5002];/*运用结构体使条理更清晰*/
//返回正数就是说 cmp 传入参数第一个要放在第二个后面, 负数就是传入参数第一个要放第二个前面, 如果是 0, 那就无所谓谁前谁后..
int cmp(const void *a,const void *b){
    
    /*struct的二级排序,先按l排若相等,则按w排,注意大小关系*/
    struct sticks *c=(sticks*)a;
    struct sticks *d=(sticks*)b;
    if(c->l!=d->l)
        return c->l - d->l;
    else
        return c->w - d->w;
}

int main(){
    
    
    int T,n;
    int i,j,count,cl,cw;/*count是计数的,ccurrent是当前的,所以cl为当前
的长,同理cw*/
    scanf("%d",&T);
    while(T--){
    
    
        scanf("%d",&n);
        memset(s,0,sizeof(s));/*清零防干扰*/
        for(i=0;i<n;i++)
            scanf("%d%d",&s[i].l,&s[i].w);
        qsort(s,n,sizeof(s[0]),cmp);/*这里排序的数目是n,从小到大排*/
        count=1;/*第一根木棍上来肯定要花1*/
        s[0].flag=1;/*标记下是否被容纳过*/
        cl=s[0].l;/*记录当前的l*/
        cw=s[0].w;/*记录当前的w*/
        /*用循环去跟当前比较,如果都小的话,就是能被容纳,不需要多加时间*/
        for(i=1;i<n;i++){
    
    
            for(j=i;j<n;j++){
    
    
                if(s[j].flag!=1&&s[j].l>=cl&&s[j].w>=cw){
    
    
                    s[j].flag=1;
                    cl=s[j].l;
                    cw=s[j].w;
                }
            }
            j=1;
            while(s[j].flag==1)/*判断是否全被容纳过,若是,则完成*/
                j++;
            i=j;/*找到最前边的没有被标记过的一组数,即剩下中最小的一组数,再次找*/
            if(i==n)
                break;/*跳出*/
            count++;/*每找过一次,就说明一次可能,所以count计数是所求结果*/
            s[i].flag=1;
            cl=s[i].l;/*重新刷新下当前数*/
            cw=s[i].w;
        }
        printf("%d\n",count);

    }
    return 0;
}

1050.Moving Tables

参考

方法1

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
    
    
    int t,n,count[410],i,start,end,k;
    scanf("%d",&t);
    while(t--){
    
    
        scanf("%d",&n);
        memset(count,0,sizeof(count));
        while(n--){
    
    
            scanf("%d%d",&start,&end);
            //可能出发位置比目的地房间大。
            if(start>end){
    
    
                //无论大小,我们都可以看做从小的房间移动到大的房间
                k=start;
                start=end;
                end=k;
            }
            if(start%2==0)//考虑实际情况,出发房间为偶数是减一,可参照题中给出的图一
               start-=1;
            if(end%2==1)//目的地房间为奇数时加一
               end+=1;
            for(i=start;i<=end;++i)
               count[i]+=10;
        }
        printf("%d\n",*max_element(count,count+400));//STL中寻找数列最大值函数
    }
    return 0;
}

max_element参考

方法2

#include <iostream>
using namespace std;
 
struct table{
    
    
	int front,back;
	bool use;
}tb[10001];
 
int main()
{
    
    
	int T,N,total;			//T为有几组数据,N为每组数据有几行数据,total为总用时
	int i,j,temp;			
	table tem;
 
	cin>>T;
	while(T--)
	{
    
    
		//**************************   输入
		cin>>N;
		for(i=0;i<N;++i)
		{
    
    
			cin>>tb[i].front>>tb[i].back;
 
			if(tb[i].front%2==1)
				tb[i].front+=1;
			if(tb[i].back%2==1)
				tb[i].back+=1;
 
			if(tb[i].front>tb[i].back)
			{
    
    
				temp=tb[i].front;
				tb[i].front=tb[i].back;
				tb[i].back=temp;
			}
		
			tb[i].use=true;
		}
		//*************************	   排序
 
		for(i=0;i<N-1;++i)
			for(j=i+1;j<N;++j)
				if(tb[i].front>tb[j].front)
				{
    
    
					tem=tb[i];
					tb[i]=tb[j];
					tb[j]=tem;
				}
 
		//*************************	   计算值
		total=0;
		for(i=0;i<N;++i)
		{
    
    
			if(tb[i].use==false)
				continue;
			
			temp=tb[i].back;
 
			for(j=i+1;j<N;++j)
			{
    
    
				if(tb[j].use==false)
					continue;
				if(tb[j].front>temp)
				{
    
    
					tb[j].use=false;
					temp=tb[j].back;
				}
			}	
			total+=10;
		}
		//*************************     输出
		cout<<total<<endl;
	}
	return 0;
}

5127.Dogs’ Candies

参考

#include <cstdio>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
#include <iostream>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f3f3f3f3f
#define mod 10000007
#define debug() puts("what the fuck!!!")
#define N 500010
#define M 1000000
#define ll long long
using namespace std;
ll n;
struct node
{
    
    
    ll p,q;
    ll num;
} zz[N];
 
int main()
{
    
    
    ll t,a,b;
    while(scanf("%lld",&n)&&n)
    {
    
    
        ll len=0;
        for(ll i=0; i<n; i++)
        {
    
    
            scanf("%lld%lld%lld",&t,&a,&b);
            if(t==1)
            {
    
    
                zz[len].p=a;
                zz[len].q=b;
                zz[len++].num=1;
            }
            else if(t==0)
            {
    
    
                ll maxx=-inf;
                for(ll j=0; j<len; j++)
                {
    
    
                    if(zz[j].num)
                    {
    
    
                         maxx=max(maxx,a*zz[j].p+b*zz[j].q);
                    }
 
                }
                printf("%lld\n",maxx);
            }
            else if(t==-1)
            {
    
    
                for(ll j=0; j<len; j++)
                    if(zz[j].p==a&&zz[j].q==b&&zz[j].num)
                    {
    
    
                        zz[j].num=0;
                        break;
                    }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Jingle_dog/article/details/120733199