V——小M的移動硬盤 W——Languages X——數字和 Y——賞賜 OR 災難 Z——圖書管理員的表白方式

Description

最近小M買了一個移動硬盤來儲存自己電腦裏不常用的文件。但是他把這些文件一股腦丟進移動硬盤後,覺得這些文件似乎沒有被很好地歸類,這樣以後找起來豈不是會非常麻煩?
小M最終決定要把這些文件好好歸類,把同一類地移動到一起。所以現在小M有了這幾種操作:
1 u 表示把編號爲u的文件放到最上面
2 u 表示把編號爲u的文件放到最下面
3 u v 表示把編號爲u的文件放到編號爲v的文件的後面
已知在最開始的時候,1號文件到n號文件從上往下排布
現在小M已經給出了他所進行的所有操作,你能告訴他操作之後的序列是會變成什麼樣子嗎?

Input

第一行爲一個數字T(T<=10)表示數據組數
第二行爲兩個數字n、m(1<=n,m<=300000)表示序列長度和小M的操作次數
接下來m行每行兩個或三個數字,具體含義見題面
保證數據合法

Output

輸出一行表示小M操作結束後的序列

Sample Input

1
10 5
1 5
2 3
2 6
3 4 8
3 1 3

Sample Output

5 2 7 8 4 9 10 3 1 6


#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
#include <stack>
#include <set>
#include <algorithm>
using namespace std;
#define ll long long
const int maxn = 300005;

int n;
int m;
int leftt[maxn];
int rightt[maxn];
int flag;
int a, b;

void init()
{
	rightt[0] = 1;
	leftt[n + 1] = n;
	for (int i = 1; i <= n; i++)
	{
		leftt[i] = i - 1;
		rightt[i] = i + 1;
	}
}

void link(int l, int r)
{
	rightt[l] = r;
	leftt[r] = l;
}

int main()
{

	int T;
	cin >> T;
	while (T--)
	{
		cin >> n >> m;
		init();
		for (int i = 0; i < m; i++)
		{
			cin >> flag;
			if (flag == 1)
			{
				cin >> a;
				if(rightt[0]==a)
                    continue;
				int la = leftt[a];
				int ra = rightt[a];
				int r0 = rightt[0];
				link(la, ra);
				link(a, r0);
				link(0, a);
			}
			else if (flag == 2)
			{
				cin >> b;
				if(leftt[n+1]==b)
                    continue;
				int lb = leftt[b];
				int rb = rightt[b];
				int ln = leftt[n + 1];
				link(lb, rb);
				link(ln, b);
				link(b, n + 1);
			}
			else
			{
				cin >> a >> b;
				if(rightt[b]==a)
                    continue;
				int la = leftt[a];
				int ra = rightt[a];
				int rb = rightt[b];
				link(la, ra);
				link(a, rb);
				link(b, a);
			}
		}
		int temp = rightt[0];
		cout << temp;
		while (rightt[temp] != (n + 1))
		{
			cout << " "<< rightt[temp] ;
			temp = rightt[temp];
		}
		cout<<endl;
	}
	return 0;
}

/**********************************************************************
	Problem: 1982
	User: jk1601zr
	Language: C++
	Result: WA
**********************************************************************/



/**********************************************************************
	Problem: 1982
	User: jk1601zr
	Language: C++
	Result: AC
	Time:956 ms
	Memory:4368 kb
**********************************************************************/

Description

The Enterprise has encountered a planet that at one point had been inhabited. The only remnant from the prior civilization is a set of texts that was found. Using a small set of keywords found in various different languages, the Enterprise team is trying to determine what type of beings inhabited the planet.

Input

The first line of input will be N (1 ≤ N ≤ 100), the number of different known languages. The next N lines contain, in order, the name of the language, followed by one or more words in that language, separated with spaces. Following that will be a blank line. After that will be a series of lines, each in one language, for which you are to determine the appropriate language. Words consist of uninterrupted strings of upper or lowercase ASCII letters, apostrophes, or hyphens, as do the names of languages. No words will appear in more than one language. No line will be longer than 256 characters. There will be at most 1000 lines of sample text. Every sample text will contain at least one keyword from one of the languages. No sample text will contain keywords from multiple languages. The sample text may contain additional punctuation (commas, periods, exclamation points, semicolons, question marks, and parentheses) and spaces, all of which serve as delimiters separating keywords. Sample text may contain words that are not keywords for any specific language. Keywords should be matched in a case-insensitive manner.

Output

For each line of sample text that follows the blank line separating the defined languages, print a single line that identifies the language with which the sample text is associated.

Sample Input

4
Vulcan throks kilko-srashiv k'etwel
Romulan Tehca uckwazta Uhn Neemasta
Menk e'satta prah ra'sata
Russian sluchilos

Dif-tor heh, Spohkh. I'tah trai k'etwel
Uhn kan'aganna! Tehca zuhn ruga'noktan!

Sample Output

Vulcan
Romulan

