博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ICPC训练联盟2021寒假冬令营(5)(部分题解):
阅读量:186 次
发布时间:2019-02-28

本文共 9223 字,大约阅读时间需要 30 分钟。

ICPC训练联盟2021寒假冬令营(5)(部分题解):


A - Brainman

Raymond Babbitt drives his brother Charlie mad. Recently Raymond counted 246 toothpicks spilled all over the floor in an instant just by glancing at them. And he can even count Poker cards. Charlie would love to be able to do cool things like that, too. He wants to beat his brother in a similar task.

Problem

Here’s what Charlie thinks of. Imagine you get a sequence of N numbers. The goal is to move the numbers around so that at the end the sequence is ordered. The only operation allowed is to swap two adjacent numbers. Let us try an example:
Start with: 2 8 0 3
swap (2 8) 8 2 0 3
swap (2 0) 8 0 2 3
swap (2 3) 8 0 3 2
swap (8 0) 0 8 3 2
swap (8 3) 0 3 8 2
swap (8 2) 0 3 2 8
swap (3 2) 0 2 3 8
swap (3 8) 0 2 8 3
swap (8 3) 0 2 3 8

So the sequence (2 8 0 3) can be sorted with nine swaps of adjacent numbers. However, it is even possible to sort it with three such swaps:

Start with: 2 8 0 3
swap (8 0) 2 0 8 3
swap (2 0) 0 2 8 3
swap (8 3) 0 2 3 8

The question is: What is the minimum number of swaps of adjacent numbers to sort a given sequence?Since Charlie does not have Raymond’s mental capabilities, he decides to cheat. Here is where you come into play. He asks you to write a computer program for him that answers the question. Rest assured he will pay a very good prize for it.

Input
The first line contains the number of scenarios.
For every scenario, you are given a line containing first the length N (1 <= N <= 1000) of the sequence,followed by the N elements of the sequence (each element is an integer in [-1000000, 1000000]). All numbers in this line are separated by single blanks.
Output
Start the output for every scenario with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the minimal number of swaps of adjacent numbers that are necessary to sort the given sequence. Terminate the output for the scenario with a blank line.
Sample Input
4
4 2 8 0 3
10 0 1 2 3 4 5 6 7 8 9
6 -42 23 6 28 -100 65537
5 0 0 0 0 0
Sample Output
Scenario #1:
3

Scenario #2:

0

Scenario #3:

5

Scenario #4:

0
大概意思就是要求逆序对数:可以直接求,或者模拟插入排序的过程也可以求:
第一种:直接遍历求a[i]后面的数中比a[i]小的数,累加起来:

#include
#include
#include
#include
#include
#include
using namespace std;#define N 1005typedef long long int ll;int main(){ int t; cin>>t; for(int i=1;i<=t;i++) { int n; cin>>n; int a[N]; for(int i=0;i

第二种:在插入排序的过程中发生交换动作记一次数

#include
#include
#include
#include
#include
#include
using namespace std;#define N 1005typedef long long int ll;int Insert_Sort(int a[],int n){ a[0]=INT_MIN; int res=0; for(int i=2;i<=n;i++) for(int j=i;a[j]
>t; for(int i=1;i<=t;i++) { int n; cin>>n; int a[N]; for(int j=1;j<=n;j++) //留出第一个空位方便操作 scanf("%d",&a[j]); int res=Insert_Sort(a,n); cout<<"Scenario #"<
<<":"<

还有一种(归并的过程中求逆序对数):数据量小的话,其实没必要用归并…

#include
#include
#include
#include
#include
#include
using namespace std;#define N 500005typedef long long int ll;int res;int temp[N];int merge(int src[],int des[],int i,int m,int n){ //将有序序列src[i...m],src[m+1,n]合并到des[i...n] int srcl=i,srcr=m+1,ldes=i; while(srcl<=m&&srcr<=n){ if(src[srcl]<=src[srcr]) { des[ldes++]=src[srcl++]; } else{ res+=m-srcl+1; des[ldes++]=src[srcr++]; } } while(srcl<=m) //判断并合并多余部分 { des[ldes++]=src[srcl++]; } while(srcr<=n) { des[ldes++]=src[srcr++]; } for(int j=i;j<=n;j++) //拷贝回原数组 src[j]=des[j]; return 0;}int mergesort(int src[],int temp[],int s,int t){ //将src[s...t]对半分解直至为长度为1的子序列,然后开始合并 if(s==t) return 0; else{ int m=(s+t)/2; //将序列分两组 mergesort(src,temp,s,m);//src[s...m]分解 mergesort(src,temp,m+1,t);//src[m+1...t]分解 merge(src,temp,s,m,t);//src[s..m]和src[m+1...t]合并到temp中,再将temp拷贝回src中 } return 0;}int merge_sort(int a[],int n){ // int temp[N]; //数组开大了只能开全局变量 memset(temp,0,sizeof(temp)); mergesort(a,temp,0,n-1); return 0;}int main(){ int t; cin>>t; int j=1; while(j<=t) { int n; cin>>n; int a[N]; for(int i=0;i

E - Word Amalgamation

In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four words. Your task is to write a program that can unscramble words.
Input
The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled ‘words’ that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X’s.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line “NOT A VALID WORD” instead. In either case, output a line containing six asterisks to signal the end of the list.
Sample Input
tarp
given
score
refund
only
trap
work
earn
course
pepper
part
XXXXXX
resco
nfudre
aptr
sett
oresuc
XXXXXX
Sample Output
score
******
refund
******
part
tarp
trap
******
NOT A VALID WORD
******
course
******
题意:先输入字典,再给出一组单词,要求在字典中查找所有和该词一样的单词(不比较字母顺序),若未找到输出NOT A VALID WORD即可
代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;#define N 1005#define PI acos(-1.0)#define reset(x) memset(x,0,sizeof(x))typedef long long int ll;bool cmp(pair
a1,pair
a2){ return a1.second
> dic; vector
wd; string s; cin>>s; while(s!="XXXXXX"){ string s0=s; sort(s.begin(),s.end()); dic.push_back( pair< string,string >(s,s0)); cin>>s; } sort(dic.begin(),dic.end(),cmp); cin>>s; while(s!="XXXXXX"){ sort(s.begin(),s.end()); wd.push_back(s); cin>>s; } for(int i=0;i

F - Flooded!

To enable homebuyers to estimate the cost of flood insurance, a real-estate firm provides clients with the elevation of each 10-meter by 10-meter square of land in regions where homes may be purchased. Water from rain, melting snow, and burst water mains will collect first in those squares with the lowest elevations, since water from squares of higher elevation will run downhill. For simplicity, we also assume that storm sewers enable water from high-elevation squares in valleys (completely enclosed by still higher elevation squares) to drain to lower elevation squares, and that water will not be absorbed by the land.
From weather data archives, we know the typical volume of water that collects in a region. As prospective homebuyers, we wish to know the elevation of the water after it has collected in low-lying squares, and also the percentage of the region’s area that is completely submerged (that is, the percentage of 10-meter squares whose elevation is strictly less than the water level). You are to write the program that provides these results.
Input
The input consists of a sequence of region descriptions. Each begins with a pair of integers, m and n, each less than 30, giving the dimensions of the rectangular region in 10-meter units. Immediately following are m lines of n integers giving the elevations of the squares in row-major order. Elevations are given in meters, with positive and negative numbers representing elevations above and below sea level, respectively. The final value in each region description is an integer that indicates the number of cubic meters of water that will collect in the region. A pair of zeroes follows the description of the last region.
Output
For each region, display the region number (1, 2, …), the water level (in meters above or below sea level) and the percentage of the region’s area under water, each on a separate line. The water level and percentage of the region’s area under water are to be displayed accurate to two fractional digits. Follow the output for each region with a blank line.
Sample Input
3 3
25 37 45
51 12 34
94 83 27
10000
0 0
Sample Output
Region 1
Water level is 46.67 meters.
66.67 percent of the region is under water.
题意:有海拔高度不同的楼房(题目以二维矩阵给出),现给定一定体积的水,问能淹没的高度以及淹没的楼占总楼数的百分比
最先淹没的肯定是海拔比较低的楼房,因此对输入的海拔排序,不断计算淹没高度h需要的水量,直至水消耗完
代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;#define N 1005#define PI acos(-1.0)#define reset(x) memset(x,0,sizeof(x))typedef long long int ll;int main(){ ios::sync_with_stdio(false); cin.tie(0); int k=1; int m,n; while(cin>>m>>n&&m&&n) { int a[N]; for(int i=0;i
>a[i]; sort(a,a+m*n); double sum; cin>>sum; double ht=a[0]; int count=1; if(sum) for(int i=1;i

转载地址:http://gkyc.baihongyu.com/

你可能感兴趣的文章