#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <map>
#include <sstream>
using namespace std;

map<string,string>ma;
string lan,s;
char c;
string line,temp;

void change(string &str)
{
    int len=str.length();
    for(int i=0;i<len;i++)
        str[i]=tolower(str[i]);
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>lan;
        c=getchar();
        while(c!='\n')
        {
            cin>>s;
            change(s);
            ma[s]=lan;
            c=getchar();
        }
    }
    int flag=0;
    while(getline(cin,line))
    {
        for(int i=0;i<line.length();i++)
        {
            if(line[i]==','||line[i]==';'||line[i]=='.'||line[i]=='('||line[i]==')'||line[i]=='!'||line[i]=='?')
                line[i]=' ';
            stringstream ss(line);
            while(ss>>temp)
            {
                change(temp);
                if(flag==0)
                {
                    if(ma.count(temp))
                    {
                        cout<<ma[temp]<<endl;
                        flag=1;
                    }
                }
            }
        }
            if(c=='\n')
                flag=0;
    }
    return 0;
}

/**********************************************************************
	Problem: 1826
	User: jk1601zr
	Language: C++
	Result: AC
	Time:756 ms
	Memory:2348 kb
**********************************************************************/


Description

長者對小明施加了膜法,使得小明每天起牀就像馬丁的早晨一樣。 今天小明早上起來後發現身體雖然變小,頭腦依舊不變變傻。

他有一條紙帶,上面有n個數字,第i個數字爲Ai。 他想把紙帶選三個位置p1, p2, p3(p1 < p2 < p3)把紙帶剪成4個每條長度至少爲1的4條紙帶。 分別爲[1, p1-1], [p1+1, p2-1], [p2+1, p3-1], [p3+1, n],使得4個部分中數字之和相等。

Input

多組輸入 每組測試數據第一行輸入一個n(7 ≤ n ≤ 105) 第二行n個數,第i個數爲Ai(0 ≤ Ai ≤ 109),表示第i個數

Output

輸出字典序最小的p1,p2,p3 如果不存在這種操作,輸出-1

Sample Input

7
6 2 6 2 6 2 6
7
1 2 3 4 5 6 7

Sample Output

2 4 6
-1


#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <map>
#include <sstream>
using namespace std;
const int maxn = 1e5+10;
  
int a[maxn];  
int sum[maxn];  
int p1,p2,p3;  
int n;  

int slove()  
{  
    p1 = 2;  
    p2 = 4;  
    p3 = 6;  
    for(;p1<=n;p1++)  
    {  
        p2 = max(p2,p1+2);  
        while(p2<=n&&sum[p1-1]>sum[p2-1]-sum[p1]) p2++;  
        if(p2>=n||sum[p1-1]<sum[p2-1]-sum[p1])  
            continue;  
        p3 = max(p3,p2+2);  
        while(p3<=n&&sum[p1-1]>sum[p3-1]-sum[p2]) p3++;  
        if(p3>=n||sum[p3-1]-sum[p2]>sum[p1-1])  
            continue;  
        if(sum[n]-sum[p3]==sum[p1-1])  
        {  
            cout<<p1<<' '<<p2<<' '<<p3<<endl;  
            return 1;  
        }  
    }  
    return 0;  
} 
 
int main()  
{  
    while(cin>>n)  
    {  
        for(int i = 1 ;i<=n;i++)  
        {  
            cin>>a[i];  
            sum[i] = sum[i-1] + a[i];  
        }  
        if(!slove())  
        {  
            cout<<"-1"<<endl;  
        }  
    }  
}  

/**********************************************************************
	Problem: 1956
	User: jk1601zr
	Language: C++
	Result: AC
	Time:764 ms
	Memory:2804 kb
**********************************************************************/


Description

大G南征北戰終於打下了大片土地成立了G國,大G在開國大典上傳召幫助自己南征北戰的三大開國元勳小A,小B,小C進殿,並要賞賜三人大量寶物以顯示天恩浩蕩。大G在征服其他國家的時候搶奪了n箱寶物,他把這些箱子依次排列在三人面前,每個箱子裏的寶物都有一個價值wi,大G令他們一人選取一個箱子作爲獎勵。 可是令大G萬萬沒有想到的是,三人在私底下是存在競爭關係的,由於小B手上兵權強於小C,小C手上兵權強於小A。所以弱者總是擔心自己領取的賞賜高於或等於強者會招來殺身之禍。所以他們三人總是會讓小B先選取獎勵之後,小C會在小B選擇的右側區域選擇價值比小B小的獎勵,而小A則會在小B選擇的左側區域選擇價值比小B和小C都小的獎勵。當然小B是個聰明人,他也會考慮到兩人的想法選擇對大家都有幫助的方案選取。請問是否存在這樣一種選擇方案讓大家都不用擔心會招致殺身之禍。如果存在輸出“YES”,否則輸出“NO”

Input

多組數據讀入
每組數據第一行輸入一個正整數n表示n箱寶物(n<=100000) 接下來一行輸入n個正整數w1,w2,w3,...,wn表示n箱寶物的價值。(wi<=10000000) 題目保證所有數據n的總和不超過500000

Output

如果存要求的選擇方案則輸出“YES”,否則輸出“NO”。

Sample Input

6
1 2 3 6 5 4
6
1 2 3 4 5 6

Sample Output

YES
NO

#include<iostream>
#include<string>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN=100005;
const int INF=1000000000;
int Min[MAXN];
int w[MAXN];
stack<int> s;

int main()
{
    int n;
    while(cin>>n)
    {
        while(!s.empty()) s.pop();
        for(int i=1;i<=n;i++)
        {
            cin>>w[i];
            if(i==1) Min[i]=w[i];
            else Min[i]=min(Min[i-1],w[i]);
        }
        bool flag=false;
        s.push(w[n]);
        for(int i=n-1;i>=2;i--)
        {
            int a=Min[i-1];
            int c=-1;
            while(!s.empty()&&s.top()<w[i])
            {
                c=s.top();
                s.pop();
            }
            s.push(w[i]);
            if(c>a)
            {
                flag=true;
                break;
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}
/**********************************************************************
	Problem: 1901
	User: jk1601zr
	Language: C++
	Result: AC
	Time:528 ms
	Memory:3196 kb
**********************************************************************/


Description

小V是中南大學圖書館的圖書管理員,每天要整理很多同學們還回來的書。久而久之,他認識了很多常來圖書館的同學,比如說小L。簡而言之吧,就是小V喜歡上了小L,並且想在下一次她來還書的時候表白。
小V的創意還是不錯的,他精心準備了各種材料,打算構成“L”,“O”,“V”,“E”四個字母,在小L來的時候悄悄組合起來給她看。但是意外來了:在小L來的時候,小V只准備好了“L”,“O”,和“E”,“V”還沒有拼好!但是機智的小V立刻想到了一個辦法:他可以隨手把旁邊別人還的書合在一起,並且抽掉其中一部分,令剩下的書的高度構成了一個“V”形。
那麼問題來了:已知N本書的高度,在不改變他們的順序的前提下,能不能得到小V想要的“V”,如果可以的話,最少去掉多少本書呢?
(組成“V”的前提:h1>h2...<hn,即整個高度必須先遞減再遞增)

Input


多組數據,第一行有一個整數T,表示有T組數據。(T<=100
以下每組數據第一行有一個整數N,表示這一排書的數量。(1<=N<=100)

然後接下來一行是N個整數,h1,h2...hn分別代表每本書的高度。(1<=hi<=100)

Output

如果可以構成”V”,輸出“HAPPY”,並在下一行輸出所需拿掉的最少數量。
如果不能,輸出“SAD”。

Sample Input

7
3
3 2 3
4
3 2 4 3
5
1 2 4 6 7
1
22
2
25 8
3
98 16 68
4
88 14 82 69

Sample Output

HAPPY
0
HAPPY
1
SAD
SAD
SAD
HAPPY
0
HAPPY
1

#include<iostream>  
#include<cstring>  
using namespace std;  
int dp1[105],dp2[105];  
int ax[105];  
int main()  
{  
    int T;  
    cin>>T;  
    while(T--)  
    {  
        int N,flag1=0,flag2=0,cnt=100000;  
        cin>>N;  
        for(int i=0;i<N;i++)  
        {  
            cin>>ax[i];  
            dp1[i]=dp2[i]=1;  
        }  
        for(int j=1;j<N-1;j++)  
        {  
             for(int i=0;i<N;i++)  
              dp1[i]=dp2[i]=1;  
  
            int cnt1=0,cnt2=0;  
            for(int i=j;i>=0;i--) 
            {  
                for(int k=i-1;k>=0;k--)  
                {  
                    if(ax[i]<ax[k])  
                   {  
                    flag1=1;  
                    dp1[k]=max(dp1[k],dp1[i]+1);  
                   }  
                   cnt1=max(dp1[k],cnt1);  
            }  
            }  
            for(int k=j;k<=N-1;k++) 
            {  
                for(int i=k+1;i<=N-1;i++)  
                {  
                    if(ax[k]<ax[i])  
                  {  
                    flag2=1;  
                    dp2[i]=max(dp2[i],dp2[k]+1);  
                  }  
                    cnt2=max(dp2[i],cnt2);  
                }  
  
            }  
            cnt=min(N-(cnt1+cnt2-1),cnt);  
        }  
        if(flag1&&flag2)  
          {  
              cout<<"HAPPY"<<endl;  
              cout<<cnt<<endl;  
          }  
        else  
            cout<<"SAD"<<endl;  
  
    }  
    return 0;  
}  
/**********************************************************************
	Problem: 1560
	User: jk1601zr
	Language: C++
	Result: AC
	Time:56 ms
	Memory:2024 kb
**********************************************************************/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章